基于到达时间的行程时间(使用 Google 电子表格)- 函数

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

主要要求是使用两个位置之间的交通数据或邮政编码查找行程时间。

输入参数将来自位置、目的地、到达时间(这是早上 6 点到 8 点之间)、交通方式、交通模型

根据我的 google 脚本函数中的上述输入参数,它应该 return 行程时间

是否可以针对此要求修改以下代码?

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

请在下面找到如何使用 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;
}

如果您想提供到达时间 - 您还需要指定日期 - 就像您在 Google 地图的用户界面中所做的那样。可以使用 JavaScript date methods.

创建日期

例如new Date(year, month, day, hours, minutes, seconds, milliseconds).

样本:

var arrive=new Date(2019, 09, 07, 06);// 7th of September 2019 06:00 am