请求路线多段线

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'

配置 Poyline 质量

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 的用户有机会定义速度类别和多段线渲染架构之间的自定义映射逻辑。例如,有时用户可能会决定在地图上用蓝色粗线显示“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