The traffic preferences you select balance the accuracy of the route details with request performance in one of two ways:
- Traffic aware: Return the most accurate results possible (higher latency).
- Traffic unaware: Return the results as quickly as possible (lowest latency).
When you make a request, weigh whether it is better to return the most accurate results possible, or to return results as quickly as possible. The Routes library provides options that let you control the quality of the response data versus the latency of the response.
Set the traffic level and traffic model
To specify the traffic level, set the
routingPreference
on the ComputeRoutesRequest
. The following list shows the available
routingPreference
options:
TRAFFIC_UNAWARE
(default): Returns results the most quickly, with approximate routing details.TRAFFIC_AWARE
: Returns more accurate results using real-time traffic data, but with higher latency.TRAFFIC_AWARE_OPTIMAL
: Returns the most accurate results possible using real-time traffic data, but with the highest latency.
The TRAFFIC_MODEL
property represents assumptions to use when predicting duration in traffic (
TrafficModel.BEST_GUESS
(default),
TrafficModel.OPTIMISTIC
,
TrafficModel.PESSIMISTIC
).
How to select traffic-aware routing
To create a traffic-aware route request, take these steps:
-
Set the
travelMode
property toDRIVING
. -
Set the
routingPreference
property to one of the following:TRAFFIC_AWARE
TRAFFIC_AWARE_OPTIMAL
NOTE:
TRAFFIC_UNAWARE
is the default setting. -
Set the
trafficModel
property to one of the following: -
Set the
extraComputations
property toTRAFFIC_ON_POLYLINE
. -
Request the
path
,speedPaths
, androuteLabels
fields.
The following example request shows how to request a traffic-aware route that shows traffic data on the polyline:
// Define a traffic aware routes request with polylines. const requestWithTraffic = { origin: '200 King St San Francisco, CA 94107', destination: 'Pier 41, San Francisco, CA 94133', travelMode: 'DRIVING', routingPreference: 'TRAFFIC_AWARE_OPTIMAL', trafficModel: 'optimistic', extraComputations: ['TRAFFIC_ON_POLYLINE'], fields: ['speedPaths'], };
Traffic conditions
Traffic conditions characterize the rate of traffic flow:
- Normal traffic: No congestion with traffic flowing at a normal speed.
- Light to moderate traffic: Increasing congestion, with traffic flowing at a reduced speed.
- Heavy traffic: Severe congestion, with heavily reduced traffic speed.
Traffic unaware
TRAFFIC_UNAWARE
is the default setting. Use this routing preference when you
want responses returned the quickest, and approximate routing details are good enough.
TRAFFIC_UNAWARE
routes are calculated without accounting for current traffic
conditions. This routing preference results in the fastest request response (lowest
latency).
If you choose TRAFFIC_UNAWARE
, the route and duration chosen are based on road
network and average time-independent traffic conditions, not current road conditions.
Consequently, routes may include roads that are temporarily closed. Results for a given
request may vary over time due to changes in the road network, updated average traffic
conditions, and the distributed nature of the service. Results may also vary between
nearly-equivalent routes at any time or frequency.
Here are the responses you'll see:
duration
: Contains the ETA for the route.staticDuration
: The ETA for the route considering only historical traffic information. ForTRAFFIC_UNAWARE
, this contains the same value asduration
.
Traffic aware
Use this routing preference when you want more accurate routing details than
TRAFFIC_UNAWARE
, and yet you don't mind if responses are returned with a
moderate increase in latency.
When you set the TRAFFIC_AWARE
routing preference, the service calculates the
route considering current traffic conditions. As a result, the route and route details more
accurately reflect real-world conditions. Because this increase in data quality comes at the
expense of response latency, performance optimizations are applied to reduce much of the
latency.
Here are the responses you'll see:
duration
: The ETA considering real-time traffic information.staticDuration
: The ETA for the route considering only historical traffic information.
Traffic aware optimal
Use this routing preference when you want results of the highest quality without regard to how long responses take. This routing preference has the longest delay in returning responses (highest latency).
When you set the TRAFFIC_AWARE_OPTIMAL
routing preference, the service
calculates the route considering current traffic conditions, but doesn't apply performance
optimizations. In this mode, the server performs a more exhaustive search of the road network
to find the optimal route.
The TRAFFIC_AWARE_OPTIMAL
routing preference is equivalent to the mode used by
maps.google.com and by the Google Maps mobile app.
When you use this option with computeRouteMatrix
, the number of elements in a
request (number of origins × number of destinations) cannot exceed 25.
Here are the responses you'll see:
duration
: The ETA for the route considering real-time traffic information.staticDuration
: The ETA for the route considering only historical traffic information.
Set departure time (optional)
Use this property only for traffic aware requests where the departure time needs
to be in the future. If you don't set the departureTime
property, it defaults
to the time that you make the request.
Use the departureTime
property along with the TRAFFIC_AWARE
and
TRAFFIC_AWARE_OPTIMAL
options when you want to adjust the way the service
predicts traffic when choosing a route.
TRAFFIC_UNAWARE
: Not recommended because the choice of route and duration are based on the road network and average time-independent traffic conditions.TRAFFIC_AWARE
andTRAFFIC_AWARE_OPTIMAL
: Recommended for departures happening in the near future because these preferences take live traffic conditions into consideration. Live traffic becomes more important and relevant the closer thedepartureTime
is to now. The farther ahead you set the departure time into the future, the more consideration is given to historical traffic conditions in selecting routes.
Get a route token
Route tokens are a web-safe, base64-encoded representation of a route. Use a route token to share a route with the Navigation SDK. The SDK uses this token to reconstruct the route and maintain the original navigation intent, even during a reroute.
Take these steps to get a route token:
- Set the travelMode to
DRIVING
. - Set the
routingPreference
toTRAFFIC_AWARE
orTRAFFIC_AWARE_OPTIMAL
. - Request the
routeToken
field.
The following example request shows how to request a route token:
// Define a traffic aware routes request with a route token. const requestWithRouteToken = { origin: '200 King St San Francisco, CA 94107', destination: 'Pier 41, San Francisco, CA 94133', travelMode: 'DRIVING', routingPreference: 'TRAFFIC_AWARE', fields: ['path', 'speedPaths', 'routeLabels', 'routeToken'], };