Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in ggmap, "must be an array" and "HTTP 400 bad request"

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.

like image 200
Hunter Stevens Avatar asked Feb 04 '20 15:02

Hunter Stevens


1 Answers

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.

like image 114
Hunter Stevens Avatar answered Sep 23 '22 10:09

Hunter Stevens