默认情况下,Routes API Compute Routes
方法计算经过多个停靠站的路线(称为停靠航点),
您提供给它们的顺序。
您可以让 Routes API 按照更高效的顺序重新排列经停点,从而优化所提供的路线。航点优化针对旅行进行优化 同时还会考虑距离和转弯次数等其他因素 确定哪条路线最高效。
优化航点
确保路线中的所有航点都没有将
via
设置为true
, 示例:{"address": "Clare,SA", "via": true}
。如需详细了解 中间路标,请参见 指定中间航点。确保
routing_preference
未设置为TRAFFIC_AWARE_OPTIMAL
。将
optimize_waypoint_order
设置为true
。例如:"optimizeWaypointOrder": "true",
在字段中指定
routes.optimizedIntermediateWaypointIndex
字段 遮盖:REST
-H X-Goog-FieldMask: routes.optimizedIntermediateWaypointIndex
RPC
const (fieldMask = "routes.optimizedIntermediateWaypointIndex")
了解如何优化航点顺序
下面介绍了 Routes API 如何优化路线中航点的顺序:
根据您提供的顺序自动为航点编制索引 开头,从 0 开始。
使用索引编号优化路标的顺序 。
在
routes
对象(位于routes.optimizedIntermediateWaypointIndex
下的waypoint_order
字段。
示例
此请求要求优化从南澳大利亚州阿德莱德前往南澳大利亚州各主要葡萄酒产区,然后返回阿德莱德的路线。
curl -X POST -H 'content-type: application/json' -d ' { "origin": { "address": "Adelaide,SA" }, "destination": { "address": "Adelaide,SA" }, "intermediates": [ {"address": "Barossa+Valley,SA"}, {"address": "Clare,SA"}, {"address": "Coonawarra,SA"}, {"address": "McLaren+Vale,SA"} ], "travelMode": "DRIVE", "optimizeWaypointOrder": "true" }' \ -H 'Content-Type: application/json' \ -H 'X-Goog-Api-Key: YOUR_API_KEY' \ -H 'X-Goog-FieldMask: routes,geocodingResults.intermediates.intermediateWaypointRequestIndex' \ 'https://routes.googleapis.com/directions/v2:computeRoutes'
Routes API 会将 请求(从 0 开始)。例如:
0 {"address": "Barossa+Valley,SA"}, 1 {"address": "Clare,SA"}, 2 {"address": "Coonawarrav,SA"}, 3 {"address": "McLaren+Vale,SA"}
然后,该服务使用请求中提供的四个航点的索引编号返回优化后的顺序:
"optimizedIntermediateWaypointIndex": [ 3, 2, 0, 1 ]