优化路线上的经停点的顺序
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
默认情况下,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.optimizedIntermediateWaypointIndex
下的 waypoint_order
字段中,返回 routes
对象中的优化航点顺序。
示例
此请求要求优化从南澳大利亚州阿德莱德到南澳大利亚州每个主要葡萄酒产区的路线,然后返回阿德莱德。
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
]
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-22。
[null,null,["最后更新时间 (UTC):2025-07-22。"],[[["\u003cp\u003eThe Routes API's \u003ccode\u003eCompute Routes\u003c/code\u003e method can optimize the order of stops (waypoints) for greater efficiency by considering factors like travel time, distance, and turns.\u003c/p\u003e\n"],["\u003cp\u003eTo enable waypoint optimization, ensure no waypoints are marked as \u003ccode\u003evia\u003c/code\u003e, avoid using \u003ccode\u003eTRAFFIC_AWARE_OPTIMAL\u003c/code\u003e routing preference, and set \u003ccode\u003eoptimizeWaypointOrder\u003c/code\u003e to \u003ccode\u003etrue\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe API indexes waypoints based on their order in the request, optimizes the order, and returns the optimized sequence in the \u003ccode\u003eoptimizedIntermediateWaypointIndex\u003c/code\u003e field.\u003c/p\u003e\n"],["\u003cp\u003eRequests with waypoint optimization are billed at a higher rate on the ComputeRoutes-Advanced SKU.\u003c/p\u003e\n"]]],["The Routes API can optimize the order of stopover waypoints to create a more efficient route. To enable this, set `optimize_waypoint_order` to `true`, ensure no waypoints have `via: true` and the `routing_preference` is not `TRAFFIC_AWARE_OPTIMAL`. The API indexes waypoints, optimizes their order based on factors like travel time and distance, and returns the optimized order in `routes.optimizedIntermediateWaypointIndex`. This optimization is billed at a higher rate.\n"],null,["# Optimize the order of stops on your route\n\n**European Economic Area (EEA) developers** If your billing address is in the European Economic Area, effective on 8 July 2025, the [Google Maps Platform EEA Terms of Service](https://cloud.google.com/terms/maps-platform/eea) will apply to your use of the Services. Functionality varies by region. [Learn more](/maps/comms/eea/faq).\n\nBy default, the Routes API `Compute Routes`\nmethod calculates a route through multiple stops, called stopover waypoints, in\nthe order that you provide them.\n\nYou can have the Routes API optimize the provided route by rearranging\nstops in a more efficient order. Waypoint optimization optimizes for travel\ntime but also considers other factors such as distance and number of turns when\ndeciding which route is the most efficient.\n\nTo optimize waypoints\n---------------------\n\n1. Make sure none of the waypoints in the route have `via` set to `true`, for\n example: `{\"address\": \"Clare,SA\", \"via\": true}`. For more information about\n intermediate waypoints, see\n [Specify intermediate waypoints](/maps/documentation/routes/intermed_waypoints).\n\n2. Make sure the `routing_preference` is not set to `TRAFFIC_AWARE_OPTIMAL`.\n\n3. Set `optimize_waypoint_order` to `true`. For example:\n\n ```json\n \"optimizeWaypointOrder\": \"true\",\n ```\n4. Specify the `routes.optimizedIntermediateWaypointIndex` field in the field\n mask:\n\n **REST** \n\n ```json\n -H X-Goog-FieldMask: routes.optimizedIntermediateWaypointIndex\n ```\n\n **RPC** \n\n ```gdscript\n const (fieldMask = \"routes.optimizedIntermediateWaypointIndex\")\n ```\n\n### Understand how waypoint order is optimized\n\nHere's how the Routes API optimizes the order of waypoints in a route:\n\n1. Automatically indexes the waypoints based on the order you provide\n them in the request, starting with 0.\n\n2. Optimizes the order of the waypoints using the index numbers it\n assigned to the waypoints in the request.\n\n3. Returns the optimized waypoint order in the `routes` object, in the\n `waypoint_order` field, under `routes.optimizedIntermediateWaypointIndex`.\n\n### Example\n\nThis request asks for optimization for a route from Adelaide, South Australia,\nto each of South Australia's main wine regions, and then returning to Adelaide. \n\n```json\ncurl -X POST -H 'content-type: application/json' -d ' {\n \"origin\": {\n \"address\": \"Adelaide,SA\"\n },\n \"destination\": {\n \"address\": \"Adelaide,SA\"\n },\n \"intermediates\": [\n {\"address\": \"Barossa+Valley,SA\"},\n {\"address\": \"Clare,SA\"},\n {\"address\": \"Coonawarra,SA\"},\n {\"address\": \"McLaren+Vale,SA\"}\n ],\n \"travelMode\": \"DRIVE\",\n \"optimizeWaypointOrder\": \"true\"\n }' \\\n-H 'Content-Type: application/json' \\\n-H 'X-Goog-Api-Key: YOUR_API_KEY' \\\n-H 'X-Goog-FieldMask: routes,geocodingResults.intermediates.intermediateWaypointRequestIndex' \\\n'https://routes.googleapis.com/directions/v2:computeRoutes'\n```\n\nThe Routes API indexes the intermediate waypoints provided in the\nrequest, starting at 0. For example: \n\n```json\n0 {\"address\": \"Barossa+Valley,SA\"},\n1 {\"address\": \"Clare,SA\"},\n2 {\"address\": \"Coonawarrav,SA\"},\n3 {\"address\": \"McLaren+Vale,SA\"}\n```\n\nUsing the index numbers for the four waypoints provided in the request, the\nservice then returns the optimized order: \n\n```json\n\"optimizedIntermediateWaypointIndex\": [\n 3,\n 2,\n 0,\n 1\n ]\n```\n| **Caution:** Requests using waypoint optimization are billed at a higher rate on the SKU for Compute Routes Pro. For more details, see [SKU: Routes: Compute Routes Pro](/maps/documentation/routes/usage-and-billing#compute-routes-pro-sku)."]]