获取具有指定边界坐标的地图

时间:2014-09-03 05:34:15

标签: r google-maps ggplot2 ggmap rgooglemaps

我想从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") 

问题是只有中心作为参数传递,因此我看到的整个图像取决于缩放级别。所以,我无法理解我得到的图像的边界是什么。我想要做的是使用我定义的坐标精确限制图像。这是否可行(也可以使用其他地图包)?

3 个答案:

答案 0 :(得分:26)

这是一种方法。首先,您获得具有特定缩放的地图。然后,在绘制图形时添加lon和lat限制,可以使用scale_x_continuousscale_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

enter image description here

答案 1 :(得分:7)

另一种选择是使用OpenStreetMap作为地图的来源。使用Call to undefined function dl() 包中的get_map函数,可以在使用OpenStreetMap作为源时指定地图的边界。用:

ggmap

你得到:

enter image description here

但是,此方法不适用于GoogleMaps。使用GoogleMaps作为源指定边界将给出以下警告:

  

警告:仅为google提供边界框 - 空间范围   近似。将边界框转换为中心/缩放规范。   (实验)

使用OpenStreetMap的一个缺点是您无法访问卫星图像。

答案 2 :(得分:3)

实际互动式Google地图的另一种方式是使用我的googleway

library(googleway)

lat <- c(44.49,44.5)                
lon <- c(11.33,11.36)  
zoom <- 14   

mapKey <- 'your_api_key'

google_map(location = c(mean(lat), mean(lon)), zoom = zoom, key = mapKey)

enter image description here

作为谷歌地图,标准配备卫星想象力

enter image description here