AI-generated Key Takeaways
-
The Routes API offers different traffic models (
BEST_GUESS,PESSIMISTIC,OPTIMISTIC) to predict travel times based on various traffic conditions. -
You can specify the desired traffic model when calculating routes, with
BEST_GUESSbeing the default, providing the most useful predictions for most use cases. -
PESSIMISTICmodels travel time based on bad traffic days, likely resulting in a longer estimated duration, whileOPTIMISTICuses good traffic days, potentially resulting in a shorter duration. -
Live traffic information is integrated into the
BEST_GUESSmodel, making it potentially shorter thanOPTIMISTICor longer thanPESSIMISTIC. -
To use traffic models, include the
trafficModelparameter in your request and specifyroutes.durationin the field mask.
Traffic models use specific algorithms and factors to predict how traffic
conditions impact total travel time for a given route. The Routes API
offers different traffic model types to prioritize different factors used in
calculating duration in traffic. You can specify the traffic model type you'd
like your routes to use when calculating the duration for your routes or route
matrix. When you set the traffic model type, the value returned in the
duration field varies depending on the traffic model you choose.
Both the Routes API Compute Routes and
Compute Route Matrix methods support traffic model types.
By default, both methods use the BEST_GUESS traffic model type.
To specify a traffic type
Set your origin and destination.
If your departure time is in the future, include the time using the
departure_timeparameter. If you don't specify a departure time, it defaults to now.Set the
routingPreferenceparameter toTRAFFIC_AWARE_OPTIMAL. Setting theroutingPreferencetoTRAFFIC_UNAWAREorTRAFFIC_AWAREis incompatible with thetrafficModelparameter.Select the traffic model to use with the
trafficModelparameter and one of the following types:BEST_GUESS(default) to request that the returnedduration_in_trafficis the best estimate of travel time given what is known about both historical traffic conditions and live traffic. Live traffic becomes more important the closer thedeparture_timeis to now. Using the default type ofBEST_GUESSgives the most useful predictions for the vast majority of use cases.PESSIMISTICto request that the returnedduration_in_trafficmodels the time based on the historical traffic conditions on bad traffic days. This type gives an estimated duration that is most likely longer than the actual travel time on most days. Occasional days with particularly bad conditions may still exceed this estimate.OPTIMISTICto request that the returnedduration_in_trafficmodels the time based on the historical traffic conditions on good traffic days. This type gives an estimated duration that is most likely shorter than the actual travel time on most days. Occasional days with particularly good conditions may still be faster than this estimate.
For example:
"trafficModel": "OPTIMISTIC"
Specify the
routes.durationfield in the field mask.REST
-H X-Goog-FieldMask: routes.duration
RPC
const (fieldMask = "routes.duration")
When the Routes API estimates time in traffic, it uses the traffic model type you've requested.
Example: Traffic model request
For example, this request specifies that the duration be estimated using the
OPTIMISTIC traffic model type at a specific departure time:
curl -X POST -H 'content-type: application/json' -d ' { "origin": { "address": "Kyoto, Japan" }, "destination": { "placeId": "ChIJrYtcv-urAWAR3XzWvXv8n_s" }, "travelMode": "DRIVE", "routingPreference": "TRAFFIC_AWARE_OPTIMAL", "trafficModel": "OPTIMISTIC" }' \ -H 'Content-Type: application/json' \ -H 'X-Goog-Api-Key: YOUR_API_KEY' \ -H 'X-Goog-FieldMask: routes.duration' \ 'https://routes.googleapis.com/directions/v2:computeRoutes'
This request returns the estimated duration using the OPTIMISTIC traffic model
type for the trip:
"duration": "1238s"
If you change the traffic model type to PESSIMISTIC, the returned duration is
longer:
"duration": "2436s"