我的代码运行了几个月,无需指定API密钥(对于source =“google”).但是,代码在几周前停止了工作.我知道谷歌已经强制要求API密钥(或者他们可能在没有我用尽的api密钥的情况下允许一定数量的呼叫).
但是,即使在指定API密钥(从Google Cloud Platform获得)后,我的代码也会以相同的方式继续运行.我甚至联系了谷歌云支持,但是他们说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'您需要在R的每个新会话中使用register_google(key =“…”).在get_map()调用中使用api_key =不起作用.
已更新:2018-12-24 for 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密钥
> Link for more info on how to get an API key
> Direct Link to Google Cloud Platform Console
> Direct Link to Google Maps API Pricing Information
>您需要的API:地图静态和地理编码
>在常规设置中启用结算.
3.加载ggmap和注册密钥
library(ggmap) register_google(key = "...") # copied directly from Google Console via 'copy' button
4.绘制默认地图
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. 07006
6.绘制经度和纬度
ggmap(get_map(location=c(16.3738,48.2082), zoom=13, scale=2))