AI-generated Key Takeaways
- 
          The Routes API's Compute Routesmethod can optimize the order of stops (waypoints) for greater efficiency by considering factors like travel time, distance, and turns.
- 
          To enable waypoint optimization, ensure no waypoints are marked as via, avoid usingTRAFFIC_AWARE_OPTIMALrouting preference, and setoptimizeWaypointOrdertotrue.
- 
          The API indexes waypoints based on their order in the request, optimizes the order, and returns the optimized sequence in the optimizedIntermediateWaypointIndexfield.
- 
          Requests with waypoint optimization are billed at a higher rate on the ComputeRoutes-Advanced SKU. 
By default, the Routes API Compute Routes
method calculates a route through multiple stops, called stopover waypoints, in
the order that you provide them.
You can have the Routes API optimize the provided route by rearranging stops in a more efficient order. Waypoint optimization optimizes for travel time but also considers other factors such as distance and number of turns when deciding which route is the most efficient.
To optimize waypoints
- Make sure none of the waypoints in the route have - viaset to- true, for example:- {"address": "Clare,SA", "via": true}. For more information about intermediate waypoints, see Specify intermediate waypoints.
- Make sure the - routing_preferenceis not set to- TRAFFIC_AWARE_OPTIMAL.
- Set - optimize_waypoint_orderto- true. For example:- "optimizeWaypointOrder": "true", 
- Specify the - routes.optimizedIntermediateWaypointIndexfield in the field mask:- REST - -H X-Goog-FieldMask: routes.optimizedIntermediateWaypointIndex - RPC - const (fieldMask = "routes.optimizedIntermediateWaypointIndex") 
Understand how waypoint order is optimized
Here's how the Routes API optimizes the order of waypoints in a route:
- Automatically indexes the waypoints based on the order you provide them in the request, starting with 0. 
- Optimizes the order of the waypoints using the index numbers it assigned to the waypoints in the request. 
- Returns the optimized waypoint order in the - routesobject, in the- waypoint_orderfield, under- routes.optimizedIntermediateWaypointIndex.
Example
This request asks for optimization for a route from Adelaide, South Australia, to each of South Australia's main wine regions, and then returning to Adelaide.
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'
The Routes API indexes the intermediate waypoints provided in the request, starting at 0. For example:
0 {"address": "Barossa+Valley,SA"}, 1 {"address": "Clare,SA"}, 2 {"address": "Coonawarrav,SA"}, 3 {"address": "McLaren+Vale,SA"}
Using the index numbers for the four waypoints provided in the request, the service then returns the optimized order:
"optimizedIntermediateWaypointIndex": [ 3, 2, 0, 1 ]