折線編碼是一種失真壓縮演算法,可讓您將一系列座標 (例如路徑) 儲存為單一字串。編碼程序會使用 base64 編碼配置,將二進位值轉換為一系列 ASCII 字元的字元碼。如需編碼程序的完整說明,請參閱編碼折線演算法格式。
computeRoutes 方法 (REST) 和 ComputeRoutes 方法 (gRPC) 都會在回應中傳回折線表示的路徑。這些 API 會傳回兩種折線:
基本折線 (預設) 代表路線,但沒有嵌入折線中的車流量資訊。傳回基本折線的要求會按路徑基本費率計費。進一步瞭解 Routes API 的計費方式。
可偵測車流量的折線,包含路線沿途的路況資訊。路況是以適用於折線特定時間間隔的速度類別 (
NORMAL
、SLOW
、TRAFFIC_JAM
) 表示。針對車流量感知折線的要求是以「偏好的路徑」費率計算。進一步瞭解 Routes API 的計費方式。詳情請參閱「設定折線品質」一文
如要進一步瞭解折線,請參閱:
- 設定折線品質與延遲時間以瞭解如何設定折線的品質。
- 編碼折線演算法格式說明用於將折線編碼的演算法。
互動式折線編碼器公用程式可讓您在 UI 中建立編碼折線,或對要顯示在地圖上的折線進行解碼。舉例來說,您可以使用這個公用程式,解碼以下程式碼建立的折線。
要求路線、路段或步驟的基本折線
折線是以折線 (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" } } ] }
設定折線編碼類型
使用 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'
交通感知折線的回應範例
在回應中,流量資料會編碼於折線,並包含在 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" } ] } } ] }
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