Skip to content

Instantly share code, notes, and snippets.

@tukachev
Last active August 29, 2015 14:02
Show Gist options
  • Save tukachev/fe66cf69acb2f02950b6 to your computer and use it in GitHub Desktop.
Save tukachev/fe66cf69acb2f02950b6 to your computer and use it in GitHub Desktop.
google.distance <- function(origin, destination, mode = "driving"){
require(XML)
require(RCurl)
origin <- gsub(" ","+",origin)
destination <- gsub(" ","+",destination)
url <- paste0("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=",origin,"&destinations=",destination,"&mode=",mode,"&language=ru_RU")
xmlpage <- xmlParse(getURL(url))
duration <- as.numeric(xmlValue(xmlChildren(xpathApply(xmlpage,"//duration")[[1]])$value))
distance <- as.numeric(xmlValue(xmlChildren(xpathApply(xmlpage,"//distance")[[1]])$value))
origin.ad <- xmlValue(xpathApply(xmlpage,"//origin_address")[[1]])
destination.ad <- xmlValue(xpathApply(xmlpage,"//destination_address")[[1]])
d <- data.frame(origin.ad, destination.ad, duration, distance, mode)
return(d)
}
#Example1
origin <- "Екатеринбург Лукиных 18"
destination <- "Екатеринбург Машиностроителей 11"
google.distance(origin, destination)
google.distance(origin, destination, "walking")
#Example2
origin <- "56.881792,60.565767"
destination <- "56.88615,60.5993"
google.distance(origin, destination)
google.distance(origin, destination, "walking")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment