get_map没有传递API密钥(HTTP状态为'403 Forbidden')

Ven*_*nky 9 google-maps r google-maps-api-3 ggmap

我一直在R 中的get_map()函数(ggmap库)中遇到这个问题.

我的代码运行时无需source = "google"为几个月指定API密钥(for ).但是,代码在几周前停止了工作.我知道谷歌已经强制要求API密钥(或者他们可能在没有我用尽的api密钥的情况下允许一定数量的呼叫).

但是,即使在指定API密钥(从Google Cloud Platform获得)后,我的代码仍然以相同的方式运行.我甚至联系了谷歌云支持,但是他们说API密钥本身没有任何问题,他们可以在最后调用地图.

我怀疑该get_map()功能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
)
Run Code Online (Sandbox Code Playgroud)

以下是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'
Run Code Online (Sandbox Code Playgroud)

Rom*_*man 16

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


已更新:2018-12-24 for ggmap 2.7.904和当前的Google Cloud API

循序渐进的教程

1.更新到最新版本的ggmap

require(devtools)
devtools::install_github("dkahle/ggmap", ref = "tidyup")
Run Code Online (Sandbox Code Playgroud)

2.为Google Cloud Console中的所有API激活Google API密钥

在此输入图像描述

在此输入图像描述

3.加载ggmap和注册密钥

library(ggmap)
register_google(key = "...")     # copied directly from Google Console via 'copy' button
Run Code Online (Sandbox Code Playgroud)

4.绘制默认地图

ggmap(get_googlemap())          
Run Code Online (Sandbox Code Playgroud)

休斯顿

5.使用位置名称绘图(地理编码)

ggmap(get_map("Hannover, Germany"))
Run Code Online (Sandbox Code Playgroud)

如果你在这里收到错误(例如,Forbidden 403),你很可能没有为正确的API激活你的密钥.解决地理编码问题的教程

汉诺威

6.绘制经度和​​纬度

ggmap(get_map(location=c(16.3738,48.2082), zoom=13, scale=2))
Run Code Online (Sandbox Code Playgroud)

3


小智 6

只是添加到Roman Abashin的答案(遗憾的是我无法发表评论):根据'?get_map()','api_key ='参数不适用于Google地图.你需要使用'register_google()'函数,但截至03/10/18,它只在ggmap的开发版本中,你可以这样:

devtools::install_github("dkahle/ggmap", ref = "tidyup")
Run Code Online (Sandbox Code Playgroud)

然后,您还需要在自己的Google帐户中启用结算功能,不过您每月使用的前100,000张地图应该是免费的,请参阅此处:https://cloud.google.com/maps-platform/pricing/sheet/了解详情.

(从这里得到的提示:https://github.com/dkahle/ggmap/issues/51)


Sym*_*xAU 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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述