Skip to content

Instantly share code, notes, and snippets.

@dwinter
Created October 23, 2013 22:47
Show Gist options
  • Save dwinter/7128210 to your computer and use it in GitHub Desktop.
Save dwinter/7128210 to your computer and use it in GitHub Desktop.
library(stringr)
library(rjson)
geocode <- function(loc_string){
clean_loc <- str_replace_all(str_replace_all(loc_string, "[[:punct:]]", ""), " ", "+")
base_url <- "http://maps.googleapis.com/maps/api/geocode/json?address="
loc_url <- paste(base_url, clean_loc, "&sensor=false", sep="")
geocode <- fromJSON(paste(readLines(loc_url), collapse=""))
if( length(geocode$results) > 0 ){
return(geocode$results[[1]]$geometry$location)
}
else return(NA)
}
#> geocode("Casey, Antarctica")
#
##$lat
##[1] -66.28167
##
##$lng
##[1] 110.5244
##
##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment