折線編碼是有損的壓縮演算法,可讓您將一系列座標 (例如路徑) 儲存為單一字串。編碼程序會使用 base64 編碼配置,將二進位值轉換為一系列 ASCII 字元的字元代碼。如需編碼程序的完整說明,請參閱編碼折線演算法格式一文。
computeRoutes 方法 (REST) 和 ComputeRoutes 方法 (gRPC) 都會在回應中傳回折線代表的路徑。這些 API 會傳回兩種折線:
基本折線 (預設):代表路徑,但沒有嵌入折線的路況資訊。傳回基本折線的要求會以路徑基本費用計費。進一步瞭解 Routes API 的計費方式。
路況感知折線:包含沿途路況的相關資訊。路況條件會以適用折線指定速度的速度類別 (
NORMAL
、SLOW
、TRAFFIC_JAM
) 表示。系統會針對流量感知的折線要求使用「偏好的路徑」費率。進一步瞭解 Roads 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 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'
要求可感知到的折線
以上範例均傳回基本折線,也就是沒有路況資訊的折線。此外,您也可以要求折線包含路線和路線中每段路線的路況資訊。
流量感知折線包含路線上的路況資訊。回應條件是按照回應折線的特定時間間隔以速度類別 (NORMAL
、SLOW
、TRAFFIC_JAM
) 表示。間隔由起始 (包含) 和結束 (不含) 折線點的索引所定義。
舉例來說,下列回應顯示折線第 2 點和第 4 點之間的 NORMAL
流量:
{ "startPolylinePointIndex": 2, "endPolylinePointIndex": 4, "speed": "NORMAL" }
如要提出要求以計算流量感知的折線,請在要求中設定下列屬性:
將
extraComputations
陣列欄位設為TRAFFIC_ON_POLYLINE
,即可啟用流量計算。將
travelMode
設為DRIVE
或TWO_WHEELER
。其他交通方式的要求則會傳回錯誤。在要求中指定
TRAFFIC_AWARE
或TRAFFIC_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" } ] } } ] }
RouteTravelAdvisory
和 RouteLegTravelAdvisory
都含有名為 speedReadingIntervals
的陣列欄位,當中包含流量速度資訊。陣列中的每個物件都以 SpeedReadingInterval (REST) 或 SpeedReadingInterval (gRPC) 物件表示。
SpeedReadingInterval
物件包含路徑間隔的速度讀取,如 NORMAL
、SLOW
或 TRAFFIC_JAM
。整個物件陣列會覆蓋路徑的完整折線,且不會重疊。指定間隔的起始點與前一個間隔的結束點相同。
每個間隔都透過其 startPolylinePointIndex
、endPolylinePointIndex
和對應的速度類別來描述。請注意,根據 proto3 做法,在間隔內沒有起始索引與索引 0 對應。
startPolylinePointIndex
和 endPolylinePointIndex
值不一定連續。例如:
{ "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