我在R语言的ggmap
库的get_map()
函数中遇到了问题。
几个月来,我的代码一直可以运行而不需要指定API密钥(对于source = "google"
)。然而,几周前这段代码停止工作了。我了解到Google已经强制要求使用API密钥了(或者他们允许在没有api密钥的情况下调用一定数量的次数,而我已经超出了这个限制)。
即使我指定了从Google Cloud Platform获得的API密钥,但我的代码仍然表现出相同的问题。我甚至联系过Google Cloud Support,但他们说API密钥本身没有问题,并且他们能够在他们的端口调用地图。
我怀疑get_map()
函数在从Google调用地图时未传递api_key
。任何有关解决此问题的指针都将不胜感激。
以下是无法重现的代码(失败了)。
library(ggmap)
lat <- c(4,41) # India lat boundaries
lon <- c(68,99) # India long boundaries
center = c(mean(lat), mean(lon))
map <- get_map(location = c(lon = mean(lon),
lat = mean(lat)),
api_key = <my api key>,
zoom = 6,
maptype = "terrain",
source = "google",
messaging = TRUE
)
以下是R中的错误信息(请注意API密钥未被传递)
trying URL 'http://maps.googleapis.com/maps/api/staticmap?center=22.5,83.5&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false'
Error in download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") :
cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=22.5,83.5&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=22.5,83.5&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false': HTTP status was '403 Forbidden'