要求路徑折線

折線編碼是一種有損壓縮演算法,可將一系列座標 (例如路徑) 儲存為單一字串。編碼程序會使用 Base64 編碼配置,將二進位值轉換為一系列 ASCII 字元字元碼。如需編碼程序的完整說明,請參閱「編碼折線演算法格式」。

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"
      }
    }
  ]
}

設定折線編碼類型

使用 polylineEncoding 要求選項即可控制折線類型。 polylineEncoding 屬性可將折線編碼為 ENCODED_POLYLINE (預設),表示使用折線編碼演算法 (即 GEO_JSON_LINESTRING),代表採用 GeoJSON 行字串格式。

例如,在要求內文中:

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'

要求考量車流量的折線

以上範例顯示所有基本折線,也就是不含交通資訊的折線。此外,您也可以要求折線包含路線和每條路段的路況資訊。

感知折線,內含沿途路況資訊。系統會依據指定折線折線的時間間隔,以速度類別 (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'

以情境感知折線的回應範例

在回應中,流量資料會以折線編碼,並包含在 RouteLegTravelAdvisory 物件 (每個路段) 和 RouteTravelAdvisory 物件類型 (路徑) 的 travelAdvisory 欄位中。

例如:

{
  "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