**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).

Use the Routes API to calculate the distance and duration of a route for
multiple origins and destinations by calling the
[computeRouteMatrix](https://developers.google.com/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix.md.txt) method
(REST) or the streaming
[ComputeRouteMatrix](https://developers.google.com/maps/documentation/routes/reference/rpc/google.maps.routing.v2.md.txt#google.maps.routing.v2.Routes.ComputeRouteMatrix)
method (gRPC).

Given a list of origins and destinations, the method
calculates the distance and duration of a route starting at each origin
and ending at each destination.

## Get a transit route matrix

You can also compute a route matrix on transit. For an
example, see [Get a route matrix on transit](https://developers.google.com/maps/documentation/routes/transit-rm.md.txt).

## Request limits

The Compute Route Matrix methods enforce the following request
limits for waypoints using address or placeIDs, and for elements. Elements are
the routes between each origin and destination in a route matrix, so the number
of elements is the number of origins *times* the number of destinations. For
example, if you have 10 origins and 10 destinations, you have 100 elements:

- **The number of elements** cannot exceed 625 for routes
  that are not `TRANSIT` routes.

- **If you specify a `TRANSIT` route**, the number of elements cannot exceed
  100.

- **If you specify `TRAFFIC_AWARE_OPTIMAL`** , the number of elements cannot
  exceed 100. For more information about `TRAFFIC_AWARE_OPTIMAL`, see
  [Specify how and if to include traffic data](https://developers.google.com/maps/documentation/routes/config_trade_offs.md.txt).

- **If you specify origins or destinations using address or place ID**,
  you specify up to 50 total this way.

## Response errors

One feature of the Compute Route Matrix methods is that errors can
be returned either for the entire response or for individual response elements.
For example, the entire response contains an error if the request is malformed
(for example, it has zero origins).

However, if an error applies to a subset of
elements in the response (for example, a route cannot be computed for one
combination of origin and destination), then only the elements affected by the
error return an error code.

## gRPC Stream results

The [ComputeRouteMatrix](https://developers.google.com/maps/documentation/routes/reference/rpc/google.maps.routing.v2.md.txt#google.maps.routing.v2.Routes.ComputeRouteMatrix)
gRPC method takes in a list of origins and destinations and returns a stream
containing route information for each combination of origin and destination.
Because the results are returned as a stream, you don't have to wait until all
possible route combinations are calculated before you can start to process the
results.

> [!NOTE]
> **Note:** Stream results are not supported by the REST API.

The elements returned by the stream are not guaranteed to be returned in any
order. Therefore, each response element contains an `origin_index` and a
`destination_index`. For the origins and destinations specified by the request,
the route origin is equivalent to `origins[origin_index]` for a given element
and the route destination is equivalent to `destinations[destination_index]`.
These arrays are zero-indexed. It is important to store the origin and
destination list orders.

## Compute a Route Matrix Examples

Use the [computeRouteMatrix](https://developers.google.com/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix.md.txt)
method in an HTTP request to compute a route matrix.

### HTTP example

The following example shows a `computeRouteMatrix` HTTP request. In this example
you:

- Specify an array of two origin and two destination waypoints. The method
  calculates a route from each origin to each destination so the response
  contains four routes.

  In the array, the first element is at an index of 0, the second is index 1,
  and so forth.
- Include a response field mask to specify which fields of the
  [response](https://developers.google.com/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix.md.txt#response-body)
  (REST) or
  [ComputeRoutesResponse](https://developers.google.com/maps/documentation/routes/reference/rpc/google.maps.routing.v2.md.txt#computeroutesresponse)
  (gRPC) to return. In this example, configure the request to return
  `originIndex`, `destinationIndex`, `duration`, `distanceMeters`, `status`,
  and `condition` for each route. For more information, see
  [Choose fields to return](https://developers.google.com/maps/documentation/routes/choose_fields.md.txt).

```json
curl -X POST -d '{
  "origins": [
    {
      "waypoint": {
        "location": {
          "latLng": {
            "latitude": 37.420761,
            "longitude": -122.081356
          }
        }
      },
      "routeModifiers": { "avoid_ferries": true}
    },
    {
      "waypoint": {
        "location": {
          "latLng": {
            "latitude": 37.403184,
            "longitude": -122.097371
          }
        }
      },
      "routeModifiers": { "avoid_ferries": true}
    }
  ],
  "destinations": [
    {
      "waypoint": {
        "location": {
          "latLng": {
            "latitude": 37.420999,
            "longitude": -122.086894
          }
        }
      }
    },
    {
      "waypoint": {
        "location": {
          "latLng": {
            "latitude": 37.383047,
            "longitude": -122.044651
          }
        }
      }
    }
  ],
  "travelMode": "DRIVE",
  "routingPreference": "TRAFFIC_AWARE"
}' \
-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: originIndex,destinationIndex,duration,distanceMeters,status,condition' \
'https://routes.googleapis.com/distanceMatrix/v2:computeRouteMatrix'
```

The response contains the four possible routes for the combination of all origin
and destination waypoints.

> [!NOTE]
> **Note:** For detailed description of the response, see [Understand the compute
> route matrix response](https://developers.google.com/maps/documentation/routes/understand-rm-response).

Identify each route in the response by using the `originIndex` and
`destinationIndex` response fields. For example, an `originIndex` of 1 in the
response corresponds to a route calculated from the waypoint at index 1 of the
`origins` array in the request.

```json
[
    {
        "originIndex": 0,
        "destinationIndex": 0,
        "status": {},
        "distanceMeters": 822,
        "duration": "160s",
        "condition": "ROUTE_EXISTS"
    },
    {
        "originIndex": 1,
        "destinationIndex": 0,
        "status": {},
        "distanceMeters": 2919,
        "duration": "361s",
        "condition": "ROUTE_EXISTS"
    },
    {
        "originIndex": 1,
        "destinationIndex": 1,
        "status": {},
        "distanceMeters": 5598,
        "duration": "402s",
        "condition": "ROUTE_EXISTS"
    },
    {
        "originIndex": 0,
        "destinationIndex": 1,
        "status": {},
        "distanceMeters": 7259,
        "duration": "712s",
        "condition": "ROUTE_EXISTS"
    }
]
```

### gRPC examples

For example [gRPC](https://www.grpc.io/) requests, see the examples on
[Example gRPC request](https://developers.google.com/maps/documentation/routes/compute_route_directions.md.txt#grpc-distance-example). The Java
example on that page calls both the Compute Routes and
Compute Route Matrix.