【发布时间】:2019-03-05 01:11:55
【问题描述】:
我在 R 的 get_map() 函数(ggmap 库)中遇到了这个问题。
我的代码在几个月内无需指定 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'
【问题讨论】:
-
感谢@SymbolixAU。我稍后会检查这个。现在,我的问题通过使用 get_googlemap 而不是 get_map 得到了解决。当来源是谷歌时,似乎 get_map 不是为了接受 api_key 。 get_map 仅在源为 cloudmade 时才使用 api_key。
标签: r google-maps google-maps-api-3 ggmap