AI-generated Key Takeaways
-
Obtain a matrix of routes using available public transportation like buses, subways, and trains for a specified region.
-
The API allows you to calculate travel distances, durations, and even transit fares between multiple origins and destinations using public transit.
-
Example code demonstrates how to make a request to the API using curl, specifying origins, destinations, and desired fields like transit fare.
-
The response provides a matrix with details for each origin-destination pair, including distance, duration, fare (if applicable), and route existence status.
Get a transit route matrix that uses the public transportation options available in the region. Transit options may include buses, subways, and trains, among others.
For additional details, see Get a route on transit .
Example transit route matrix
The following example computes a route matrix on transit, requesting the transit fare in addition to field masks typical for a route matrix:
curl - X POST - d ' { "origins" : [ { "waypoint" : { "location" : { "latLng" : { "latitude" : 37.420761 , "longitude" : -122.081356 } } } }, { "waypoint" : { "location" : { "latLng" : { "latitude" : 37.403184 , "longitude" : -122.097371 } } } } ], "destinations" : [ { "waypoint" : { "location" : { "latLng" : { "latitude" : 37.420999 , "longitude" : -122.086894 } } } }, { "waypoint" : { "location" : { "latLng" : { "latitude" : 37.383047 , "longitude" : -122.044651 } } } } ], "travelMode" : "TRANSIT" } ' \ - H 'Co ntent - Type : applica t io n /jso n ' - H 'X - Goog - Api - Key : YOUR_API_KEY ' \ - H 'X - Goog - FieldMask : origi n I n dex , des t i nat io n I n dex , dura t io n , dis tan ceMe ters , s tatus , co n di t io n , tra velAdvisory. trans i t Fare' \ 'h tt ps : //routes.googleapis.com/distanceMatrix/v2:computeRouteMatrix'
Transit route matrix response
Here is the response from the transit route matrix request:
[ { "originIndex" : 1 , "destinationIndex" : 1 , "status" : {}, "distanceMeters" : 8709 , "duration" : "2754s" , "travelAdvisory" : { "transitFare" : { "currencyCode" : "USD" , "units" : "2" , "nanos" : 500000000 } }, "condition" : "ROUTE_EXISTS" }, { "originIndex" : 0 , "destinationIndex" : 0 , "status" : {}, "distanceMeters" : 530 , "duration" : "394s" , "travelAdvisory" : { "transitFare" : {} }, "condition" : "ROUTE_EXISTS" }, { "originIndex" : 1 , "destinationIndex" : 0 , "status" : {}, "distanceMeters" : 2934 , "duration" : "740s" , "travelAdvisory" : { "transitFare" : { "currencyCode" : "USD" , "units" : "2" , "nanos" : 500000000 } }, "condition" : "ROUTE_EXISTS" }, { "originIndex" : 0 , "destinationIndex" : 1 , "status" : {}, "distanceMeters" : 9883 , "duration" : "3049s" , "travelAdvisory" : { "transitFare" : { "currencyCode" : "USD" , "units" : "5" } }, "condition" : "ROUTE_EXISTS" } ]

