get_map发生了什么?

时间:2014-07-25 22:06:56

标签: r ggmap

我今天开始时遇到大约230个地图请求googlemaps与get_map之前我收到错误(丢失了原始错误)。我的第一个假设只是我使用它超出api的限制,但我尝试使用开放的街道地图api作为源,我得到的错误消息仍然看起来像get_map正在使用googlemaps。这是怎么回事?我重新启动计算机并尝试重新创建错误。当我进入源代码中列出的url时,我没有得到我的地图,我得到一个小图像,看起来我已经过了我的api用法。但我把我的来源设为osm。

> library(ggmap)
Loading required package: ggplot2
Warning message:
package ‘ggmap’ was built under R version 3.1.1 
Loading required package: sp
> get_map(source = "osm")
Error in download.file(url, destfile = destfile, quiet = !messaging, mode = "wb") : 
  cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=29.763284,-95.363271&zoom=10&size=%20640x640&maptype=terrain&sensor=false'
In addition: Warning messages:
1: package ‘sp’ was built under R version 3.1.1 
2: In download.file(url, destfile = destfile, quiet = !messaging, mode = "wb") :
  cannot open: HTTP status was '403 Forbidden'
> detach(sp)
Error in detach(sp) : invalid 'name' argument
> detach("sp")
Error in detach("sp") : invalid 'name' argument
> get_map(source = "osm")
Error in download.file(url, destfile = destfile, quiet = !messaging, mode = "wb") : 
  cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=29.763284,-95.363271&zoom=10&size=%20640x640&maptype=terrain&sensor=false'
In addition: Warning message:
In download.file(url, destfile = destfile, quiet = !messaging, mode = "wb") :
  cannot open: HTTP status was '403 Forbidden'

这是我设置的原始循环以获取我的地图图像。

for(i in seq(levels(named_trails$TRAIL_NAME))) {
one_trail <- named_trails[which(named_trails$TRAIL_NAME == levels(named_trails$TRAIL_NAME)[i]),]
map_center <- c(min(one_trail$long) - 0.02, min(one_trail$lat) - 0.02, max(one_trail$long) + 0.02, max(one_trail$lat) + 0.02)
anc_map <- get_map("anchorage, AK", source = "stamen")
p <- ggmap(anc_map) +
    geom_line(data = one_trail, aes(x = long, y = lat, color = TRAIL_NAME)) +
    ggtitle(levels(named_trails$TRAIL_NAME)[i]) +
    theme(legend.position="none")
ggsave(paste(i, ".png", sep = ""))
Sys.sleep(4)
}

windows 7 x86 R 3.1.0 Rstudio版本0.98.932

1 个答案:

答案 0 :(得分:3)

问题似乎是当您指定source="osm"但位置类型不是边界框时,它会调用get_googlemap以获取lat / lon位置,然后再拉出地图来自openstreetmap。因此,在使用source="osm"时,您需要确保使用纬度/经度坐标传递长度为4的向量,以避免对Google Maps API的任何调用。似乎OSM无法将“anchorage,AK”等内容转换为经度和纬度值。