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
如需详细了解如何指定响应字段掩码,请参阅“选择要返回的字段”。
示例响应
只要通过字段掩码请求 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 的用户有机会定义速度类别与多段线渲染架构之间的自定义映射逻辑。例如,用户可能会决定在地图上以粗蓝线显示“正常”速度,以粗橙线显示“缓慢”速度,依此类推。
以下代码段将会添加一条蓝色的粗多段线,其中包含从墨尔本至珀斯的测地线段。如需了解详情,请参阅自定义外观(适用于 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