Getting driving distance between two points (lat, lon) using R and Google Map API Getting driving distance between two points (lat, lon) using R and Google Map API xml xml

Getting driving distance between two points (lat, lon) using R and Google Map API


I authored the gmapsdistance package to do just that. It is available on CRAN. You can use the function in the following way:

results = gmapsdistance(origin = "38.1621328+24.0029257",                        destination = "37.9908372+23.7383394",                        mode = "walking") results# $Time# [1] 30025# # $Distance# [1] 39507# # $Status# [1] "OK"

You can also include vectors of origins and destinations, and get the resulting distance matrix. It supports also directions, and has a bunch of options:

results = gmapsdistance(origin = c("Washington+DC", "New+York+NY", "Seattle+WA", "Miami+FL"),                         destination = c("Los+Angeles+CA", "Austin+TX", "Chicago+IL", "Philadelphia+PA"),                         mode = "bicycling",                         departure = 1514742000)results# $Time#              or Time.Los+Angeles+CA Time.Austin+TX Time.Chicago+IL Time.Philadelphia+PA# 1 Washington+DC              856621         535146          247765                54430# 2   New+York+NY              917486         596011          308630                32215# 3    Seattle+WA              374692         678959          674989               956702# 4      Miami+FL              829039         416667          452035               411283# # $Distance#              or Distance.Los+Angeles+CA Distance.Austin+TX Distance.Chicago+IL Distance.Philadelphia+PA# 1 Washington+DC                 4567470            2838519             1303067                   266508# 2   New+York+NY                 4855086            3126136             1590684                   160917# 3    Seattle+WA                 1982354            3562970             3588297                  5051951# 4      Miami+FL                 4559205            2279966             2381610                  2169382# # $Status#              or status.Los+Angeles+CA status.Austin+TX status.Chicago+IL status.Philadelphia+PA# 1 Washington+DC                    OK               OK                OK                     OK# 2   New+York+NY                    OK               OK                OK                     OK# 3    Seattle+WA                    OK               OK                OK                     OK# 4      Miami+FL                    OK               OK                OK                     OK


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


I needed to calculate driving distances for a bunch of addresses, so I wrote a short function for it and put it in a likewise small packet. You can find it in my GitHub repo: https://github.com/JanMultmeier/GeoData/blob/master/GeoDataPackage/R/GetDist.R

This should get it to run:

require(devtools)install_github("JanMultmeier/GeoData/GeoDataPackage")library(GeoData)getDist(from="1 Infinity Loop, Cupertino, CA 95014", to="1600 Amphitheatre Pkwy, Mountain View, CA 94043",modus="driving",get="distance")

It should return 14.8 km.

Barryhunter has already hinted at the usage restriction by Google, which ties the use of this API to displaying the results on a Google map.

Hope that still helps some people who stumble across this post (like me)...