Skip to content

Instantly share code, notes, and snippets.

@hadley
Created March 13, 2014 21:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadley/9537581 to your computer and use it in GitHub Desktop.
Save hadley/9537581 to your computer and use it in GitHub Desktop.
# This script uses httr to download data from Google's API
# Notice ther is a limit of 2,500 calls per day
library(httr)
base_url <- "http://maps.google.com/maps/api/geocode/json"
geoCode <- function(address,verbose=FALSE) {
r <- GET(base_url, query = list(address = address, sensor = "false"))
stop_for_status(r)
result <- content(r)
if (!identical(result$status, "OK")) {
warning("Request failed.", call. = FALSE)
return(c(NA,NA,NA, NA))
}
first <- result$results[[1]]
list(
lat = first$geometry$location$lat,
lng = first$geometry$location$lng,
type = first$geometry$location_type,
address = first$formatted_address
)
}
# Test with a single address
geoCode("6100 Main Street, Houston, TX")
@schnee
Copy link

schnee commented Feb 17, 2015

consider "https" instead of "http" in base_url.

(I would have done this via a pull request, but gists don't appear to have that functionality)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment