1

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
Heisenberg
  • 865
  • 10
  • 26
  • There is the method `setArrive()` https://developers.google.com/apps-script/reference/maps/direction-finder#setArrive(Date) that allows you to specify the arrival time, but I am not aware of it taking into account the current traffic situation. For that you might have to look inot Maps API: https://developers.google.com/maps/documentation/embed/start – ziganotschka Sep 06 '19 at 07:32
  • @ziganotschka thanks for your reply and yes I noticed this,would you mind taking this code block and writing this as your answer ? I got some errors when I attempted to this and also because I'm not so good at scripting skills :D I can accept and up-vote this as well. – Heisenberg Sep 06 '19 at 07:45
  • @ziganotschka Ignore the previous comment I've figured it out regarding the Logger.log and Thanks this what I want would it possible to set hard-coded value to var arrive variable ? Like 6 AM ? f its difficult set var arrive variable to pick the time defined in the row of the spreadsheet – – Heisenberg Sep 06 '19 at 11:11
  • I updated my answer regarding your question. – ziganotschka Sep 06 '19 at 11:29
  • 1
    @ziganotschka hey thanks again for your support will check this ! :) – Heisenberg Sep 06 '19 at 11:35

2 Answers2

1

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
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
0

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664