gpt4 book ai didi

xml - 使用 R 和 Google Map API 获取两点(纬度、经度)之间的行驶距离

转载 作者:数据小太阳 更新时间:2023-10-29 01:39:58 24 4
gpt4 key购买 nike

我正在尝试在给定纬度/经度的情况下获取两点之间的行驶距离。我可以手动将它们放入谷歌地图并获得行驶距离,但我想以编程方式完成所有这些工作。

我想 JavaScript 是合适的语言。但是,我不懂 JavaScript,而且我对 R 的使用相当熟悉。我更愿意在 R 中进行,因为我在 R 中进行所有数据分析。

我正在寻找沿路的距离,而不是乌鸦飞行距离。经过几个小时的尝试,我在 R 中编写了以下函数(Thisthis one 帮助)。您是否有任何更好的方法来在此函数内或任何非常非常简单的方法中获取距离?

library(XML)
latlon2ft <- function(origin,destination)
{

xml.url <- paste0('http://maps.googleapis.com/maps/api/distancematrix/xml?origins=',origin,'&destinations=',destination,'&mode=driving&sensor=false')

xmlfile <- xmlTreeParse(xml.url)
xmltop = xmlRoot(xmlfile)
distance <- xmltop[['row']][[1]][5][1][['distance']][['value']][[1]]
distance <- as.numeric(unclass(distance)[['value']])
ft <- distance*3.28084 # FROM METER TO FEET
return(ft)
}

latlon2ft(origin='37.193489,-121.07395',destination='37.151616,-121.046586')

结果 = 17224.41

最佳答案

我编写了 gmapsdistance 包来做到这一点。它在 CRAN 上可用。您可以通过以下方式使用该功能:

results = gmapsdistance(origin = "38.1621328+24.0029257",
destination = "37.9908372+23.7383394",
mode = "walking") results
# $Time
# [1] 30025
#
# $Distance
# [1] 39507
#
# $Status
# [1] "OK"

您还可以包含起点和终点的向量,并获得生成的距离矩阵。它也支持方向,并且有很多选项:

results = gmapsdistance(origin = c("Washington+DC", "New+York+NY", "Seattle+WA", "Miami+FL"), 
destination = c("Los+Angeles+CA", "Austin+TX", "Chicago+IL", "Philadelphia+PA"),
mode = "bicycling",
departure = 1514742000)
results
# $Time
# or Time.Los+Angeles+CA Time.Austin+TX Time.Chicago+IL Time.Philadelphia+PA
# 1 Washington+DC 856621 535146 247765 54430
# 2 New+York+NY 917486 596011 308630 32215
# 3 Seattle+WA 374692 678959 674989 956702
# 4 Miami+FL 829039 416667 452035 411283
#
# $Distance
# or Distance.Los+Angeles+CA Distance.Austin+TX Distance.Chicago+IL Distance.Philadelphia+PA
# 1 Washington+DC 4567470 2838519 1303067 266508
# 2 New+York+NY 4855086 3126136 1590684 160917
# 3 Seattle+WA 1982354 3562970 3588297 5051951
# 4 Miami+FL 4559205 2279966 2381610 2169382
#
# $Status
# or status.Los+Angeles+CA status.Austin+TX status.Chicago+IL status.Philadelphia+PA
# 1 Washington+DC OK OK OK OK
# 2 New+York+NY OK OK OK OK
# 3 Seattle+WA OK OK OK OK
# 4 Miami+FL OK OK OK OK

关于xml - 使用 R 和 Google Map API 获取两点(纬度、经度)之间的行驶距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16863018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com