3

I am using the google maps api to get directions with the durationInTraffic option.

var request = {
    origin: start_address,
    destination: end_address,
    waypoints: waypts,
    durationInTraffic: true,
    optimizeWaypoints: true,
    travelMode: tmode
};

directionsService.route(request, function(response, status) { ....

This is working fine with a start and end address with no waypoints, as seen below:

Directions with Time in Current Traffic

My problem is that when i add a waypoint to to route then the "time in current traffic" estimate is no longer shown.

Is this a limitation of the API (I have found no documentation supporting that)? Or a possible fix / workaround as the traffic information is very important to my app.

Vince Lowe
  • 3,521
  • 7
  • 36
  • 63
  • You have Maps API for Business, right? And did you check the `DirectionsLeg` object? – MrUpsidown May 12 '14 at 10:58
  • @MrUpsidown, Yes i have Maps API for Business of course. I do not see any traffic info in the DirectionsLeg object. – Vince Lowe May 12 '14 at 16:54
  • You should have a `duration` and `duration_in_traffic` property in the `DirectionsLeg` object. That's what the doc says: https://developers.google.com/maps/documentation/javascript/reference?csw=1#DirectionsLeg Is it undefined? Can you create a fiddle with your code? – MrUpsidown May 13 '14 at 06:26
  • I'm running into this as well. Ever come across a solution? – Bradley Jan 20 '16 at 04:19
  • @Brad Ryan yea kind of with a workaround. if 'duration_in_traffic' was not defined i gave a link to the user named "Get time in traffic". Then i iterated over the legs which i had stored in array and made a directionsService route request for each and added up the time for each. – Vince Lowe Jan 21 '16 at 11:10
  • @VinceLowe Ah, thanks. I'm also heading toward submitting individual routes. I ended up getting some more info I'll share in an answer. – Bradley Jan 21 '16 at 22:23

1 Answers1

4

I asked a similar question to the folks at Google about whether this was because I was using the stopover = true option in the waypoints and indeed it was. If you set it stopover to false, the duration in traffic should be returned.

There was small mention of this in the current documentation here

From Google:

... we can't predict how long you will stop at stopovers, making it difficult to give a proper estimation for the duration_in_traffic. By using non stopover waypoints you state that you wont stop at those waypoints, thus allowing our service to estimate the duration_in_traffic of every legs.

Also, they mentioned that if your using the Directions Matrix API, only origin/destination pairs count against your quota, so you can send multiple request without additional penalty.

Bradley
  • 2,379
  • 1
  • 11
  • 17
  • Good find. This did resolve the issue. `waypts.push({ location:$(this).val(), stopover:false });` – Vince Lowe Jan 22 '16 at 10:56
  • 1
    But by doing this (stopover=false) you no longer have the 'leg' per waypoint in the response. Any workaround to keep legs entries there? – Raul Claus Mar 25 '17 at 18:40