我试图获得两点之间的驾驶距离.
我可以手动将它们放入谷歌地图并获得驾驶距离,但是我想以编程方式进行所有操作.
我可以手动将它们放入谷歌地图并获得驾驶距离,但是我想以编程方式进行所有操作.
我想JavaScript是要走的语言.但是,我不知道JavaScript,我很熟悉使用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')
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。