computeRoutes 方法会返回以折线表示的路线作为响应的一部分。您可以请求两种类型的折线:
- 基本多段线(默认),表示路线,但多段线中未嵌入路况信息。返回基本折线的请求按 Routes Basic 费率结算。 详细了解 Routes API 的结算方式。
- 可感知路况的多段线,包含有关路线沿途路况的信息。路况以速度类别(
NORMAL、SLOW、TRAFFIC_JAM)表示,适用于多段线的指定间隔。请求使用考虑路况的多段线的费用按 Routes Preferred 费率计算。 详细了解 Routes API 的结算方式。 - 多模态多段线,包含公交详情以及路况信息。多模态折线的请求按“首选路线”费率结算。 详细了解 Routes API 的结算方式。
基本折线(默认)
多段线由 Polyline 对象表示;路径是 LatLngAltitude 坐标的数组。如需返回基本折线,请调用 computeRoutes 方法并将 fields 属性设置为 path,然后对路线实例调用 createPolylines 方法以获取 Polyline 对象。
以下示例展示了如何请求基本折线:
// Define a basic routes request. const requestWithBasicPolyline = { origin: '155 Steuart St, San Francisco, CA 94105', destination: '450 Powell St, San Francisco, CA 94102', travelMode: 'WALKING', fields: ['path'], // Request path field to get a polyline. };
考虑路况的多段线
如需请求包含路况信息的折线,请在请求中添加以下属性:
- 将
travelMode属性设置为DRIVING。 - 将
routingPreference属性设置为TRAFFIC_AWARE。 - 将
extraComputations属性设置为TRAFFIC_ON_POLYLINE。 - 指定
path、speedPaths和routeLabels字段。
以下示例展示了如何请求一条考虑了路况的多段线:
// Define a traffic aware routes request. const requestWithTraffic = { origin: '200 King St San Francisco, CA 94107', destination: 'Pier 41, San Francisco, CA 94133', travelMode: 'DRIVING', routingPreference: 'TRAFFIC_AWARE', extraComputations: ['TRAFFIC_ON_POLYLINE'], fields: ['speedPaths'], };
在地图上显示多段线
如需在地图上显示多段线,请对路线对象调用 createPolylines,然后使用 setMap 方法将多段线的地图设置为地图对象。地图对象用于在地图上显示多段线。
以下示例展示了如何在地图上显示折线:
// Call createPolylines to create polylines for the first route. mapPolylines = routes[0].createPolylines(); // Add polylines to the map. mapPolylines.forEach((polyline) => polyline.setMap(map));