AI-generated Key Takeaways
-
To get a route token from the Routes API, you must set
travelModetoDRIVEandroutingPreferenceto eitherTRAFFIC_AWAREorTRAFFIC_AWARE_OPTIMAL. -
Ensure that none of your route waypoints are via waypoints when requesting a route token.
-
Use the
routes.routeTokenfield mask in your request to ensure that a route token is returned. -
The route generated with a token may vary slightly from the planned route due to dynamic road conditions, and it's recommended to use the token soon after generation.
-
The route token can be used in the Navigation SDK for your planned route.
To request that the Compute Routes returns route tokens for generated routes, follow these steps:
Set the following parameters required to return a route token:
- Set
travelModetoDRIVE. - Set
routingPreferencetoTRAFFIC_AWAREorTRAFFIC_AWARE_OPTIMAL.
- Set
Check that none of your route waypoints are via waypoints.
Specify the
routes.routeTokenfield mask to return a route token:X-Goog-FieldMask: routes.routeToken
You can use the route token for your planned route in the Navigation SDK. For more details, see:
Route token example
Here's an example curl request body for a single origin, single-destination route. It uses field masks to request a route token, along with the route duration, distance, and route polyline:
curl -X POST -d
{"origin":{
"location": {
"latLng":{
"latitude": -37.8167,
"longitude": 144.9619
}
}
},
"destination":{
"location": {
"latLng":{
"latitude":-37.8155,
"longitude": 144.9663
}
}
},
"routingPreference":"TRAFFIC_AWARE",
"travelMode":"DRIVE"
}
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H X-Goog-FieldMask: routes.routeToken,routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline
'https://routes.googleapis.com/directions/v2:computeRoutes'
For more details, see the Compute Routes API reference.