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;
}