要求路徑折線

computeRoutes 方法 (REST) 和 ComputeRoutes 方法 (gRPC) 都會將折線表示的路線做為回應的一部分。這些 API 會傳回兩種類型的折線:

  • 基本折線 (預設):表示路線,但不會嵌入折線中的車流量資訊。如果要求傳回基本折線,將按路徑基本費率計費。進一步瞭解 Routes API 的計費方式

  • 車流量感知折線:包含路線沿途的路況資訊。路況以折線指定間隔適用的速度類別 (NORMALSLOWTRAFFIC_JAM) 表示。可感知車流量的折線要求會按照「偏好路徑」費率計費。進一步瞭解 Routes API 的計費方式。詳情請參閱「設定折線品質」一文

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

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

折線以折線 (REST) 或折線 (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'

設定 Poyline 品質

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) 表示,在回應折線的指定時間間隔。間隔是由起始 (含) 和結束折線點 (不含) 的索引定義。

舉例來說,下列回應顯示折線點 2 到 4 之間的 NORMAL 流量:

{
  "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 物件 (路線)。

例如:

{
  "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) 的速度讀數。整個物件陣列會涵蓋路線的整個折線,而且不會重疊。指定間隔的起點與前一個間隔的終點相同。

每個間隔都是由其 startPolylinePointIndexendPolylinePointIndex 和對應的速度類別來描述。請注意,時間間隔內缺少開始索引會按照 proto3 做法的索引 0 對應。

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