如何更改ggmap中的地图类型?

问题描述 投票:1回答:1

我无法更改ggmap的背景。当我尝试更改maptype时,它总是显示为terrain。我想将其更改为satellite。有什么建议么?这是我的代码:

 library(ggmap)
 # Long and Lat Coordinates
 Cuba_all <- data.frame("Longitude" = c(-79.79623, -79.42313, -79.01722, -80.29218, -80.50040, -80.51981, -80.36674, -79.87957, -79.66906, -79.76122, -80.26587, -79.91689, -80.10454, -80.22530, -80.12910, -79.98889, -79.84307, -79.81694, -80.22201, -80.48088, -80.44482, -80.29068, -80.36213, -80.50879, -80.29634), "Latitude" = c(22.06622, 22.14845, 22.20900, 22.05258, 22.30107, 22.88154, 22.70679, 22.53541, 22.39237, 22.35697, 21.91868, 22.08949, 21.83760, 22.10561, 22.11061, 22.02766, 22.04936, 22.37516, 22.56684, 22.44313, 22.44416, 22.50470, 22.75872, 22.35473, 22.49178))
 # Create Cuba Dimensions
 sbbox <- make_bbox(lon = Cuba_all$Longitude, lat = Cuba_all$Latitude, f = .1)
 sbbox
 # Grab Map of Cuba
 sq_map <- get_map(location = sbbox, source = "google", maptype = "satellite")
 # Plot Map
 ggmap(sq_map)
r ggmap terrain
1个回答
2
投票

@ 42的评论部分正确(据我所知)。但这不一定是您的API密钥,而是您的位置规范。 Google地图服务器希望在center处将位置指定为lon / lat,再加上缩放比例。如果您以边界框格式提交位置,则get_map() 静默地决定获取Stamen地形图;对我来说,这似乎是get_map()中的错误(或“不诚实”),并且是two问题列表上至少issues ggmap的主题。

在任何情况下,当我指定正确的lon / lat向量,并使用缩放倍数直到正确为止(除了反复试验,我都不知道该怎么做...),它可以我。

sm <- with(Cuba_all,c(lon=median(Longitude),lat=median(Latitude)))
sq_map1 <- get_map(location = sm, zoom=9,
                   source = "google", maptype = "satellite")
ggmap(sq_map1)+
    geom_point(data=Cuba_all,aes(x=Longitude,y=Latitude),colour="red")
ggsave("cuba.png")

enter image description here


get_map()的更多挖掘使其看起来像是开发人员的疏忽。它似乎已在ggmap的开发版本中修复,但尚未在CRAN版本中使用(请参见注释here。)

此代码块:

if (is.numeric(location) && length(location) == 4) {
    location_type <- "bbox"
    location_stop <- FALSE
    source <- "stamen"
    maptype <- "terrain"
    ## ...

检测到该位置已作为边界框给出,并自动将源设置为“雄蕊”,将地图类型设置为“地形”。稍后有一段代码,如果您通过source=="google"传递了一个边界框,它看起来应该发出警告,但它永远无法到达(因为此时源已被更改为“雄蕊”)...] >

if (source == "google") {
    if (location_type == "bbox") {
        warning("bounding box given to google - spatial extent only approximate.", 
            call. = FALSE, immediate. = TRUE)
        message("converting bounding box to center/zoom specification. (experimental)")
        user_bbox <- location
        location <- c(lon = mean(location[c("left", "right")]), 
            lat = mean(location[c("bottom", "top")]))
    }
© www.soinside.com 2019 - 2024. All rights reserved.