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 travel mode 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
stepsOverview
field mask). |
Cannot specify how and if to include traffic data | |
Cannot specify route features to avoid | |
Can only specify transitPreferences . For details, see
TransitPreference . |
To get a transit route
- Set your origin and destination.
- Set the
travelMode
toTRANSIT
. - Add the appropriate field masks to get the response you need. See Request the transit route fields you need.
- 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. | * |
Transit details for route legs. | legs |
Estimated fare for each step of the route, and localized estimated fare. | travelAdvisory |
Localized text for duration and distance | localizedValues |
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: new Date(yyyy-mm-ddThh:mm:ssZ)
OR departureTime: new Date(yyyy-mm-ddThh:mm:ssZ) |
Optional. You can specify either arrival time or departure time. If
neither time is specified, the departureTime 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:
Date object.
Note: Transit schedules change frequently, available transit trips may change over time and there is no guarantee to provide consistent results for predictions far in advance. |
Include alternate routes | computeAlternativeRoutes: true |
Optional. Set to true to have computeRoutes compute up to 3
additional routes, when available.
Learn more |
Specify preferences for the transit type | transitPreference: {allowedTransitModes: ["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 | transitPreference: {routingPreference: "LESS_WALKING|FEWER_TRANSFERS"} |
Optional. Specify transit routing preferences. Learn more |
Example request
The following example shows how to request a transit route:
const request = { origin: 'Hunters Point San Francisco, CA 94124', destination: '201 Marine Dr, San Francisco, CA 94129', travelMode: 'TRANSIT', departureTime: new Date(), transitPreference: { allowedTransitModes: ['BUS', 'SUBWAY', 'TRAIN', 'LIGHT_RAIL', 'RAIL'], routingPreference: 'FEWER_TRANSFERS', // Alternatively, 'LESS_WALKING'. }, fields: ['path', 'legs', 'travelAdvisory', 'localizedValues'], };