Google V3 Maps API for receiving data for and without traffic

I want to access the duration from one point to another point with and without traffic (see picutre below)

enter image description here

I want to calculate the delay with and without traffic. I cannot find a method in the Gogole Maps API v3 that could provide me with this data. Or did I somehow compute it with other data?

EDIT:
I know how to calculate the duration. The problem is that I want to get a duration with traffic, as well as a duration without traffic (see Image above) in order to calculate the possible delay time.

EDIT: Please correct me if I am wrong, but it seems that this is not possible with Google API V3 according to this link Google Maps forecast traffic API V3

Does anyone know how I can get duration from one point to another? This is not necessarily Google.

+5
source share
2 answers

The Google Maps JS API has a traffic level .

You need to add the traffic level to the map:

var trafficLayer = new google.maps.TrafficLayer(); trafficLayer.setMap(map); 

There is a living example.

And if you want to calculate the duration, the distance API from Google and in this question from Stackoverflow you can see the answer.

In the Direction Service you need to specify this field:

 { origin: LatLng | String, destination: LatLng | String, travelMode: TravelMode, transitOptions: TransitOptions, unitSystem: UnitSystem, durationInTraffic: Boolean, /* this is with traffic or without */ waypoints[]: DirectionsWaypoint, optimizeWaypoints: Boolean, provideRouteAlternatives: Boolean, avoidHighways: Boolean, avoidTolls: Boolean region: String } 

You can try the traffic first and save the duration, and then you can close the traffic option and save it. So you can see the difference between the two.

+2
source

You can do this using the distance matrix API

GET Request -

 https://maps.googleapis.com/maps/api/distancematrix/json?origins=garching&destinations=hamburg&departure_time=now&key=YOUR_KEY 

will give you duration and duration_in_traffic for the route -

 { "destination_addresses": [ "Hamburg, Germany" ], "origin_addresses": [ "85748 Garching, Germany" ], "rows": [ { "elements": [ { "distance": { "text": "761 km", "value": 760831 }, "duration": { "text": "7 hours 1 min", "value": 25242 }, "duration_in_traffic": { "text": "6 hours 42 mins", "value": 24145 }, "status": "OK" } ] } ], "status": "OK" } 

departure_time is indicated as an optional parameter in the documentation, but is required if you want to see a duration_in_traffic estimate.

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1203850/


All Articles