获取谷歌表格中具有模式传输的两个地址之间的距离



我正在尝试获取 A 和 B 与模式传输之间的距离。

这是我在谷歌表格(excel(上的代码

=importXML("http://maps.googleapis.com/maps/api/directions/json?origin="& A4  & "&destinations=" & D4 & "&mode=transit&arrival_time=1391374800&key=MYKEYHERE"; "//distance/text")

它说"不能调用 URL"。 我在这个问题上坐了一段时间,我将不胜感激任何帮助。

除此之外,导入XML并在代码中说/json?origin我真的不明白这个问题。

编辑:我在文档中找到了这个

<travel_mode>TRANSIT</travel_mode>
<start_location>
<lat>40.7563670</lat>
<lng>-73.9907530</lng>
</start_location>
<end_location>
<lat>40.8179080</lat>
<lng>-74.0656630</lng>

但我不确定如何在上面的行中输入它。 我为目的地和起点做所有的长和纬度。

这是答案

=importXML("https://maps.googleapis.com/maps/api/distancematrix/xml?&origins="& A4 & "&destinations=" & D4 & "&mode=transit&key=YOURKEYHERE"; "//distance/text")

好吧,您可以构建一个自定义函数以在工作表上使用, 在工作表的菜单上,转到: 工具> 脚本编辑器 并粘贴以下内容:

/**
* @customFunction
*/
function getDistanceBetween(originLat, originLong, destLat, destLong) {
var directions, route;
// Create a new Direction finder using Maps API available on Google Apps Script
directions = Maps.newDirectionFinder()
.setOrigin(originLat, originLong)
.setDestination(destLat, destLong)
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections(); // Direction Object
// The path may have some segments so it sum it all
if(directions.routes[0]){
route = directions.routes[0].legs.reduce(function(acc, currentRow){
acc.dist += currentRow.distance.value/1000;
acc.dur += currentRow.duration.value/60;
return acc;
},{dist:0,dur:0});
// This will return data to fill 2 columns. 
// First with de distance
// Second with a link to view the path on map's site;
return [[route.dist, "https://www.google.com/maps/dir/"+originLat+","+originLong+"/"+destLat+","+destLong+"/"]];
} else {
return [["", ""]];
}
}

然后转到工作表并将其设置为单元格:

=getDistanceBetween(-23.319937, -51.166299, -23.310565, -51.165488)或单元格引用;

我也在寻找一种方法来做到这一点,以前从未在工作表上使用过自定义谷歌功能,并发现:这个要点 https://gist.github.com/simonwhatley/4ea2e87815747bf14d1d247181248512

它几乎可以开箱即用,唯一复杂的部分是将其添加到工作表中。

要使用它:

  1. 打开工作表文件,然后转到Tools>Script Editor
  2. 填写您想要的项目名称
  3. 将要点复制到code.gs
  4. 点击保存
  5. 在谷歌表格中,每当你想使用该功能时调用=GOOGLEDISTANCE()

例如:

=GOOGLEDISTANCE(A1, A2, "diriving", "minutes")
=GOOGLEDISTANCE("100 Someplace", "200 otherplace", "driving", "minutes")
=GOOGLEDISTANCE("22.5985, 47.6767", "45.3545, 23.44545", "walking", "minutes")

请务必将 gist 中的区域设置自定义到您的实际位置。例如UK>US

该函数 GOOGLEDISTANCE(( 效果很好!我已经提升了我的表格技能。

最新更新