get_map not passing the API key (HTTP status was '403 Forbidden')

You need to use register_google(key = "...") in every new session of R. Using api_key = inside the get_map() call does not work.


updated: 2018-12-24 for ggmap 2.7.904 and current Google Cloud API

Step-by-Step Tutorial

1. Update to newest version of ggmap

require(devtools)
devtools::install_github("dkahle/ggmap", ref = "tidyup")

2. Activate your Google API key for all APIs in the Google Cloud Console

  • Link for more info on how to get an API key

  • Direct Link to Google Cloud Platform Console

  • Direct Link to Google Maps API Pricing Information

  • APIs you need: Maps Static and Geocoding

  • Enable billing in the general settings.

enter image description here

enter image description here

3. Load ggmap and register key

library(ggmap)
register_google(key = "...")     # copied directly from Google Console via 'copy' button

4. Plot default map

ggmap(get_googlemap())          

Houston

5. Plot with location name (Geocoding)

ggmap(get_map("Hannover, Germany"))

If you get an error here (e.g., Forbidden 403) you most probably have not activated your key for the right APIs. Tutorial to troubleshoot geocoding

Hannover

6. Plot with longitude and latitude

ggmap(get_map(location=c(16.3738,48.2082), zoom=13, scale=2))

3


Just to add to Roman Abashin's answer (I can't comment, unfortunately): As per '?get_map()', the 'api_key =' argument doesn't work for Google maps. You'll need to use the 'register_google()' function, but as of 03/10/18, it's only in the development version of ggmap, which you can get like so:

devtools::install_github("dkahle/ggmap", ref = "tidyup")

Then you'll also need to enable billing on your Google account, though the first 100,000 maps you use each month should be free, see here: https://cloud.google.com/maps-platform/pricing/sheet/ for details.

(tips drawn from here: https://github.com/dkahle/ggmap/issues/51)


I don't know the direct resolution to the ggmap issue, but if you're happy to work with an interactive map rather than a static one you can use my googelway library

library(googleway)

set_key("GOOGLE_MAP_KEY")

lat <- c(4,41) #India lat boundaries
lon <- c(68,99) #India long boundaries
center = c(mean(lat), mean(lon))

google_map(location = center, zoom = 6)

enter image description here


Just adding to @Roman's response, here's the code that worked for me:

if(!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/ggmap", ref = "tidyup")
library(ggmap)
register_google(key = "your_API_key") 
usa<- get_googlemap(location='united states', zoom=4,maptype = "hybrid")

For more information you could refer to the library page on github: here
Hopefully it helps!