要求路徑折線

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

  • 基本折線 (預設) 代表路線,但折線沒有內嵌的車流量資訊。傳回基本折線的要求會按照 Routes Basic 費率計費。進一步瞭解 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'

設定折線品質

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'

偵測流量折線的回應範例

在回應中,車流量資料會編碼在折線中,並納入類型為 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」速度,「慢」速度則顯示為粗橘色。

下列程式碼片段會針對墨爾本至伯斯的測地線段,加上粗的藍色折線。詳情請參閱「自訂外觀」(適用於 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