get_map未传递API密钥(HTTP状态为“403 Forbidden”)。

11

我在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'

2
谢谢@SymbolixAU。我稍后会检查这个。目前,我的问题已经通过使用get_googlemap而不是get_map得到解决。似乎get_map并不设计为在源为google时接受api_key。只有当源为cloudmade时,get_map才使用api_key。 - Venky
4个回答

17

在每个新的R会话中,您需要使用register_google(key = "...")。在get_map()调用内部使用api_key = 是不起作用的。


更新日期:2018年12月24日,适用于ggmap 2.7.904和当前的Google Cloud API

逐步教程

1. 更新到最新版本的ggmap

require(devtools)
devtools::install_github("dkahle/ggmap", ref = "tidyup")

2. 激活您的 Google API 密钥以启用 Google Cloud Console 中的所有 API

enter image description here

enter image description here

3. 加载ggmap并注册密钥

library(ggmap)
register_google(key = "...")     # copied directly from Google Console via 'copy' button

4. 绘制默认地图

ggmap(get_googlemap())          

Houston

5. 使用地名绘制图表(地理编码)

ggmap(get_map("Hannover, Germany"))

如果您在此处遇到错误(例如Forbidden 403),那么您很可能没有为正确的API激活密钥。教程:解决地理编码问题

Hannover

6. 使用经纬度绘制图表

ggmap(get_map(location=c(16.3738,48.2082), zoom=13, scale=2))

3


1
嗨,我在运行 ggmap(get_googlemap()) 时遇到以下错误: "Source : https://maps.googleapis.com/maps/api/staticmap?center=29.763284,-95.363271&zoom=10&size=640x640&scale=2&maptype=terrain&key=xxx Error in aperm.default(map, c(2, 1, 3)) : invalid first argument, must be an array In addition: Warning message: In get_googlemap() : HTTP 400 Bad Request" .. 我该怎么办? - JRBatHolmes

6

补充一下Roman Abashin的回答(很遗憾我无法发表评论):根据“?get_map()”,对于谷歌地图,“api_key =”参数不起作用。您需要使用“register_google()”函数,但截至2018年3月10日,它仅在ggmap的开发版本中提供,您可以通过以下方式获取:

devtools::install_github("dkahle/ggmap", ref = "tidyup")

然后,您还需要在Google账户上启用计费,不过每月使用的前100,000张地图应该是免费的。详情请参见此处:https://cloud.google.com/maps-platform/pricing/sheet/
(提示来自此处:https://github.com/dkahle/ggmap/issues/51

1
非常正确,谢谢!这是我尝试的一个粗略的逐步教程 - Roman
@ErnestScribbler,我扩展了我的答案,还包括教程。 - Roman

4

我不知道如何直接解决ggmap问题,但如果您愿意使用交互地图而不是静态地图,您可以使用我的googelway库。

library(googleway)

set_key("GOOGLE_MAP_KEY")

lat <- c(4,41) #India lat boundaries
lon <- c(68,99) #India long boundaries
center = c(mean(lat), mean(lon))

google_map(location = center, zoom = 6)

enter image description here


我开始遇到之前的同样问题。在 R 视图器中没有任何内容显示,这一次即使在浏览器中打开也无济于事。只是想让你知道。感谢你在这些年里通过你的软件包提供的所有帮助。 - M--
@Masoud - 请问在 JavaScript 控制台中是否有错误(在浏览器中,右键单击(地图外),选择“检查”,然后转到“控制台”选项卡)? - SymbolixAU
我没有带我的笔记本电脑,等我回去后会查看并报告。 - M--
1
@Masoud 奇怪,不过很高兴它现在又可以工作了。 - SymbolixAU

2

在@Roman的回答中补充一点,这是对我有效的代码:

if(!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/ggmap", ref = "tidyup")
library(ggmap)
register_google(key = "your_API_key") 
usa<- get_googlemap(location='united states', zoom=4,maptype = "hybrid")

如需更多信息,请参考github上的库页面:这里
希望能对你有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接