Method: computeRouteMatrix

Takes in a list of origins and destinations and returns a stream containing route information for each combination of origin and destination.

NOTE: This method requires that you specify a response field mask in the input. You can provide the response field mask by using the URL parameter $fields or fields, or by using the HTTP/gRPC header X-Goog-FieldMask (see the available URL parameters and headers. The value is a comma separated list of field paths. See this detailed documentation about how to construct the field paths.

For example, in this method:

  • Field mask of all available fields (for manual inspection): X-Goog-FieldMask: *
  • Field mask of route durations, distances, element status, condition, and element indices (an example production setup): X-Goog-FieldMask: originIndex,destinationIndex,status,condition,distanceMeters,duration

It is critical that you include status in your field mask as otherwise all messages will appear to be OK. Google discourages the use of the wildcard (*) response field mask, because:

  • Selecting only the fields that you need helps our server save computation cycles, allowing us to return the result to you with a lower latency.
  • Selecting only the fields that you need in your production job ensures stable latency performance. We might add more response fields in the future, and those new fields might require extra computation time. If you select all fields, or if you select all fields at the top level, then you might experience performance degradation because any new field we add will be automatically included in the response.
  • Selecting only the fields that you need results in a smaller response size, and thus higher network throughput.

HTTP request

POST https://routes.googleapis.com/distanceMatrix/v2:computeRouteMatrix

The URL uses gRPC Transcoding syntax.

Request body

The request body contains data with the following structure:

JSON representation
{
  "origins": [
    {
      object (RouteMatrixOrigin)
    }
  ],
  "destinations": [
    {
      object (RouteMatrixDestination)
    }
  ],
  "travelMode": enum (RouteTravelMode),
  "routingPreference": enum (RoutingPreference),
  "departureTime": string,
  "languageCode": string,
  "regionCode": string,
  "extraComputations": [
    enum (ExtraComputation)
  ]
}
Fields
origins[]

object (RouteMatrixOrigin)

Required. Array of origins, which determines the rows of the response matrix. Several size restrictions apply to the cardinality of origins and destinations:

  • The number of elements (origins × destinations) must be no greater than 625 in any case.
  • The number of elements (origins × destinations) must be no greater than 100 if routingPreference is set to TRAFFIC_AWARE_OPTIMAL.
  • The number of waypoints (origins + destinations) specified as placeId must be no greater than 50.
destinations[]

object (RouteMatrixDestination)

Required. Array of destinations, which determines the columns of the response matrix.

travelMode

enum (RouteTravelMode)

Optional. Specifies the mode of transportation.

routingPreference

enum (RoutingPreference)

Optional. Specifies how to compute the route. The server attempts to use the selected routing preference to compute the route. If the routing preference results in an error or an extra long latency, an error is returned. You can specify this option only when the travelMode is DRIVE or TWO_WHEELER, otherwise the request fails.

departureTime

string (Timestamp format)

Optional. The departure time. If you don't set this value, this defaults to the time that you made the request. If you set this value to a time that has already occurred, the request fails.

A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".

languageCode

string

Optional. The BCP-47 language code, such as "en-US" or "sr-Latn". For more information, see http://www.unicode.org/reports/tr35/#Unicode_locale_identifier. See Language Support for the list of supported languages. When you don't provide this value, the display language is inferred from the location of the first origin.

regionCode

string

Optional. The region code, specified as a ccTLD ("top-level domain") two-character value. For more information see https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains#Country_code_top-level_domains

extraComputations[]

enum (ExtraComputation)

Optional. A list of extra computations which may be used to complete the request. Note: These extra computations may return extra fields on the response. These extra fields must also be specified in the field mask to be returned in the response.

Response body

If successful, the response body contains data with the following structure:

Encapsulates route information computed for an origin/destination pair in the v2.computeRouteMatrix API. This proto can be streamed to the client.

JSON representation
{
  "status": {
    object (Status)
  },
  "condition": enum (RouteMatrixElementCondition),
  "distanceMeters": integer,
  "duration": string,
  "staticDuration": string,
  "travelAdvisory": {
    object (RouteTravelAdvisory)
  },
  "fallbackInfo": {
    object (FallbackInfo)
  },
  "originIndex": integer,
  "destinationIndex": integer
}
Fields
status

object (Status)

Error status code for this element.

condition

enum (RouteMatrixElementCondition)

Indicates whether the route was found or not. Independent of status.

distanceMeters

integer

The travel distance of the route, in meters.

duration

string (Duration format)

The length of time needed to navigate the route. If you set the routingPreference to TRAFFIC_UNAWARE, then this value is the same as staticDuration. If you set the routingPreference to either TRAFFIC_AWARE or TRAFFIC_AWARE_OPTIMAL, then this value is calculated taking traffic conditions into account.

A duration in seconds with up to nine fractional digits, ending with 's'. Example: "3.5s".

staticDuration

string (Duration format)

The duration of traveling through the route without taking traffic conditions into consideration.

A duration in seconds with up to nine fractional digits, ending with 's'. Example: "3.5s".

travelAdvisory

object (RouteTravelAdvisory)

Additional information about the route. For example: restriction information and toll information

fallbackInfo

object (FallbackInfo)

In some cases when the server is not able to compute the route with the given preferences for this particular origin/destination pair, it may fall back to using a different mode of computation. When fallback mode is used, this field contains detailed information about the fallback response. Otherwise this field is unset.

originIndex

integer

Zero-based index of the origin in the request.

destinationIndex

integer

Zero-based index of the destination in the request.

RouteMatrixOrigin

A single origin for ComputeRouteMatrixRequest

JSON representation
{
  "waypoint": {
    object (Waypoint)
  },
  "routeModifiers": {
    object (RouteModifiers)
  }
}
Fields
waypoint

object (Waypoint)

Required. Origin waypoint

routeModifiers

object (RouteModifiers)

Optional. Modifiers for every route that takes this as the origin

RouteMatrixDestination

A single destination for ComputeRouteMatrixRequest

JSON representation
{
  "waypoint": {
    object (Waypoint)
  }
}
Fields
waypoint

object (Waypoint)

Required. Destination waypoint

ExtraComputation

Extra computations to perform while completing the request.

Enums
EXTRA_COMPUTATION_UNSPECIFIED Not used. Requests containing this value will fail.
TOLLS Toll information for the matrix element(s).

RouteMatrixElementCondition

The condition of the route being returned.

Enums
ROUTE_MATRIX_ELEMENT_CONDITION_UNSPECIFIED Only used when the status of the element is not OK.
ROUTE_EXISTS A route was found, and the corresponding information was filled out for the element.
ROUTE_NOT_FOUND No route could be found. Fields containing route information, such as distanceMeters or duration, will not be filled out in the element.