Increase the api limit in ggmap's geocode function (in R)

10,066

Solution 1

I didn't find a way to use the existing geocode function (from the ggmap library) to answer this question, so I just created a new function to just do this myself using the existing getURL function (from the RCurl library) and the fromJSON function (from the RJSONIO library).

Write the new function:

library(RJSONIO)
library(RCurl)

getGeoData <- function(location){
  location <- gsub(' ','+',location)
  geo_data <- getURL(paste("https://maps.googleapis.com/maps/api/geocode/json?address=",location,"&key=**[YOUR GOOGLE API KEY HERE]**", sep=""))
  raw_data_2 <- fromJSON(geo_data)
  return(raw_data_2)
}

Test: getGeoData("San Francisco")

This gives you a list with the same data that's almost (but not quite) in the same exact format as the list produced by geocode("San Francisco").

Solution 2

I've written the package googleway to access google maps API where you can specify your api key.

For example

library(googleway)

key <- "your_api_key"

google_geocode(address = "San Francisco",
               key = key)

# $results
# address_components
# 1 San Francisco, San Francisco County, California, United States, SF, San Francisco County, CA, US, locality, political, administrative_area_level_2, political, administrative_area_level_1, political, country, political
# formatted_address geometry.bounds.northeast.lat geometry.bounds.northeast.lng geometry.bounds.southwest.lat
# 1 San Francisco, CA, USA                      37.92977                     -122.3279                      37.69313
# geometry.bounds.southwest.lng geometry.location.lat geometry.location.lng geometry.location_type
# 1                     -123.1661              37.77493             -122.4194            APPROXIMATE
# geometry.viewport.northeast.lat geometry.viewport.northeast.lng geometry.viewport.southwest.lat
# 1                          37.812                       -122.3482                         37.7034
# geometry.viewport.southwest.lng                    place_id               types
# 1                        -122.527 ChIJIQBpAG2ahYAR_6128GcTUEo locality, political
# 
# $status
# [1] "OK"

Solution 3

With ggmap version 2.7 or greater (as of 13 Dec, not yet available on Cran, but you can install with devtools::install_github("dkahle/ggmap"), you simply need to run register_google(key = 'LONG KEY STRING') and then you can call any of the ggmap functions such as geocode or mutate_geocode and use your API key.

Solution 4

Thanks for this! It helped me along immensely. Your solution is pretty specific, so I wanted to include the adaptations I made of your function. It threw bugs because raw_data and geo_data_list are undefined. I'm guessing these were specific to your local environment.

For me, inputting a location and returning the lat, lon worked with this:

 getGeoData <- function(location, api_key){
  location <- gsub(' ','+',location)
  geo_data <- getURL(paste("https://maps.googleapis.com/maps/api/geocode/json?address=",location,sprintf("&key=%s",api_key), sep=""))
  geo_data <- fromJSON(geo_data)
  return(geo_data$results[[1]]$geometry$location)
}

You can modify the return statement to index into geo_data to get different properties other than lat lon too.

Hope this helps someone.

R

Share:
10,066
L P
Author by

L P

Updated on July 13, 2022

Comments

  • L P
    L P almost 2 years

    I'm trying to use the geocode function from the ggmaps library in R to get coordinates for specific locations. I'm able to use the function fine so far.

    The issue I'm running into is that I would like to increase my daily limit from 2,500 to 100,000. The official Google documentation says that this is readily possible if you enable billing on the project, which I'm happy to do. When you proceed with this process, the Google Developers Console gives you a personalized API key.

    However, the geocode function doesn't have an option to put in this personalized API key. Instead, it asks for the client (client ID for business users) and signature(signature for business users), which is how Google Maps API for Work customers can access the API. I get that this is also an option, but that seems to be a very use case, since Google Maps API for Work seems to be designed for large enterprise accounts:

    Daily quota starting at 100,000 requests per 24 hours, based on annual contractual purchase.

    So my question boils down to this: can I use the geocode function from the ggmapslibrary in R to ping the Google Maps Geocoding API?

  • yuk
    yuk about 6 years
    Its Mar 2018, and 2.7 still not in CRAN. ;(
  • Arthur
    Arthur over 5 years
    @yuk October 2018 now, still not in CRAN, and now geocode doesn't work at all without an API key: Keyless access to Google Maps Platform is deprecated. Please use an API key with all your API calls to avoid service interruption. For further details please refer to http://g.co/dev/maps-no-account