Search code examples
rggplot2ggmap

How to pass a google API key to get_maps?


I am trying to use get_map and I presume google now requires to access the API using a key since the code in the documentation doesn't work

get_map(location = "texas", zoom = 6, source = "stamen")

Error in download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") : cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=texas&zoom=6&size=640x640&scale=2&maptype=terrain&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=texas&zoom=6&size=640x640&scale=2&maptype=terrain&sensor=false': HTTP status was '403 Forbidden'

Suppose my key is XXX, I thought this would work:

get_map(location = "texas", zoom = 6, source = "stamen", api_key = 'XXX')

However, I get the same error as before. I suppose I am passing the key incorrectly since the new error message doesn't show XXX anywhere in the URL.


Solution

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

    from: R get_map not passing the api key