**European Economic Area (EEA) developers**

> [!NOTE]
> 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](https://developers.google.com/maps/comms/eea/faq.md.txt).

By default, the Routes library `computeRoutes` 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

1. Make sure none of the waypoints in the route have `via` set to `true`.
2. Make sure the `routingPreference` is not set to `TRAFFIC_AWARE_OPTIMAL`.
3. Set `optimizeWaypointOrder` to `true`.
4. Specify the `optimizedIntermediateWaypointIndices` field in the field mask.

## Understand how waypoint order is optimized

Here's how the Routes API optimizes the order of waypoints in a route:

1. Automatically indexes the waypoints based on the order you provide them in the request, starting with 0.
2. Optimizes the order of the waypoints using the index numbers it assigned to the waypoints in the request.
3. 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.

```javascript
const request = {
  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 includes `optimizedIntermediateWaypointIndices`.

```json
  Response:
 [
  {
    "optimizedIntermediateWaypointIndices": [
      3, // McLaren+Vale, SA
      2, // Coonawarra, SA
      0, // Barossa+Valley, SA
      1  // Clare, SA
    ],
    ...
  
```