要求路徑折線

computeRoutes 方法 (REST) 和 ComputeRoutes 方法 (gRPC) 都會傳回折線代表的路線, 回應。這些 API 會傳回兩種折線:

  • 基本折線 (預設):表示路線但沒有路況 嵌入於折線中的特定資訊傳回基本折線的要求 按路徑基本費率計費。進一步瞭解 為 Routes API計費。

  • 可偵測路況的折線:包含路況資訊 沿著路線行駛路況以速度表示 特定時間間隔適用的類別 (NORMALSLOWTRAFFIC_JAM) 折線的控制機制針對流量感知折線的要求,計費方式為 路線偏好費率。若想進一步瞭解計費方式,請參閱這篇文章。 Routes API。詳情請參閱 設定折線品質

如要進一步瞭解折線,請參閱:

要求路線、路段或步數的基本折線

折線是以 Polyline (REST) 或 Polyline (gRPC) 物件。 您可以在路線、航段和步驟層級在回應中傳回折線。

使用 回應欄位遮罩

  • 在路線層級,在回應中傳回折線,方法是加入 routes.polyline 的值。

  • 在腿部層級,在第一個路段的回應中傳回折線 路線,包括 routes.legs.polyline

  • 在步驟層級,在每個步驟的回應中傳回折線 加入 routes.legs.steps.polyline 即可調整車程

例如,傳回整個路線、每個航段的折線,以及 每段航程的每個步驟:

curl -X POST -d '{
  "origin":{
    "address": "1600 Amphitheatre Parkway, Mountain View, CA"
  },
  "destination":{
    "address": "24 Willie Mays Plaza, San Francisco, CA 94107"
  },
  "travelMode": "DRIVE"
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline,routes.legs.polyline,routes.legs.steps.polyline' \
'https://routes.googleapis.com/directions/v2:computeRoutes'

這個要求會傳回以下回應,其中包含 針對路線的每一個路段,以及路段的每一個步驟:

{
  "routes": [
    {
      "legs": [
        {
          "polyline": {
              "encodedPolyline": "ipkcFfich...@Bs@?A?O?SD{A@o@B}@I?qA?_AA_@@_@?"
          }
        },
          "steps": [
              {
                  "polyline": {
                      "encodedPolyline": "kclcF...@sC@YIOKI"
                  }
              },
              {
                  "polyline": {
                      "encodedPolyline": "wblcF~...SZSF_@?"
                  }
              },
              ...
      ],
      "distanceMeters": 56901,
      "duration": "2420s",
      "polyline": {
        "encodedPolyline": "ipkcFfich...@Bs@?A?O?SD{A@o@B}@I?qA?_AA_@@_@?"
      }
    }
  ]
}

由於這項要求只包含起點和目的地,因此會傳回 路線只包含一個航段因此,形狀的折線 路徑都相同

如果您在要求中加入中繼路點,則傳回的 路線包含兩個路段:

curl -X POST -d '{
  "origin":{
    "address": "1600 Amphitheatre Parkway, Mountain View, CA"
  },
  "destination":{
    "address": "24 Willie Mays Plaza, San Francisco, CA 94107"
  },
  "intermediates": [
    { "address": "450 Serra Mall, Stanford, CA 94305, USA"},
  ],
  "travelMode": "DRIVE",
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline,routes.legs.polyline' \
'https://routes.googleapis.com/directions/v2:computeRoutes'

此要求會傳回兩條航段,每個航段各有一個不重複的折線,而 整條路線:

{
  "routes": [
    {
      "legs": [
        {
          "polyline": {
            "encodedPolyline": "kclcFfqchV?A...?I@G?GAECCCEKICBAFG"
          }
          "steps": [
            {
                "polyline": {
                    "encodedPolyline": "kclcFfqch...YIOKI"
                }
            },
        ...
        },
        {
          "polyline": {
            "encodedPolyline": "ojmcFtethV?K...QOYQOGA?_@MUG[Ga@G"
          }
          "steps": [
            {
                "polyline": {
                    "encodedPolyline": "uypeFbo`jVgJq...PoBiC"
                }
            },
        ...
        }
      ],
      "distanceMeters": 68403,
      "duration": "3759s",
      "polyline": {
          "encodedPolyline": "kclcFfqchV?A?CBKF[Ha...?GAECCCEKICBAFGJEBE"
      }
    }
  ]
}

折線品質

下列字詞可用來說明折線的品質:

  • 點的浮點精確度

    點的指定方式是以經緯度值表示, 這種模型具有單精度浮點格式這個做法適合小數值 (能以精確的方式表示),但精確度會下降。 增加,因為浮點捨入錯誤。

    computeRoutes 方法 (REST) 和 ComputeRoutes, 這是由 polylineEncoding 控制。

  • 折線的組成點數量

    使用的點越多,折線也越流暢 (尤其是在 。

    computeRoutes 方法 (REST) 和 ComputeRoutes, 這是由 polylineQuality 控制。

設定折線編碼類型

使用 polylineEncoding 要求選項控制折線類型。 polylineEncoding 屬性可控制是否要將折線編碼為 ENCODED_POLYLINE (預設),意即 編碼折線演算法格式GEO_JSON_LINESTRING」,也就是 GeoJSON LineString 格式

例如,在要求主體中:

curl -X POST -d '{
  "origin":{
    "address": "1600 Amphitheatre Parkway, Mountain View, CA"
  },
  "destination":{
    "address": "24 Willie Mays Plaza, San Francisco, CA 94107"
  },
  "travelMode": "DRIVE",
  "polylineEncoding": "ENCODED_POLYLINE"
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline,routes.legs.polyline' \
'https://routes.googleapis.com/directions/v2:computeRoutes'

設定折線品質

polylineQuality 會將折線的品質指定為 HIGH_QUALITYOVERVIEW (預設)。使用 OVERVIEW 時,折線是以 和 HIGH_QUALITY 要求延遲時間較短

例如,在要求主體中:

{
  "origin":{
    "location":{
      "latLng":{
        "latitude": 37.419734,
        "longitude": -122.0827784
      }
    }
  },
  "destination":{
    "location":{
      "latLng":{
        "latitude": 37.417670,
        "longitude": -122.079595
      }
    }
  },
  "travelMode": "DRIVE",
  "routingPreference": "TRAFFIC_AWARE",
  "polylineQuality": "HIGH_QUALITY",
  "polylineEncoding": "ENCODED_POLYLINE",
  "departureTime": "2023-10-15T15:01:23.045123456Z",
  ...
}

要求可偵測車流量的折線

上方所示的範例所有會傳回基本折線,也就是沒有 路況資訊此外,您也可以要求折線包含 路線和路線中每個路段的路況資訊。

可偵測路況的折線包含沿途路況的資訊 這個路徑路況會以速度類別表示 指定回應時間間隔 (NORMALSLOWTRAFFIC_JAM) 折線。間隔是由其起始 (含) 的索引定義。 和結束 (獨佔) 折線點。

例如,以下回應顯示折線之間的 NORMAL 流量 第 2 點和第 4 點:

{
  "startPolylinePointIndex": 2,
  "endPolylinePointIndex": 4,
  "speed": "NORMAL"
}

如要要求計算流量感知折線,請設定下列內容: 要求中的屬性:

  • extraComputations 陣列欄位設為 TRAFFIC_ON_POLYLINE 即可啟用 流量計算方式

  • travelMode 設為 DRIVETWO_WHEELER。為任何其他人員發出的要求 交通方式傳回錯誤。

  • 指定 TRAFFIC_AWARETRAFFIC_AWARE_OPTIMAL 轉送 偏好設定。詳情請參閱設定品質與 延遲時間

  • 設定用來指定傳回回應屬性的回應欄位遮罩:

    • 在路線層級,於回應中傳回所有旅遊資訊: 在回應欄位遮罩中納入 routes.travelAdvisory。返回 請提供路況資訊 routes.travelAdvisory.speedReadingIntervals

    • 在車廂層級,在回應中傳回所有旅遊資訊 路線的每個路段,方法是加入 routes.legs.travelAdvisory。返回 請提供路況資訊 routes.legs.travelAdvisory.speedReadingIntervals

curl -X POST -d '{
  "origin":{
    "address": "1600 Amphitheatre Parkway, Mountain View, CA"
  },
  "destination":{
    "address": "24 Willie Mays Plaza, San Francisco, CA 94107"
  },
  "travelMode": "DRIVE",
  "extraComputations": ["TRAFFIC_ON_POLYLINE"],
  "routingPreference": "TRAFFIC_AWARE_OPTIMAL"
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline,routes.legs.polyline,routes.travelAdvisory,routes.legs.travelAdvisory' \
'https://routes.googleapis.com/directions/v2:computeRoutes'

偵測流量折線的回應範例

在回應中,流量資料會在折線中編碼,並包含在 travelAdvisory 欄位,類型為 RouteLegTravelAdvisory 物體 (每條航段) 和 RouteTravelAdvisory 物件 (route)。

例如:

{
  "routes": [
    {
      "legs": {
        "polyline": {
          "encodedPolyline": "}boeF~zbjVAg@EmB`GWHlD"
        },
        // Traffic data for the leg.
        "travelAdvisory": {
          "speedReadingIntervals": [
            {
              "endPolylinePointIndex": 1,
              "speed": "NORMAL"
            },
            {
              "startPolylinePointIndex": 1,
              "endPolylinePointIndex": 2,
              "speed": "SLOW"
            },
            {
              "startPolylinePointIndex": 2,
              "endPolylinePointIndex": 4,
              "speed": "NORMAL"
            }
          ] 
        }
      },
      "polyline": {
        "encodedPolyline": "}boeF~zbjVAg@EmB`GWHlD"
      },
      // Traffic data for the route.
      "travelAdvisory": {
        "speedReadingIntervals": [
          {
            "endPolylinePointIndex": 1,
            "speed": "NORMAL"
          },
          {
            "startPolylinePointIndex": 1,
            "endPolylinePointIndex": 2,
            "speed": "SLOW"
          },
          {
            "startPolylinePointIndex": 2,
            "endPolylinePointIndex": 4,
            "speed": "NORMAL"
          }
        ] 
      }
    }
  ]
}

RouteTravelAdvisoryRouteLegTravelAdvisory 都含有陣列欄位 呼叫 speedReadingIntervals,其中包含車流量速度資訊。每項 陣列中的物件會由 SpeedReadingInterval (REST) 或 SpeedReadingInterval (gRPC) 物件。

SpeedReadingInterval 物件包含路線間隔的速度讀數。 例如 NORMALSLOWTRAFFIC_JAM涵蓋整個物件陣列 整個路線沒有重疊。 間隔與前一個間隔的終點相同。

每個間隔都是由其 startPolylinePointIndex 說明。 endPolylinePointIndex 和對應的速度類別。 請注意,間隔內缺少起始索引對應的索引 0 按照 proto3 做法

startPolylinePointIndexendPolylinePointIndex 值不是 一律接續播放。例如:

{
  "startPolylinePointIndex": 2,
  "endPolylinePointIndex": 4,
  "speed": "NORMAL"
}

在本例中,路況 與索引 2 相同,到索引 4。

使用 Maps SDK 轉譯可偵測交通情形的折線

建議您在地圖上顯示交通情況折線時,使用 Google Maps SDK 提供的功能,包括自訂色彩、筆觸和 沿著折線延伸的圖案如需進一步瞭解如何使用折線 看 Android 折線功能iOS 適用的折線功能

折線算繪範例

Maps SDK 使用者可定義自訂地圖 速度類別和折線算繪結構定義之間的邏輯。身為 舉例來說,使用者可能決定顯示「NORMAL」是藍色的 「SLOW」時的地圖例如,速度可能會以粗的橘色線條顯示。

下列程式碼片段會根據測地線段,加入粗的藍色折線 墨爾本到伯斯。若需更多資訊,請參閲 自訂外觀 (Android) 和 自訂折線 (iOS 版)。

Android

Java

Polyline line = map.addPolyline(new PolylineOptions()
    .add(new LatLng(-37.81319, 144.96298), new LatLng(-31.95285, 115.85734))
    .width(25)
    .color(Color.BLUE)
    .geodesic(true));

Kotlin

val line: Polyline = map.addPolyline(
  PolylineOptions()
    .add(LatLng(-37.81319, 144.96298), LatLng(-31.95285, 115.85734))
    .width(25f)
    .color(Color.BLUE)
    .geodesic(true)
)

iOS

Objective-C

GMSMutablePath *path = [GMSMutablePath path];
[path addLatitude:-37.81319 longitude:144.96298];
[path addLatitude:-31.95285 longitude:115.85734];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 10.f;
polyline.strokeColor = .blue;
polyline.geodesic = YES;
polyline.map = mapView;

Swift

let path = GMSMutablePath()
path.addLatitude(-37.81319, longitude: 144.96298)
path.addLatitude(-31.95285, longitude: 115.85734)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 10.0
polyline.geodesic = true
polyline.map = mapView