请求多段线的路况信息

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

请求示例

路线级和路段级均提供交通状况感知多段线。在路线级,交通速度信息以 SpeedReadingIntervals 的形式提供,位于 RouteTravelAdvisory 响应字段下。如需在路线的多段线旁边接收交通信息,请在响应字段掩码中同时添加 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 的用户有机会定义速度类别与多段线渲染架构之间的自定义映射逻辑。例如,用户可能会决定在地图上将“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