多段线编码是一种有损压缩算法,可让您将一系列坐标(例如路线)存储为单个字符串。编码过程使用 base64 编码方案将二进制值转换为一系列 ASCII 字符的字符代码。有关编码过程的完整说明,请参阅编码多段线算法格式。
computeRoutes 方法 (REST) 和 ComputeRoutes 方法 (gRPC) 均将在响应中返回由多段线表示的路由。这些 API 会返回两种类型的多段线:
基本多段线(默认)表示路线,但多段线中未嵌入路况信息。返回基本多段线的请求将按路线基本费率计费。详细了解 Routes API 的结算。
路况感知多段线,包含路线沿途路况信息。路况信息用适用于多段线的给定时间间隔的速度类别(
NORMAL
、SLOW
、TRAFFIC_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" } } ] }
配置多段线编码类型
您可以使用 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'
请求可感知路况的多段线
上面显示的示例均返回基本多段线,这意味着没有路况信息的多段线。此外,您还可以请求多段线包含路线以及每一段路程的路况信息。
路况感知型多段线包含路线沿途路况信息。路况信息以速度类别中给定速度区间的指定速度类别(NORMAL
、SLOW
、TRAFFIC_JAM
)表示。这些时间间隔按照起点(含边界)和终点(不含)多段线的索引定义。
例如,以下响应显示了多段线 2 和 4 之间的 NORMAL
流量:
{ "startPolylinePointIndex": 2, "endPolylinePointIndex": 4, "speed": "NORMAL" }
要请求计算路况感知型多段线,请在请求中设置以下属性:
将
extraComputations
数组字段设置为TRAFFIC_ON_POLYLINE
即可启用流量计算。将
travelMode
设置为DRIVE
或TWO_WHEELER
。对任何其他出行方式的请求会返回错误。在请求中指定
TRAFFIC_AWARE
或TRAFFIC_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" } ] } } ] }
RouteTravelAdvisory
和 RouteLegTravelAdvisory
都包含一个名为 speedReadingIntervals
的数组字段,其中包含流量速度信息。数组中的每个对象均由一个 SpeedReadingInterval (REST) 或 SpeedReadingInterval (gRPC) 对象表示。
SpeedReadingInterval
对象包含路由间隔(例如 NORMAL
、SLOW
或 TRAFFIC_JAM
)的速度读数。整个对象数组会涵盖路线的整个多段线,不会重叠。指定间隔的起点与前一个时间段的端点相同。
每个时间间隔都会通过其 startPolylinePointIndex
、endPolylinePointIndex
和相应的速度类别进行说明。请注意,根据 proto3 做法,间隔内缺少起始索引对应于索引 0。
startPolylinePointIndex
和 endPolylinePointIndex
值并非始终是连续的。例如:
{ "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