Reputation:
I want to get the navigation duration from google maps api with http request: https://maps.googleapis.com/maps/api/directions/json?origin=src&destination=destination
but i get the duration without traffic. how can i get the duration with traffic?
Upvotes: 3
Views: 7765
Reputation: 4017
You are now able to get duration_in_traffic
without a premium account (just a standard API key from the developer console with the "Google Maps Directions API" enabled). Verify in your browser with
https://maps.googleapis.com/maps/api/directions/json?origin=Brooklyn&destination=Queens&departure_time=now&key=YOUR_KEY_HERE
It appears that the javascript client uses a different endpoint that does not yet return duration_in_traffic
.
The request must meet the following conditions, as updated in the docs:
- The request includes a departure_time parameter.
- The request includes a valid API key, or a valid Google Maps APIs Premium Plan client ID and signature.
- Traffic conditions are available for the requested route.
- The request does not include stopover waypoints.
- The request is specifically for driving directions— the mode parameter is set to driving.
Upvotes: 7
Reputation: 6921
From the Google Directions API, it states that:
duration_in_traffic indicates the total duration of this leg, taking into account current traffic conditions. The duration in traffic will only be returned if all of the following are true:
1.The directions request includes a departure_time parameter set to a value within a few minutes of the current time.
2.The request includes a valid Google Maps API for Work client and signature parameter.
3.Traffic conditions are available for the requested route.
4.The directions request does not include stopover waypoints.
So you need to meet all the above requirements in order to retrieve duration with traffic.
For more information, you can visit the Leg section of the Google Directions API documentation.
Upvotes: 6