9

I'm trying to call a map from google maps using ggmap. Example code:

get_googlemap(center = c(lon = -56.67, lat = 51.47), zoom = 8, size = c(640, 640), scale = 2,
                   format = "png8", maptype = "satellite")

However, this returns the following error:

Source : https://maps.googleapis.com/maps/api/staticmap?center=24.8,-81.7&zoom=8&size=640x640&scale=4&maptype=satellite&key=(removed key for posting)

Error in aperm.default(map, c(2, 1, 3)) : 
  invalid first argument, must be an array
In addition: Warning message:
In get_googlemap(center = c(lon = -81.7, lat = 24.8), zoom = 8,  :
  HTTP 400 Bad Request

I have looked at threads where people are experiencing similar problems. It appears they were resolved after some fiddling with the API key and restrictions. I have done the following things in that context:

  1. Enabled billing
  2. Restricted my API key - however, I am not given an option to restrict it to "Static maps". I currently have it restricted to "Google Cloud apis". Same error as before, but the key is still registering hits.
  3. Restarted R after doing the aforementioned things, and double checking my packages.

Would appreciate some help with this, none of my labmates are having this problem and I'm starting to tear my hair out, lol.

Eugene Chong
  • 1,591
  • 5
  • 16
Hunter Stevens
  • 251
  • 2
  • 7
  • Your code works fine for my API key. Additionally, my key is unrestricted, and when I look at the options for restricting it, `Maps Static API` is available. Try creating a new API key? – Eugene Chong Feb 05 '20 at 22:41

3 Answers3

16

After some messing around, I figured it out. It wasn't extremely intuitive, so I'm posting it here in case someone else who is new runs into the same problem.

Essentially, after generating the key, from the cloud console, you need to go into the library and enable the following four APIs:

Static Maps, Map Embed, Geolocation, Geocoding.

Then, go edit the key. Scroll down and restrict it to the aforementioned four APIs. Worked perfectly fine after that.

Hunter Stevens
  • 251
  • 2
  • 7
1

I had the same issue as you and I fixed it regenerating the API key in this link: https://console.cloud.google.com/

First you look for "APIs and services" (or something like that) and later for "Credentials" in the vertical menu.

If your API key is to old, like mine was and I used just a few times, so maybe the key stopped to work. After I regenerate a new one, it's working fine!

0

I have a bit to add to the solutions above. I only use my API key for generating maps in R and was receiving the same error message as you. I went through all the suggested solutions: enabled billing, restricted the key to only four APIs, and yet was still experiencing the issue. My "free trial" on Google Cloud Console had ended and all I needed to do was Renew my billing when the trial ended. Sharing in case this will help someone out in the future.

KMaddox
  • 3
  • 3
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 31 '22 at 01:45