Heisenberg
Heisenberg

Reputation: 895

Travel time based on arrival time (Using Google Spreadsheet) - Function

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

Upvotes: 1

Views: 1558

Answers (2)

KyleMit
KyleMit

Reputation: 29927

There are actually a couple preconditions for getting traffic duration in the Google Maps API, documented under their advanced Traffic Information section:

  • The travel mode parameter is driving, or is not specified (driving is the default travel mode).
  • The request includes a valid departure_time parameter. The departure_time can be set to the current time or some time in the future. It cannot be in the past.
  • The request does not include stopover waypoints.

What this looks like within Google Apps Script Map API is something like this using .setDepart():

var start = "1600 Amphitheatre Parkway, Mountain View, CA 94043"
var end = "One Apple Park Way, Cupertino, CA 95014"
var tomorrow = new Date()
tomorrow.setDate(tomorrow.getDate() + 1);

var mapObj = Maps.newDirectionFinder();
mapObj.setOrigin(start);
mapObj.setDestination(end);
mapObj.setDepart(tomorrow)

var directions = mapObj.getDirections();
var journey = directions.routes[0].legs[0];
var { duration, duration_in_traffic } = journey

Logger.log({ duration, duration_in_traffic })
// {duration={text=20 mins, value=1209.0}, duration_in_traffic={text=21 mins, value=1230.0}}

You'll then want to read the value from duration_in_traffic instead of duration.

  • If you provide a depature date in the past, you'll get an error
  • If duration_in_traffic is null, it means you didn't satistfy the three criteria

See Also

Upvotes: 0

ziganotschka
ziganotschka

Reputation: 26796

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

Upvotes: 1

Related Questions