lbe*_*gni 18 google-maps r ggplot2 rgooglemaps ggmap
我想从R获得带有RgoogleMaps的地图,具有特定的坐标边界.
我可以调用的是GetMap,并指定一个中心,我必须添加一个缩放级别.一切都很好,除了我没有得到一个与我选择的坐标有界的图像地图.
这是一个例子:
lat <- c(44.49,44.5)
lon <- c(11.33,11.36)
center = c(mean(lat), mean(lon))
zoom <- 14
mmap <- GetMap(center = center, zoom=zoom, maptype= "satellite", destfile = "m.png")
Run Code Online (Sandbox Code Playgroud)
问题是只有中心作为参数传递,因此我看到的整个图像取决于缩放级别.所以,我无法理解我得到的图像的边界是什么.我想要做的是使用我定义的坐标精确限制图像.这是可能的(还有其他地图包)吗?
jaz*_*rro 28
这是一种方式.首先,您获得具有特定缩放的地图.然后,在绘制图形时添加lon和lat限制,可以使用scale_x_continuous和scale_y_continuous.
library(ggmap)
library(ggplot2)
### Set a range
lat <- c(44.49, 44.5)
lon <- c(11.33, 11.36)
### Get a map
map <- get_map(location = c(lon = mean(lon), lat = mean(lat)), zoom = 14,
maptype = "satellite", source = "google")
### When you draw a figure, you limit lon and lat.
foo <- ggmap(map)+
scale_x_continuous(limits = c(11.33, 11.36), expand = c(0, 0)) +
scale_y_continuous(limits = c(44.49, 44.5), expand = c(0, 0))
foo
Run Code Online (Sandbox Code Playgroud)

另一个选择是使用OpenStreetMap作为地图的源.使用包中的get_map函数ggmap,可以在使用OpenStreetMap作为源时指定地图的边界.附:
mmap <- get_map(location = c(11.33,44.49,11.36,44.50), source = "osm")
ggmap(mmap)
Run Code Online (Sandbox Code Playgroud)
你得到:
但是,此方法不适用于GoogleMaps.使用GoogleMaps作为源指定边界将给出以下警告:
警告:给谷歌的边界框 - 空间范围只是近似值.将边界框转换为中心/缩放规范.(实验)
使用OpenStreetMap的一个缺点是您无法访问卫星图像.