Error When Mapping in Ggmap with API Key (403 Forbidden)

Is something wrong with the ggmap and mapview packages in R?

Google has tightened controls on API access to Google Map products. This means that you now need a Google API key to use ggmap().

There are a few steps involved:

  1. Visit https://console.cloud.google.com and create a new project.
  2. Set up an API key: navigation menu -> APIs and services -> Library
    -> Maps Static API
  3. Create a billing account and enable billing for the API key. You will need to provide credit card details but don't need to pay anything. It is a good idea to set some limits on how the API key can be used to prevent theft -- if you are not sharing your code, the easiest way is probably to limit it to requests from your own IP address.
  4. Enable static maps for this api key.
  5. In R, run register_google("<your API key>"). You will need to run this
    for every new session in which you will use ggmap(). I have added it to my .Rprofile.

Good luck!

Using ggmap to create maps and persistent error R

So there are two things here that are causing problems.

  1. ggmap is using Google Maps as its standard map source. The Google Maps API that ggmap is connecting to needs a registered Static Maps API key. You can get a (free) API key here, though note that you will have to register billing to be able to use one (if you don't Maps API requests will return an error). Once you have that set up, you will need to register it in every new session via register_google(key = "...")

Sample Image


  1. The Google Maps API that ggmap is connecting to needs lon/lat coordinates (e.g., location = c(-75.1636077,39.9524175)) to fully run.

So, the full code for you would be:

library(rjson)
library(digest)
library(glue)
library(devtools)
if(!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/ggmap", ref = "tidyup")
library(ggmap)

register_google(key = "...", # your Static Maps API key
account_type = "standard")

map <- get_map(location = c(-75.1636077,39.9524175), zoom = 13)

ggmap(map)

Sample Image



Related Topics



Leave a reply



Submit