获取 google 工作表中两个地址之间的距离,并使用模式传输
Get the distance between two Addresses in google sheets with mode transit
我正在尝试通过模式运输获取 A 和 B 之间的距离。
这是我在 google 张(excel)
上的代码
=importXML("http://maps.googleapis.com/maps/api/directions/json?origin="& A4 & "&destinations=" & D4 & "&mode=transit&arrival_time=1391374800&key=MYKEYHERE"; "//distance/text")
它说 "Url can not be called"。
我在这个问题上坐了一段时间,如果有任何帮助,我将不胜感激。
其他然后导入 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")
好吧,您可以构建一个自定义函数以在您的 sheet 上使用,
在 sheet 的菜单上选择:工具 > 脚本编辑器并粘贴:
/**
* @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 [["", ""]];
}
}
然后继续 sheet 并将其设置为一个单元格:
=getDistanceBetween(-23.319937, -51.166299, -23.310565, -51.165488)
// 或者单元格引用;
我也在寻找一种方法来做到这一点,之前从未在 sheet 上使用过自定义 google 函数,然后发现:这个要点 https://gist.github.com/simonwhatley/4ea2e87815747bf14d1d247181248512
开箱即用,唯一复杂的部分是将其添加到 sheets。
使用方法:
- 打开一个 sheet 文件然后转到
Tools
> Script Editor
- 项目名称随便填什么
- 将要点复制到
code.gs
- 点击保存
- 在 google sheet 中,只要您想使用函数
就调用 =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
我正在尝试通过模式运输获取 A 和 B 之间的距离。
这是我在 google 张(excel)
上的代码=importXML("http://maps.googleapis.com/maps/api/directions/json?origin="& A4 & "&destinations=" & D4 & "&mode=transit&arrival_time=1391374800&key=MYKEYHERE"; "//distance/text")
它说 "Url can not be called"。 我在这个问题上坐了一段时间,如果有任何帮助,我将不胜感激。
其他然后导入 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")
好吧,您可以构建一个自定义函数以在您的 sheet 上使用, 在 sheet 的菜单上选择:工具 > 脚本编辑器并粘贴:
/**
* @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 [["", ""]];
}
}
然后继续 sheet 并将其设置为一个单元格:
=getDistanceBetween(-23.319937, -51.166299, -23.310565, -51.165488)
// 或者单元格引用;
我也在寻找一种方法来做到这一点,之前从未在 sheet 上使用过自定义 google 函数,然后发现:这个要点 https://gist.github.com/simonwhatley/4ea2e87815747bf14d1d247181248512
开箱即用,唯一复杂的部分是将其添加到 sheets。
使用方法:
- 打开一个 sheet 文件然后转到
Tools
>Script Editor
- 项目名称随便填什么
- 将要点复制到
code.gs
- 点击保存
- 在 google sheet 中,只要您想使用函数 就调用
=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