Skip to content

Instantly share code, notes, and snippets.

@drewconway
Created June 18, 2012 14:05
Show Gist options
  • Save drewconway/2948536 to your computer and use it in GitHub Desktop.
Save drewconway/2948536 to your computer and use it in GitHub Desktop.
Grab lat / lon from an address using Google Maps
# Get a lat/lon value for each address
geocode.addr <- function(city, country) {
geo.url <- "http://maps.googleapis.com/maps/api/geocode/json?address="
geo.text <- getURL(paste(geo.url, URLencode(paste(city, country, collapse="+")), "&sensor=false", sep=""))
geo.json <- fromJSON(geo.text)
if(geo.json$status == "OK"){
return(geo.json$results[[1]]$geometry$location)
}
else{
if(geo.json$status == "OVER_QUERY_LIMIT") {
stop("Hit rate limit")
}
else {
return(c(NA, NA))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment