Search code examples
rggmap

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


This past summer I was working on a code in R where I used the packages mapview and ggmap. During the summer the code was working fine. This past week I ran the code without any changes and I get the following message when I use mapview:

mapview(as(tr10, "Spatial"), zcol = "utc_timestamp", lwd = 5,
      legend = TRUE)
Error in htmlwidgets::sizingPolicy(defaultWidth = defaultWidth, defaultHeight = defaultHeight,  : 
unused argument (browser.external = FALSE)

I did not made any changes to my code and I upgrade the package just in case but still does not work. And with ggmap I get the following:

puerto_rico <- get_map(location = c(lon = mean(long), lat = mean(lat)), 
                     maptype = "terrain", source = "google",
                     zoom = 12)
Error in download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") : 
cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=18.2,-67.1&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false'
In addition: Warning message:
In download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") :

cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=18.2,-67.1&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false': HTTP status was '403 Forbidden'

Can someone explain what is happening with these packages and what other alternatives do I have for me to access the use of maps for my plots?


Solution

  • 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!