The traffic preferences you select balance the accuracy of the route details with request performance. When you make a request, you want to weigh whether it is better to return the most accurate results possible, or to return results as quickly as possible. The Routes API provides options that let you control the quality of the response data vs the latency of the response.
Set the level of traffic data
The Routes API provides RoutingPreference (REST) and RoutingPreference (gRPC) that let you specify routing preferences for calculating routes. These preferences differ in the extent that they factor traffic conditions into the route calculation. Each routing preference produces results that differ to a degree in regard to route quality, estimated ETA, and response latency.
Traffic conditions characterize the rate of traffic flow. For example:
- When there is no congestion, traffic conditions are considered normal, and traffic flows at the regular unimpeded speed.
- Approaching rush-hour, traffic density increases, which causes traffic to slow down, producing light to moderate traffic conditions.
- In bumper-to-bumper traffic, the flow-rate grinds to a halt, producing heavy traffic conditions.
Traffic unaware
When you set the TRAFFIC_UNAWARE
routing preference, routes are calculated
without accounting for current traffic conditions. This routing preference
provides the lowest response latency (responses are returned the fastest).
TRAFFIC_UNAWARE
is the default setting.
In the response:
The ETA is contained in the
duration
response property.The
duration
andstaticDuration
response properties contain the same value.
Use this routing preference when you want responses returned the quickest, and approximate routing details are good enough.
Traffic aware
When you set the TRAFFIC_AWARE
routing preference, routes are calculated
accounting for 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.
In the response:
The ETA taking real-time traffic into consideration is contained in the
duration
response property.The
staticDuration
response property contains the duration of traveling through the route without taking traffic conditions into consideration.
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.
Traffic aware optimal
When you set the TRAFFIC_AWARE_OPTIMAL
routing preference, routes are
calculated accounting for current traffic conditions, but no performance
optimizations are applied. 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 using this option with Compute Route Matrix, the number of elements in a request (number of origins × number of destinations) cannot exceed 100. For more on Compute Route Matrix limits, see Compute a route matrix.
In the response:
The ETA taking real-time traffic into consideration is contained in the
duration
response property.The
staticDuration
response property contains the duration of traveling through the route without taking traffic conditions into consideration.
This routing preference provides the highest response latency (that is, responses return with the longest delay). Use this routing preference when you want results of the highest quality, without regard to how long responses take.
Effect of setting departure time
You optionally use the departureTime
property to set the departure time for a
trip. If you don't set the departureTime
property, it defaults to
the time that you make the request.
For
TRAFFIC_UNAWARE
, you cannot setdepartureTime
because the choice of route and duration are based on road network and average time-independent traffic conditions.For
TRAFFIC_AWARE
andTRAFFIC_AWARE_OPTIMAL
, which take live traffic conditions into consideration, live traffic becomes more important the closer thedepartureTime
is to now. The farther you set the departure time into the future, the more consideration is given to historical traffic conditions.
Example setting routing preference
The following JSON code demonstrates how to set the routing preference in a request message entity body.
{ "origin":{ "location":{ "latLng":{ "latitude":37.419734, "longitude":-122.0827784 } } }, "destination":{ "location":{ "latLng":{ "latitude":37.417670, "longitude":-122.079595 } } }, "travelMode":"DRIVE", "routingPreference":"TRAFFIC_AWARE_OPTIMAL" }
Configure traffic on polylines
The Routes API lets you request information about traffic conditions along a traffic-aware polyline. See Request polylines for more information.