首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于到达时间的旅行时间(使用Google电子表格)-函数

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

Stack Overflow用户
提问于 2019-09-06 01:33:05
回答 1查看 629关注 0票数 0

主要的需求是找到两个地点或邮政编码之间的交通数据的旅行时间。

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

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

下面的代码可以针对这个需求进行修改吗?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-06 15:52:03

请在下面找到如何使用setArrive()的示例

代码语言:javascript
复制
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 Maps的用户界面中一样。可以使用JavaScript date methods创建日期。

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

示例:

代码语言:javascript
复制
var arrive=new Date(2019, 09, 07, 06);// 7th of September 2019 06:00 am
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57810486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档