Mulai sekarangMulai gratis

A Map with a Narrower View

We can limit users' ability to pan away from the map's focus using the options argument in the leaflet() function. By setting minZoom anddragging, we can create an interactive web map that will always be focused on a specific area.

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

Alternatively, if we want our users to be able to drag the map while ensuring that they do not stray too far, we can set the maps maximum boundaries by specifying two diagonal corners of a rectangle.

You'll use dc_hq to create a map with the "CartoDB" provider tile that is centered on DataCamp's Belgium office.

Latihan ini merupakan bagian dari kursus

Interactive Maps with leaflet in R

Lihat Kursus

Instruksi latihan

  • Use a minimum zoom level of 12.
  • Set the dragging option to TRUE.
  • Use maximum bounds of .05 decimal degrees from the headquarters.

Latihan interaktif langsung praktik

Cobalah latihan ini dengan melengkapi kode contoh ini.

leaflet(options = leafletOptions(
                    # Set minZoom and dragging 
                    minZoom = ___, dragging = FALSE))  %>% 
  addProviderTiles("CartoDB")  %>% 
  
  # Set default zoom level 
  setView(lng = dc_hq$lon[2], lat = dc_hq$lat[2], zoom = 14) %>% 
  
  # Set max bounds of map 
  setMaxBounds(lng1 = dc_hq$lon[2] + ___, 
               lat1 = dc_hq$lat[2] + .05, 
               lng2 = dc_hq$lon[2] - ___, 
               lat2 = dc_hq$lat[2] - .05) 
Edit dan Jalankan Kode