Routes Preferred API 可讓您要求折線上的路況相關資訊。路況類別會以速度類別 (NORMAL、SLOW、TRAFFIC_JAM) 的形式表示,適用於回應折線的指定時間間隔。間隔是由起始 (含) 和結束折線點 (不含) 的索引定義。
要求範例
車流量感知折線支援路線層級和航線層級。在路線層級,流量速度資訊會以 SpeedReadingIntervals 的形式顯示在 RouteTravelAdvisory 回應欄位下方。如要與路線折線一起接收流量資訊,請在回應欄位遮罩中加入 polyline
和 speedReadingIntervals
。
如果欄位遮罩包含 routes.legs.travelAdvisory.speedReadingIntervals
,則回應會包含 RouteLegTravelAdvisory 下的車程等級車流量資料。
X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline,routes.travelAdvisory.speedReadingIntervals,routes.legs.polyline.encodedPolyline,routes.legs.travelAdvisory.speedReadingIntervals
如需指定回應欄位遮罩的其他詳細資料,請參閱「選擇要傳回的欄位」。
回應範例
只要透過欄位遮罩要求 speedReadingIntervals
,就會填入 routes.travelAdvisory.speedReadingIntervals
下方。可以在 routes.legs.travelAdvisory.speedReadingIntervals
下查看腿部等級的路況。
每個間隔都是由其 startPolylinePointIndex
、endPolylinePointIndex
和對應的速度類別來描述。請注意,時間間隔內缺少起始索引會依照 proto3 做法的索引 0 對應。
{ "routes": [ { "legs": { "polyline": { "encodedPolyline": "}boeF~zbjVAg@EmB`GWHlD" }, "travelAdvisory": { "speedReadingIntervals": [ { "endPolylinePointIndex": 1, "speed": "NORMAL" }, { "startPolylinePointIndex": 1, "endPolylinePointIndex": 2, "speed": "SLOW" }, { "startPolylinePointIndex": 2, "endPolylinePointIndex": 4, "speed": "NORMAL" } ] } }, "polyline": { "encodedPolyline": "}boeF~zbjVAg@EmB`GWHlD" }, "travelAdvisory": { "speedReadingIntervals": [ { "endPolylinePointIndex": 1, "speed": "NORMAL" }, { "startPolylinePointIndex": 1, "endPolylinePointIndex": 2, "speed": "SLOW" }, { "startPolylinePointIndex": 2, "endPolylinePointIndex": 4, "speed": "NORMAL" } ] } } ] }
使用 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