Routes Preferred API 可讓您要求折線沿途的路況資訊。路況會以速度類別 (NORMAL、SLOW、TRAFFIC_JAM) 表示,適用於回應折線的特定間隔。間隔是由折線點的起始 (含) 和結束 (不含) 索引定義。
要求範例
路線層級和路段層級都提供車流量監測折線。
在路線層級,車流速度資訊會以 RouteTravelAdvisory 回應欄位下的 SpeedReadingIntervals 形式提供。如要接收路線折線的車流量資訊,請在回應欄位遮罩中加入 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
如要進一步瞭解如何指定回應 fieldmask,請參閱「選擇要傳回的欄位」。
回應範例
只要透過欄位遮罩要求 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 地圖 SDK 提供的各種功能,在地圖上顯示可感知交通流量的折線,包括自訂顏色、筆觸和沿著折線延伸的模式。如要進一步瞭解如何使用折線,請參閱「Android 適用的折線功能」和「iOS 適用的折線功能」。
折線的算繪範例
地圖 SDK 使用者可定義速度類別與折線算繪架構之間的自訂對應邏輯。 舉例來說,您可能會決定在地圖上以粗藍線顯示「正常」速度,以粗橘線顯示「緩慢」速度,依此類推。
下列程式碼片段會針對墨爾本至伯斯的測地線段,加上粗的藍色折線。詳情請參閱「自訂外觀」(適用於 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