使用R和Google Map API获取两点之间的驾驶距离(lat,lon) [英] Getting driving distance between two points (lat, lon) using R and Google Map API

查看:231
本文介绍了使用R和Google Map API获取两点之间的驾驶距离(lat,lon)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得两个点之间的行驶距离,并给出经纬度。
我可以手动将它们放到Google地图中并获得驾驶距离,但我想以编程方式完成所有这些。

我猜JavaScript是要去的语言。但是,我不懂JavaScript,而且我对R的使用相当熟悉。我宁愿在R中执行它,因为我正在做R中的所有数据分析。



I我正在寻找路上的距离而不是乌鸦飞的距离。经过几个小时的尝试之后,我在R中写了下面的函数( this one 帮助)。你有没有更好的方法来获得这个函数内的距离,或者有什么更简单的方法?

  library(XML)
latlon2ft< - 函数(来源,目标)
{

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#从METER到FEET
return(ft )
}

latlon2ft(origin = '37 .193489,-121.07395',destination = '37 .151616,-121.046586')

RESULT = 17224.41

解决方案

RCurl 或同等价格。

 图书馆(XML)
库(bithop)
库(RCurl)
latlon2ft< - 函数(来源,目标){
xml.url< - paste0('http:/ /maps.googleapis.com/maps/api/distancematrix/xml?origins=',origin,'&destinations=',destination,'&mode=driving&sensor=false')
xmlfile< - xmlParse(getURL(xml.url))
dist < - xmlValue(xmlChildren(xpathApply(xmlfile,// distance)[[1]])$ value)
distance< - as。数字(小(km,,dist))
ft < - 距离* 3.28084#从METER到FEET
返回(英尺)
}

latlon2ft(origin = '37 .193489,-121.07395',destination = '37 .151616,-121.046586')

结果:

  [1] 17224.41 


I am trying to get the driving distance between two points with lat/lon given. I can manually put them into google map and get the driving distance but I want to do all this programatically.

I guess JavaScript is the language to go. But, I do not know JavaScript and I am fairly familiar using R. I would prefer to do it in R since I am doing all the data analysis in R.

I am looking for distance along the road not crow-fly distance. After few hours of trying, I wrote the following function in R (This and this one helped). Do you have any better way to get the distance either within this function or anything very very simpler?

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

解决方案

You need RCurl or an equivalent here.

library(XML)
library(bitops)
library(RCurl)
latlon2ft <- function(origin,destination){
  xml.url <- paste0('http://maps.googleapis.com/maps/api/distancematrix/xml?origins=',origin,'&destinations=',destination,'&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.07395',destination='37.151616,-121.046586')

Result:

[1] 17224.41

这篇关于使用R和Google Map API获取两点之间的驾驶距离(lat,lon)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆