请求路线多段线

computeRoutes 方法 (REST) 和 ComputeRoutes 方法 (gRPC) 均在响应中返回由多段线表示的路线。这些 API 会返回两种类型的多段线:

  • 基本多段线(默认),表示路线,但多段线上没有嵌入路况信息。返回基本多段线的请求会按路线基本费率计费。详细了解 Routes API 的结算功能

  • 路况感知多段线包含有关路线沿途路况的信息。路况会以适用于多段线指定间隔的速度类别(NORMALSLOWTRAFFIC_JAM)表示。对路况感知多段线的请求按路线首选费率计费。详细了解 Routes API 的结算功能。如需了解详情,请参阅配置多段线的质量

有关多段线的详细信息,请参阅:

请求路线、路程或路段的基本多段线

多段线由 多段线 (REST) 或 多段线 (gRPC) 对象表示。您可以在响应中返回路线、路程和路段级别的多段线。

使用响应字段掩码指定要返回的多段线:

  • 在路线级别,通过在响应字段掩码中添加 routes.polyline,在响应中返回多段线。

  • 在路程级别,通过添加 routes.legs.polyline,为路线的每段路程返回一条多段线。

  • 在路段级别,通过添加 routes.legs.steps.polyline,在路段每个路段的响应中返回一条多段线。

例如,如需为整个路线、每条路程和每条路程的每个路段返回一条多段线,请使用以下代码:

curl -X POST -d '{
  "origin":{
    "address": "1600 Amphitheatre Parkway, Mountain View, CA"
  },
  "destination":{
    "address": "24 Willie Mays Plaza, San Francisco, CA 94107"
  },
  "travelMode": "DRIVE"
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline,routes.legs.polyline,routes.legs.steps.polyline' \
'https://routes.googleapis.com/directions/v2:computeRoutes'

此请求会返回以下响应,其中包含路线的多段线、路线的每段路程以及路程的每个路段的多段线:

{
  "routes": [
    {
      "legs": [
        {
          "polyline": {
              "encodedPolyline": "ipkcFfich...@Bs@?A?O?SD{A@o@B}@I?qA?_AA_@@_@?"
          }
        },
          "steps": [
              {
                  "polyline": {
                      "encodedPolyline": "kclcF...@sC@YIOKI"
                  }
              },
              {
                  "polyline": {
                      "encodedPolyline": "wblcF~...SZSF_@?"
                  }
              },
              ...
      ],
      "distanceMeters": 56901,
      "duration": "2420s",
      "polyline": {
        "encodedPolyline": "ipkcFfich...@Bs@?A?O?SD{A@o@B}@I?qA?_AA_@@_@?"
      }
    }
  ]
}

由于此请求仅包含出发地和目的地,因此返回的路线仅包含一段路程。因此,路程的多段线和路线的多段线是相同的。

如果您在请求中添加中间航点,则返回的路线将包含两段路程:

curl -X POST -d '{
  "origin":{
    "address": "1600 Amphitheatre Parkway, Mountain View, CA"
  },
  "destination":{
    "address": "24 Willie Mays Plaza, San Francisco, CA 94107"
  },
  "intermediates": [
    { "address": "450 Serra Mall, Stanford, CA 94305, USA"},
  ],
  "travelMode": "DRIVE",
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline,routes.legs.polyline' \
'https://routes.googleapis.com/directions/v2:computeRoutes'

此请求会返回两条路程,每条路线都有唯一的多段线,以及整个路线的多段线:

{
  "routes": [
    {
      "legs": [
        {
          "polyline": {
            "encodedPolyline": "kclcFfqchV?A...?I@G?GAECCCEKICBAFG"
          }
          "steps": [
            {
                "polyline": {
                    "encodedPolyline": "kclcFfqch...YIOKI"
                }
            },
        ...
        },
        {
          "polyline": {
            "encodedPolyline": "ojmcFtethV?K...QOYQOGA?_@MUG[Ga@G"
          }
          "steps": [
            {
                "polyline": {
                    "encodedPolyline": "uypeFbo`jVgJq...PoBiC"
                }
            },
        ...
        }
      ],
      "distanceMeters": 68403,
      "duration": "3759s",
      "polyline": {
          "encodedPolyline": "kclcFfqchV?A?CBKF[Ha...?GAECCCEKICBAFGJEBE"
      }
    }
  ]
}

多段线质量

多段线的质量可以用以下词语描述:

  • 数据点的浮点精度

    点指定为纬度值和经度值,以单精度浮点格式表示。这非常适合较小的值(可以精确地表示),但由于浮点舍入误差,精度会随着值的增加而降低。

    computeRoutes 方法 (REST) 和 ComputeRoutes 中,这由 polylineEncoding 控制。

  • 组成多段线的点数量

    点越多,多段线(尤其是在曲线中)越平滑。

    computeRoutes 方法 (REST) 和 ComputeRoutes 中,这由 polylineQuality 控制。

配置多段线编码类型

使用 polylineEncoding 请求选项来控制多段线类型。polylineEncoding 属性用于控制多段线是否编码为 ENCODED_POLYLINE(默认),意味着将使用编码多段线算法格式,即 GEO_JSON_LINESTRING,意味着将使用 GeoJSON LineString 格式

例如,在请求正文中:

curl -X POST -d '{
  "origin":{
    "address": "1600 Amphitheatre Parkway, Mountain View, CA"
  },
  "destination":{
    "address": "24 Willie Mays Plaza, San Francisco, CA 94107"
  },
  "travelMode": "DRIVE",
  "polylineEncoding": "ENCODED_POLYLINE"
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline,routes.legs.polyline' \
'https://routes.googleapis.com/directions/v2:computeRoutes'

配置多段线质量

polylineQuality 将多段线的质量指定为 HIGH_QUALITYOVERVIEW(默认值)。使用 OVERVIEW 时,多段线由少量的点组成,且请求延迟时间比 HIGH_QUALITY 短。

例如,在请求正文中:

{
  "origin":{
    "location":{
      "latLng":{
        "latitude": 37.419734,
        "longitude": -122.0827784
      }
    }
  },
  "destination":{
    "location":{
      "latLng":{
        "latitude": 37.417670,
        "longitude": -122.079595
      }
    }
  },
  "travelMode": "DRIVE",
  "routingPreference": "TRAFFIC_AWARE",
  "polylineQuality": "HIGH_QUALITY",
  "polylineEncoding": "ENCODED_POLYLINE",
  "departureTime": "2023-10-15T15:01:23.045123456Z",
  ...
}

请求路况感知多段线

以上示例均返回基本的多段线,即没有路况信息的多段线。此外,您还可以要求多段线包含路线和路线的每段路程的路况信息。

路况感知多段线包含有关路线沿途路况的信息。以响应多段给定时间间隔内的速度类别(NORMALSLOWTRAFFIC_JAM)表示路况。间隔由其起始(含)和结束(不含)多段线点的索引定义。

例如,以下响应显示了多段线点 2 和 4 之间的 NORMAL 流量:

{
  "startPolylinePointIndex": 2,
  "endPolylinePointIndex": 4,
  "speed": "NORMAL"
}

如需发出计算路况感知多段线的请求,请在请求中设置以下属性:

  • extraComputations 数组字段设置为 TRAFFIC_ON_POLYLINE 以启用流量计算。

  • travelMode 设置为 DRIVETWO_WHEELER。针对任何其他出行方式的请求都会返回错误。

  • 在请求中指定 TRAFFIC_AWARETRAFFIC_AWARE_OPTIMAL 路由偏好设置。如需了解详情,请参阅配置质量与延迟时间

  • 设置指定返回响应属性的响应字段掩码:

    • 在路线级别,通过在响应字段掩码中添加 routes.travelAdvisory,在响应中返回所有出行信息。如需仅返回路况信息,请指定 routes.travelAdvisory.speedReadingIntervals

    • 在路程级别,通过添加 routes.legs.travelAdvisory,在路线每段路程的响应中返回所有行程信息。如需仅返回路况信息,请指定 routes.legs.travelAdvisory.speedReadingIntervals

curl -X POST -d '{
  "origin":{
    "address": "1600 Amphitheatre Parkway, Mountain View, CA"
  },
  "destination":{
    "address": "24 Willie Mays Plaza, San Francisco, CA 94107"
  },
  "travelMode": "DRIVE",
  "extraComputations": ["TRAFFIC_ON_POLYLINE"],
  "routingPreference": "TRAFFIC_AWARE_OPTIMAL"
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline,routes.legs.polyline,routes.travelAdvisory,routes.legs.travelAdvisory' \
'https://routes.googleapis.com/directions/v2:computeRoutes'

路况感知多段线的响应示例

在响应中,路况数据会在多段线中编码,并包含在 travelAdvisory 字段中,其类型为 RouteLegTravelAdvisory 对象(每条路程)和 RouteTravelAdvisory 对象(路线)。

例如:

{
  "routes": [
    {
      "legs": {
        "polyline": {
          "encodedPolyline": "}boeF~zbjVAg@EmB`GWHlD"
        },
        // Traffic data for the leg.
        "travelAdvisory": {
          "speedReadingIntervals": [
            {
              "endPolylinePointIndex": 1,
              "speed": "NORMAL"
            },
            {
              "startPolylinePointIndex": 1,
              "endPolylinePointIndex": 2,
              "speed": "SLOW"
            },
            {
              "startPolylinePointIndex": 2,
              "endPolylinePointIndex": 4,
              "speed": "NORMAL"
            }
          ] 
        }
      },
      "polyline": {
        "encodedPolyline": "}boeF~zbjVAg@EmB`GWHlD"
      },
      // Traffic data for the route.
      "travelAdvisory": {
        "speedReadingIntervals": [
          {
            "endPolylinePointIndex": 1,
            "speed": "NORMAL"
          },
          {
            "startPolylinePointIndex": 1,
            "endPolylinePointIndex": 2,
            "speed": "SLOW"
          },
          {
            "startPolylinePointIndex": 2,
            "endPolylinePointIndex": 4,
            "speed": "NORMAL"
          }
        ] 
      }
    }
  ]
}

RouteTravelAdvisoryRouteLegTravelAdvisory 都包含一个名为 speedReadingIntervals 的数组字段,其中包含交通速度信息。数组中的每个对象都由一个 SpeedReadingInterval (REST) 或 SpeedReadingInterval (gRPC) 对象表示。

SpeedReadingInterval 对象包含路线时间间隔内的速度读数,例如 NORMALSLOWTRAFFIC_JAM。整个对象数组会涵盖路线的整个多段线,而不会有重叠。指定时间间隔的起点与前一个时间间隔的终点相同。

每个间隔由其 startPolylinePointIndexendPolylinePointIndex 和相应的速度类别描述。请注意,根据 proto3 实践,间隔内缺少起始索引对应于索引 0。

startPolylinePointIndexendPolylinePointIndex 值并不总是连续的。例如:

{
  "startPolylinePointIndex": 2,
  "endPolylinePointIndex": 4,
  "speed": "NORMAL"
}

在本例中,从索引 2 到索引 4 的流量条件没有变化。

使用 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