Skip to content

Instantly share code, notes, and snippets.

@Isaquedanielre
Forked from sh4n3d4v15/Google Directions API
Created December 17, 2015 02:10
Show Gist options
  • Save Isaquedanielre/c9b61b2356046fe4f800 to your computer and use it in GitHub Desktop.
Save Isaquedanielre/c9b61b2356046fe4f800 to your computer and use it in GitHub Desktop.
R functions to lookup distance and duration from two given points
library(RCurl)
library(RJSONIO)
library(plyr)
url <- function(origin, destination) {
root <- "https://maps.googleapis.com/maps/api/directions/json"
u <- paste(root, "?origin=", origin, "&destination=", destination, "&key=AIzaSyAuj0ix2b8t54hrhIEHRsQNk0r6y9eBK2U", sep = "")
return (URLencode(u))
}
lookupDistanceAndDuration <- function(orgin,destination,verbose = FALSE) {
if (verbose) cat(address, "\n")
u <- url(orgin,destination)
print(u)
doc <- getURL(u)
x <- fromJSON(doc, simplify = FALSE)
if (x$status == "OK") {
distance <- x$routes[[1]]$legs[[1]]$distance$value
duration <- x$routes[[1]]$legs[[1]]$duration$value
return (c(distance,duration))
}else{
return (c(NA,NA))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment