Optimize the order of stops on your route Stay organized with collections Save and categorize content based on your preferences.
By default, the Routes librarycomputeRoutes 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 totrue. - Make sure the
routingPreferenceis not set toTRAFFIC_AWARE_OPTIMAL. - Set
optimizeWaypointOrdertotrue. - Specify the
optimizedIntermediateWaypointIndicesfield in the field mask.
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 order of the waypoints in the response under
optimizedIntermediateWaypointIndices.
Example request
The following example shows how to request optimized waypoints in a route from Adelaide, South Australia, to each of South Australia's main wine regions, and then returning to Adelaide.
constrequest={origin:'Adelaide, SA',destination:'Adelaide, SA',intermediates:[{location:"Barossa+Valley,SA"},{location:"Clare,SA"},{location:"Coonawarra,SA"},{location:"McLaren+Vale,SA"},],travelMode:'DRIVING',optimizeWaypointOrder:true,fields:['path','optimizedIntermediateWaypointIndices'],};
Example response
The response includesoptimizedIntermediateWaypointIndices.Response:[{"optimizedIntermediateWaypointIndices":[3,// McLaren+Vale, SA2,// Coonawarra, SA0,// Barossa+Valley, SA1// Clare, SA],...
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-10-31 UTC.