Skip to content

Instantly share code, notes, and snippets.

@abelsonlive
Forked from drewconway/google_geocode.R
Created September 9, 2012 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abelsonlive/3687007 to your computer and use it in GitHub Desktop.
Save abelsonlive/3687007 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