【问题标题】:Travel time in Google Sheets using Google Directions API使用 Google Directions API 在 Google 表格中的旅行时间
【发布时间】:2019-10-07 19:35:34
【问题描述】:

对于这些输入:

  1. 产地
  2. 目的地
  3. 到货时间

我想要两个可以显示的 Google 表格公式:

  1. 以英里为单位的距离
  2. 行程时间(开车,堵车)。

引用表格中单元格的简单公式类似于

=TravelTime(Origin,Destination,Arrive)

到目前为止,我已经设置了一个 Google Directions API 帐户并将其拼凑在一起,但我不知道如何使 URL 正常工作以及如何让公式返回我想要的输出。

function TravelTime(Origin,Destination,Arrive) {

   var Origin
   var Destination
   var Arrive

   var apiUrl = 'https://maps.googleapis.com/maps/api/directions/json?origin='&Origin&'&destination='&Destination&'&key=MYKEY';

}

【问题讨论】:

  • 这个问题非常广泛。看起来您需要阅读一些教程 Google Apps 脚本、发出 REST API 请求和使用 JSON。当您遇到包含 minimal reproducible example 的特定编码问题并展示了对该解决方案进行编码的可靠尝试时,Stack Overflow 社区将能够为您提供更好的帮助。
  • 还有看看 TomTom。每天有 2500 笔免费交易。我用它来显示我们网站的旅行时间,甚至显示由于交通等原因造成的延迟时间。它还提供距离、出发和到达时间,developer.tomtom.com/store/maps-api
  • 谢谢大家!在你的帮助下,我让它与谷歌地图一起工作。我检查了 TomTom,它看起来很相似。如果我用完 Google API 的信用额度,我可能需要查看两者之间的定价。

标签: json google-apps-script google-sheets google-api directions


【解决方案1】:

您似乎正在尝试拨打external API

尝试做这样的事情:

function TravelTime(Origin,Destination,Arrive) {
  var apiUrl = 'https://maps.googleapis.com/maps/api/directions/json?origin='&Origin&'&destination='&Destination&'&key=MYKEY';
  var response = UrlFetchApp.fetch(apiUrl);
  var json = response.getContentText();
  var data = JSON.parse(json); //your final object, get the data you want here then return it.
}

希望对你有帮助!

【讨论】:

  • 谢谢!在你的帮助下,我得到了它的工作。一件事是我使用 & 而不是 + 来连接 URL 的元素。
猜你喜欢
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多