使用ggmap创建映射和持久性错误R

使用ggmap创建映射和持久性错误R,r,google-maps,openstreetmap,ggmap,R,Google Maps,Openstreetmap,Ggmap,我有一段代码突然停止了工作。就这么简单。我找到了一些模糊的答案,但对我来说没有任何帮助 library(ggmap) myLocation <- c(21.5, -18.5, 34, -8) myMap <- get_map(location=myLocation, source="google", maptype="terrain", crop=FALSE, color="bw") 尝试了这句话,但没有帮助: options(download.

我有一段代码突然停止了工作。就这么简单。我找到了一些模糊的答案,但对我来说没有任何帮助

library(ggmap)
myLocation <- c(21.5, -18.5, 34, -8)

myMap <- get_map(location=myLocation,
                 source="google", maptype="terrain", crop=FALSE, color="bw")
尝试了这句话,但没有帮助:

options(download.file.method = "curl")
如果我尝试osm-还存在以下问题:

osmmap <- get_openstreetmap(bbox = c(left = 21.5, bottom = -18.5, right =
                                   34, top = 8), scale = 7, color = c("bw"))
Error: map grabbing failed - see details in ?get_openstreetmap.
In addition: There were 16 warnings (use warnings() to see them)

osmmap我在使用get_map(403禁止和超过查询限制)下载地图时遇到类似问题-我通过使用dput将地图保存在计算机上和dget加载地图来解决

map <- get_map(location = myLocation, source="google", maptype="terrain", crop=FALSE, color="bw")
dput(map, file = "myMaps")
map <- dget(file = "myMaps")

map所以这里有两个问题

  • ggmap
    使用谷歌地图作为标准地图源。
    ggmap
    正在连接的谷歌地图API需要注册的静态地图API密钥。您可以获得一个(免费)API密钥,不过请注意,您必须注册计费才能使用一个(如果您不映射API请求,则会返回一个错误)。设置好后,您需要通过
    register\u google(key=“…”)
  • 正在连接到lon/lat坐标(例如,location=c(-75.1636077,39.9524175))的Google Maps API需要完全运行
  • 因此,您的完整代码是:

    library(rjson)
    library(digest)
    library(glue)
    library(devtools)
    if(!requireNamespace("devtools")) install.packages("devtools")
    devtools::install_github("dkahle/ggmap", ref = "tidyup")
    library(ggmap)
    
    register_google(key = "...",  # your Static Maps API key
                    account_type = "standard")
    
    map <- get_map(location = c(-75.1636077,39.9524175), zoom = 13)
    
    ggmap(map)
    
    库(rjson)
    图书馆(摘要)
    图书馆(胶水)
    图书馆(devtools)
    如果(!requireNamespace(“devtools”))安装.packages(“devtools”)
    devtools::安装github(“dkahle/ggmap”,ref=“tidyup”)
    图书馆(ggmap)
    注册_google(key=“…”,#您的静态地图API密钥
    账户类型=“标准”)
    
    map Google现在要求用户注册并使用api密钥,请参见此。OSM的相同功能似乎非常有用,所以它不是run@CarlosBonilla感谢您的回复!如何做到这一点?如何注册和使用api密钥?我看不到你指的那篇文章的方向。非常感谢您的帮助。遗憾的是,我无法首先创建
    map
    对象,因此
    dput()
    无法工作
    myLocation <- c(21.5, -18.5, 34, -8)
    
    getMap <- function(loc){
      map <- get_map(location = loc, source="google", maptype="terrain", crop=FALSE, color="bw")
      i <<- 0
      return(map)
    }
    
    i <- -1
    c <- 0 # avoid infinite loop
    
    while(i < 0 & c < 20){
      tryCatch(map <- getMap(myLocation), error = function(w){
        i <- -1
      })
      c <- c+1
    }
    
    library(rjson)
    library(digest)
    library(glue)
    library(devtools)
    if(!requireNamespace("devtools")) install.packages("devtools")
    devtools::install_github("dkahle/ggmap", ref = "tidyup")
    library(ggmap)
    
    register_google(key = "...",  # your Static Maps API key
                    account_type = "standard")
    
    map <- get_map(location = c(-75.1636077,39.9524175), zoom = 13)
    
    ggmap(map)