我想要获取给定经纬度的两个点之间的驾车距离。我可以手动将它们放入 Google Maps 中并获取驾车距离,但我希望通过编程实现。
我猜 JavaScript 是解决方案。但是,我不懂 JavaScript,我相当熟悉使用 R。因为我所有的数据分析都是在 R 中进行的,所以我更喜欢用 R 实现。
我需要的是沿着道路的距离而不是直线距离。试了几个小时后,我在 R 中编写了以下函数(这篇文章 和 这篇文章 帮了我不少)。你有更好的方法来获取距离吗?可能是在这个函数内部或者更简单的方法。
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