5

I am trying to write a simple script in order to calculate in a google sheet the travelling time between two locations, by taking in account the traffic.

I am using the class DirectionFinder of the google Maps API.

I have managed to calculate the time necessary for a trip, but whatever departure time I enter, my travelling time stays the same.

  • Any idea on what am I doing wrong?
  • Is it even possible to take traffic into account using this class?
  • Do I need to be a business user to have access to this?

Here is my code:

function DrivingSeconds(origin, destination, Y, M, D, H, S) {
  Utilities.sleep(1000);
  var time= new Date(Y,M,D,H,S);
  var directions = Maps.newDirectionFinder()
  .setDepart(time)
  .setOrigin(origin)
  .setDestination(destination)
  .setMode(Maps.DirectionFinder.Mode.DRIVING)
  .getDirections();
  return directions.routes[0].legs[0].duration.value;  
}
KyleMit
  • 30,350
  • 66
  • 462
  • 664

2 Answers2

3

directions.routes[0].legs[0].duration_in_traffic may be what you need.

E.g. this code:

function traffic_test() {
  var threeAM = new Date();
  threeAM.setDate(threeAM.getDate() + 1);
  threeAM.setHours(3, 0, 0);
  var eightAM = new Date(threeAM);
  eightAM.setHours(8, 0, 0);

  var directionFinder = Maps.newDirectionFinder()
     .setOrigin('Union Square, San Francisco')
     .setDestination('Golden Gate Park, San Francisco')
     .setMode(Maps.DirectionFinder.Mode.DRIVING);

  var directions = directionFinder.setDepart(threeAM).getDirections();
  var leg = directions.routes[0].legs[0];
  Logger.log('3AM: leg.duration=' + leg.duration.text + 
             '; leg.duration_in_traffic=' + leg.duration_in_traffic.text);

  directions = directionFinder.setDepart(eightAM).getDirections();
  leg = directions.routes[0].legs[0];
  Logger.log('8AM: leg.duration=' + leg.duration.text +
             '; leg.duration_in_traffic=' + leg.duration_in_traffic.text);
}

logs this:

[19-07-21 08:39:54:606 PDT] 3AM: leg.duration=16 mins; leg.duration_in_traffic=12 mins
[19-07-21 08:39:54:795 PDT] 8AM: leg.duration=16 mins; leg.duration_in_traffic=16 mins
Kirill Spitsyn
  • 226
  • 2
  • 6
2

From Google documentation:

For requests where the travel mode is driving: You can specify the departure_time to receive a route and trip duration (response field: duration_in_traffic) that take traffic conditions into account. This option is only available if the request contains a valid API key, or a valid Google Maps APIs Premium Plan client ID and signature. The departure_time must be set to the current time or some time in the future. It cannot be in the past.

https://developers.google.com/maps/documentation/directions/intro#RequestParameters

So yes, only for Premium users.Then your request should look like this:

var request = {
  origin: origin,
  destination: destination,
  drivingOptions: {
    departureTime: new Date(),
  },
  travelMode: google.maps.TravelMode[DRIVING]
};

var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(response,status) {
  if(status == google.maps.DirectionsStatus.OK) {
    console.log(response.routes[0].legs[0].duration_in_traffic);
  }
});
cdm
  • 1,360
  • 11
  • 18