The main requirement is to find travel time with traffic data between two locations or zip-code.
Inputs parameters would be from Location, destination, arrival time(this is between 6AM 8AM), mode of transportation, traffic model
Based on the above input parameters in my google script function it should return travel time
Can the below code modified for this requirement ?
function GetDuration(location1, location2, mode) {
var directions = Maps.newDirectionFinder()
.setOrigin(location1)
.setDestination(location2)
.setMode(Maps.DirectionFinder.Mode[mode])
.getDirections();
return directions.routes[0].legs[0].duration.text;
}
//directions from Times Sq to Central Park, NY
Logger.log(GetDuration("40.7591017,-73.984488","40.7670973,-73.9793693","DRIVING") )
From To Mode Distance
Central Park, NY Times Sq TRANSIT 8 mins
Please find below a sample how to use setArrive()
:
function GetDuration(location1, location2, mode) {
var arrive = new Date(new Date().getTime() + (10 * 60 * 60 * 1000));//arrive in ten hours from now
var directions = Maps.newDirectionFinder().setArrive(arrive)
.setOrigin(location1)
.setDestination(location2)
.setMode(Maps.DirectionFinder.Mode[mode])
.getDirections();
return directions.routes[0].legs[0].duration.text;
}
If you want to provide an arrival time - you also need to specify the date - just like you would from the user interface in Google Maps. Dates can be created with JavaScript date methods.
E.g. with new Date(year, month, day, hours, minutes, seconds, milliseconds)
.
Sample:
var arrive=new Date(2019, 09, 07, 06);// 7th of September 2019 06:00 am