请求多段线的路况信息

Routes Preferred API 可用于请求沿多段线的路况信息。路况信息以速度类别(NORMAL、SLOW、TRAFFIC_JAM)的形式表示,这些类别适用于响应多段线的给定间隔。间隔由多段线的起点(含)和终点(不含)多段线点的索引来定义。

示例请求

路况感知多段线适用于路线级别和路程级别。在路线级别,车流速度信息以 RouteTravelAdvisory 响应字段下的 SpeedReadingIntervals 提供。为了接收路线多段线旁边的路况信息,请在响应字段掩码中同时包含 polylinespeedReadingIntervals

如果字段掩码包含 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 下提供路程级路况信息。每个间隔由其 startPolylinePointIndexendPolylinePointIndex 和相应的速度类别进行描述。请注意,根据 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