xml – 使用R和Google Map API获取两点之间的距离(纬度,lon)

我试图获得两点之间的驾驶距离.
我可以手动将它们放入谷歌地图并获得驾驶距离,但是我想以编程方式进行所有操作.

我想JavaScript是要走的语言.但是,我不知道JavaScript,我很熟悉使用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')

RESULT = 17224.41

你需要RCurl或等同于此.
library(XML)
library(RCurl)
latlon2ft <- function(origin,destination){
  xml.url <- paste0('http://maps.googleapis.com/maps/api/distancematrix/xml?origins=','&mode=driving&sensor=false')
  xmlfile <- xmlParse(getURL(xml.url))
  dist <- xmlValue(xmlChildren(xpathApply(xmlfile,"//distance")[[1]])$value)
  distance <- as.numeric(sub(" km","",dist))
  ft <- distance*3.28084 # FROM METER TO FEET
  return(ft)
}

latlon2ft(origin='37.193489,-121.046586')

结果:

[1] 17224.41

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念