我在 R 的 get_map()
函数(ggmap
库)中遇到了这个问题。
我的代码运行了几个月,无需指定 API key (对于 source = "google"
)。然而,该代码在几周前停止工作。我了解到 Google 已强制要求 API key (或者可能他们允许在没有我用尽的 api key 的情况下进行一定数量的调用)。
但是,即使指定了 API key (从 Google Cloud Platform 获取),我的代码仍然以相同的方式运行。我什至联系了 Google Cloud 支持,但他们说 API key 本身没有任何问题,他们能够在最后调用 map 。
我怀疑 get_map()
函数在从 Google 调用 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
)
下面是 R 中的错误消息(注意 API key 没有被传递)
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'
最佳答案
您需要使用register_google(key = "..."
)在 R 的每个新 session 中。使用 api_key =
里面get_map()
通话无法进行。
已更新:2018 年 12 月 24 日,适用于 ggmap 2.7.904 和当前的 Google Cloud API
分步教程
1。更新到最新版本的ggmap
require(devtools)
devtools::install_github("dkahle/ggmap", ref = "tidyup")
2。在 Google Cloud Console 中为所有 API 激活您的 Google API key
您需要的 API:静态 map 和地理编码
在常规设置中启用计费。
3。加载ggmap并注册 key
library(ggmap)
register_google(key = "...") # copied directly from Google Console via 'copy' button
4。绘制默认 map
ggmap(get_googlemap())
5。带有位置名称的绘图(地理编码)
ggmap(get_map("Hannover, Germany"))
If you get an error here (e.g., Forbidden 403) you most probably have not activated your key for the right APIs. Tutorial to troubleshoot geocoding
6。经度和纬度绘图
ggmap(get_map(location=c(16.3738,48.2082), zoom=13, scale=2))
关于r - get_map 未传递 API key (HTTP 状态为 '403 Forbidden' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52565472/