Get a transit route

A transit route provides navigation instructions using the public transportation options available in the region. Transit options may include buses, subways, and trains, among others. A transit route also usually includes instructions on walking to, from, and between transit stations. Because a transit route typically requires you to travel using more than one mode of travel, how you request the route and some parts of the response are different.

How transit routes differ from other routes

Transit routes, which you request by setting a travelMode of TRANSIT, differ from routes using different travelMode options. You cannot request all of the same objects and options, and the response returns different fields, when compared to other routes.

In the Routes API, steps are consistently one navigation instruction across all types of travel. So each navigation instruction is a step. A transit route response is very similar to routes with other travel modes, with a few key differences:

Request differences Response differences
You cannot specify intermediate waypoints. Includes Transit details.
Cannot get eco-friendly routes Includes metadata for each travel mode that contains the summary of the steps for that travel mode, in the `stepsOverview` (to request this metadata, use the `routes.legs.stepsOverview` field mask).
Cannot specify how and if to include traffic data
Cannot specify route features to avoid
Can only specify transitPreferences for routingPreference. For details, see [TransitPreferences](reference/rest/v2/TransitPreferences).

For more information about the response returned by the Routes API, see Review transit route responses.

To get a transit route

  1. Set your origin and destination.

  2. Set the travel mode to transit:

    travelMode: "TRANSIT"

  3. Add a field mask to get the response fields you want. See Request the transit route fields you need.

  4. If needed, set optional parameters. See Set parameters for a transit route.

Request the transit route fields you need

Request the transit route fields you need using field masks. The following table contains some suggested responses for a transit route, and the field mask to use to request them.

To request this information Use this field mask
All route details routes.*
All transit details routes.legs.steps.transitDetails
Start location for each leg routes.legs.steps.startLocation
End location for each leg routes.legs.steps.endLocation
Route polyline for each step routes.legs.steps.polyline
Type of transit used for each step routes.legs.steps.travelMode
Estimated fare for each step and the route, and Localized estimated fare routes.travel_advisory.transitFare localized routes.localizedValues.transitFare

Note: transitFare is only provided if the API can determine transit fare information for all steps.

Localized text for duration and distance routes.localizedValues

For more information about setting field masks, see Choose fields to return.

Set parameters for a transit route

Here are the relevant parameters for transit routes:

To do this Use this parameter Notes
Select transit mode travelMode: "TRANSIT" Required. Learn more.
Set an arrival or departure time "arrivalTime": "yyyy-mm-ddThh:mm:ssZ"

OR

"departureTime": "yyyy-mm-ddThh:mm:ssZ"
Optional. You can specify either arrival_time or departure_time. If neither time is specified, the departure_time defaults to the current execution time (now). You can set arrival and departure times only within the following time window, with the current execution time (now) as the reference:
  • up to and including 7 days prior to now
  • up to and including 100 days after now
Time is expressed as a timestamp in RFC3339 UTC "Zulu" format. Learn more
Include alternate routes "computeAlternativeRoutes": true Optional. Set to true to have the Routes API compute up to 3 additional routes, when available. Learn more
Specify preferences for the transit type "transitPreferences": { allowedTravelModes: ["BUS","SUBWAY","TRAIN","LIGHT_RAIL","RAIL"]}

Note: If you specify a preferred travel mode, returned routes may still use other transit modes, even only use other transit modes, depending on the efficiency of the route on, and the availability of, the preferred travel.

Optional. Specify preferred transit travel modes. Learn more
Specify preferences for the transit route "transitPreferences": {routingPreference: "LESS_WALKING|FEWER_TRANSFERS"} Optional. Specify transit routing preferences. Learn more

Example: Get a route on transit

The following example gets a route on transit with the following parameters:

  • Specifies preferences for travel on a train and less walking.

  • Requests alternate routes.

  • Provides a field mask that returns the transit details:

curl -X POST -H 'content-type: application/json' -d '{
  "origin": {
    "address": "Humberto Delgado Airport, Portugal"
  },
  "destination": {
    "address": "Basílica of Estrela, Praça da Estrela, 1200-667 Lisboa, Portugal"
  },
  "travelMode": "TRANSIT",
  "computeAlternativeRoutes": true,
  "transitPreferences": {
     routingPreference: "LESS_WALKING",
     allowedTravelModes: ["TRAIN"]
  },
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.legs.steps.transitDetails' \
'https://routes.googleapis.com/directions/v2:computeRoutes'

The response shows route.legs.steps.transitDetails fields for the two routes:

{
  "routes": [
    {
    "legs": [
        {
        "steps": [
          {},
          {},
          {},
          {
          "transitDetails": {
            "stopDetails": {
            "arrivalStop": {
              "name": "Saldanha",
              "location": {
                "latLng": {
                  "latitude": 38.73532,
                  "longitude": -9.14543
                }
              }
            },
            "arrivalTime": "2023-08-26T10:49:42Z",
            "departureStop": {
              "name": "Aeroporto",
              "location": {
                "latLng": {
                  "latitude": 38.769047799999996,
                  "longitude": -9.1284593
                }
              }
            },
            "departureTime": "2023-08-26T10:32:10Z"
              },
              "localizedValues": {
              "arrivalTime": {
                  "time": {
                    "text": "11:49"
                  },
                  "timeZone": "Europe/Lisbon"
              },
              "departureTime": {
                "time": {
                  "text": "11:32"
                },
                "timeZone": "Europe/Lisbon"
              }
            },
              "headsign": "São Sebastião",
              "transitLine": {
                "agencies": [
                  {
                    "name": "Metropolitano de Lisboa, E.P.E.",
                    "phoneNumber": "+351 21 350 0115",
                    "uri": "https://www.metrolisboa.pt/"
                  }
                ],
                "name": "Vermelha",
                "color": "#f23061",
                "nameShort": "Vm",
                "textColor": "#000000",
                "vehicle": {
                  "name": {
                    "text": "Metrô"
                  },
                "type": "SUBWAY",
                "iconUri": "//maps.gstatic.com/mapfiles/transit/iw2/6/subway2.png"
                }
              },
              "stopCount": 11
            }
          },
        {},
        {
          "transitDetails": {
            "stopDetails": {
              "arrivalStop": {
                "name": "Rato",
                "location": {
                  "latLng": {
                    "latitude": 38.7201022,
                    "longitude": -9.1540562
                  }
              }
            },
            "arrivalTime": "2023-08-26T11:01:37Z",
            "departureStop": {
              "name": "Saldanha",
                "location": {
                  "latLng": {
                    "latitude": 38.73527,
                    "longitude": -9.1455200000000012
                  }
                }
              },
              "departureTime": "2023-08-26T10:57:57Z"
            },
            "localizedValues": {
              "arrivalTime": {
                "time": {
                  "text": "12:01"
                },
                "timeZone": "Europe/Lisbon"
              },
              "departureTime": {
                "time": {
                  "text": "11:57"
                },
                "timeZone": "Europe/Lisbon"
              }
            },
            "headsign": "Rato",
            "transitLine": {
              "agencies": [
                {
                "name": "Metropolitano de Lisboa, E.P.E.",
                "phoneNumber": "+351 21 350 0115",
                "uri": "https://www.metrolisboa.pt/"
                }
              ],
              "name": "Amarela",
              "color": "#f2c200",
              "nameShort": "Am",
              "textColor": "#000000",
              "vehicle": {
                "name": {
                  "text": "Metrô"
                },
                "type": "SUBWAY",
                "iconUri": "//maps.gstatic.com/mapfiles/transit/iw2/6/subway2.png"
              }
            },
            "stopCount": 4
          }
        },
        {},
        {},
        {},
        {},
        {},
        {}
        ]
      }
    ]
  }
]
}