设置默认地图视图

在 R 中使用 leaflet 构建交互式地图

Rich Majerus

Vice President of Strategy & Planning, Queens University of Charlotte

R 中的地理编码

  • 常用方法:使用 ggmap 包的 geocode() 函数
  • 返回地址或地名的纬度和经度
library(ggmap)

geocode("350 5th Ave, New York, NY 10118")
Information from URL : https://maps.googleapis.com/maps/api/geocode/...

lon       lat
-73.98575 40.74856
在 R 中使用 leaflet 构建交互式地图

R 中的地理编码 II

geocode(location, 
        output = c("latlon", "latlona", "more", "all"),
        source = c("google", "dsk"))

geocode("Colby College", output = "more", source = "google")
lon       lat      type          loctype  
-69.66264 44.56387 establishment rooftop  

address
4000 mayflower hill dr, waterville, me 04901, usa
在 R 中使用 leaflet 构建交互式地图

设置默认地图视图

setView()

leaflet() %>% 
 addTiles() %>% 
 setView(lng = -73.98575, 
         lat = 40.74856, 
         zoom = 13)

setView() 示例

fitBounds()

leaflet() %>% 
 addTiles() %>% 
 fitBounds(
  lng1 = -73.910, lat1 = 40.773, 
  lng2 = -74.060, lat2 = 40.723)

fitBounds() 示例

在 R 中使用 leaflet 构建交互式地图

保持聚焦

leaflet(options = leafletOptions(dragging = FALSE,
                                 minZoom = 14, 
                                 maxZoom = 18))  %>% 
    addProviderTiles("CartoDB")  %>% 
    setView(lng = -73.98575, lat = 40.74856, zoom = 18)

在 R 中使用 leaflet 构建交互式地图

恢复焦点

leaflet()  %>% 
     addTiles()  %>% 
     setView(lng = -73.98575, lat = 40.74856, zoom = 18) %>% 
     setMaxBounds(lng1 = -73.98575, lat1 = 40.74856, 
                  lng2 = -73.98575, lat2 = 40.74856) 

setMaxBounds() 示例

在 R 中使用 leaflet 构建交互式地图

¡Vamos a practicar!

在 R 中使用 leaflet 构建交互式地图

Preparing Video For Download...