I have a piece of code which all of the sudden stopped working. As simple as that. I found some vague answers on SO, but nothing is helping in my case.
library(ggmap)
myLocation <- c(21.5, -18.5, 34, -8)
myMap <- get_map(location=myLocation,
source="google", maptype="terrain", crop=FALSE, color="bw")
I get the following error:
Warning: bounding box given to google - spatial extent only approximate.
converting bounding box to center/zoom specification. (experimental)
Error in download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") :
cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=-13.25,27.75&zoom=6&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=-13.25,27.75&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false': HTTP status was '403 Forbidden'
Tried this line, but didn't help:
options(download.file.method = "curl")
If I try osm - also problems:
osmmap <- get_openstreetmap(bbox = c(left = 21.5, bottom = -18.5, right =
34, top = 8), scale = 7, color = c("bw"))
Error: map grabbing failed - see details in ?get_openstreetmap.
In addition: There were 16 warnings (use warnings() to see them)
Any idea what to do?
So there are two things here that are causing problems.
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 = "...")
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)