A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/maps/documentation/routes/compute_route_matrix below:

Get a route matrix | Routes API

Skip to main content Get a route matrix

Stay organized with collections Save and categorize content based on your preferences.

Use the Routes API to calculate the distance and duration of a route for multiple origins and destinations by calling the computeRouteMatrix method (REST) or the streaming 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.

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:

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 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: 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 method in an HTTP request to compute a route matrix.

HTTP example

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

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: For detailed description of the response, see Understand the compute route matrix 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.

[
    {
        "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 requests, see the examples on Example gRPC request. The Java example on that page calls both the Compute Routes and Compute Route Matrix.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-18 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-18 UTC."],[[["Google Maps Routes API calculates distance and duration for routes with multiple origins and destinations using the `computeRouteMatrix` method."],["Request limits apply to the number of elements (routes between origin-destination pairs), varying based on route type and traffic settings."],["Responses can contain errors for the entire request or individual route elements, providing granular feedback on route calculation success."],["The gRPC method offers stream results, allowing processing of route information as it becomes available, without waiting for all calculations to complete."],["Examples demonstrate how to use the API with HTTP requests and interpret the response, identifying routes using origin and destination indices."]]],["The Routes API calculates route distances and durations for multiple origins and destinations using `computeRouteMatrix`. It supports REST and gRPC methods, with gRPC offering streaming results. Key actions include specifying origins and destinations, setting travel modes (like transit), and handling request limits (e.g., element counts for `TRANSIT` or `TRAFFIC_AWARE_OPTIMAL` routes). Errors can be response-wide or element-specific. The response provides `originIndex`, `destinationIndex`, `duration`, `distanceMeters`, and `status` for each route.\n"]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4