【发布时间】:2013-05-27 14:43:20
【问题描述】:
我试图在给定纬度/经度的情况下获得两点之间的行驶距离。 我可以手动将它们放入谷歌地图并获取行驶距离,但我想以编程方式完成所有这些。
我猜想 JavaScript 是最好的语言。但是,我不懂 JavaScript,而且我对使用 R 相当熟悉。我更喜欢在 R 中进行,因为我在 R 中进行所有数据分析。
我正在寻找沿路的距离,而不是乌鸦飞行的距离。经过几个小时的尝试,我在 R 中编写了以下函数(This 和 this 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
【问题讨论】:
-
哎哟!你有任何其他可接受的方式来获得我需要的东西吗?
标签: xml r google-maps-api-3 driving-distance