— Jose Gonzalez

Using Google maps API and R

The White House This post shows how to use Google Maps‘ API with R making some tweaks to this function. Combine the first part with sapply or Plyr and it becomes a very powerful tool in just a few lines of code. You can find a gist in RMarkdown with the code here or click below to continue reading.

#### This script uses RCurl and RJSONIO to download data from Google's API:
#### Latitude, longitude, location type (see explanation at the end), formatted address
#### Notice ther is a limit of 2,500 calls per day

url <- function(address, return.call = "json", sensor = "false") {
 root <- "http://maps.google.com/maps/api/geocode/"
 u <- paste(root, return.call, "?address=", address, "&sensor=", sensor, sep = "")

geoCode <- function(address,verbose=FALSE) {
 if(verbose) cat(address,"\n")
 u <- url(address)
 doc <- getURL(u)
 x <- fromJSON(doc,simplify = FALSE)
 if(x$status=="OK") {
 lat <- x$results[[1]]$geometry$location$lat
 lng <- x$results[[1]]$geometry$location$lng
 location_type <- x$results[[1]]$geometry$location_type
 formatted_address <- x$results[[1]]$formatted_address
 return(c(lat, lng, location_type, formatted_address))
 } else {
##Test with a single address
#address #address
#[1] "38.8976831"
#[2] "-77.0364972"
#[4] "The White House, 1600 Pennsylvania Avenue Northwest, Washington, D.C., DC 20500, USA"

# Use plyr to ggeocoding for a vector
address <- c("The White House, Washington, DC", "The Capitol, Washington, DC")
locations <- ldply(address, function(x) geoCode(x))
names(locations) <- c("lat", "lon", "location_type", "formatted")
# lat lon location_type
# 1 38.8976831 -77.0364972 APPROXIMATE
# 2 38.8899389 -77.0090505 APPROXIMATE
# 1 The White House, 1600 Pennsylvania Avenue Northwest, Washington, D.C., DC 20500, USA
# 2 United States Capitol, East Capitol St NE & First St, Washington, D.C., DC 20004, USA

#Location type, for more info check here: https://developers.google.com/maps/documentation/directions/
#"ROOFTOP" indicates that the returned result is a precise geocode for which we have location information accurate down to street address precision.
#RANGE_INTERPOLATED" indicates that the returned result reflects an approximation (usually on a road) interpolated between two precise points (such as intersections). Interpolated results are generally returned when rooftop geocodes are unavailable for a street address.
#GEOMETRIC_CENTER" indicates that the returned result is the geometric center of a result such as a polyline (for example, a street) or polygon (region).
#APPROXIMATE" indicates that the returned result is approximate.

  • http://decisionstats.com Ajay Ohri

    Have you looked at the geocode function in ggmap package , i think yours is an enhancement

  • Jose

    I didn’t know the one at ggmap. Although, some Python packages follow a similar structure like the one here. By the way… very cool that stamen maps can be used with ggmap.

  • John

    Have you had any luck doing something similar with the Google Civics API? I’m having trouble getting it to do much of anything.

  • Danny

    I’m relatively new to R, what’s so special about this map?

  • andi

    Can you plot a fragments of map?

  • David

    Fairly new to geocoding and json but this code has been very helpful. Is it possible to return the street_name and street_number along with the lat, long, type and formatted address?

    • Jose

      You can get the formatted address, look here Not so sure about the street number, perhaps with the google places API..