ComeçarComece de graça

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.

Este exercício faz parte do curso

Interactive Maps with leaflet in R

Ver curso

Instruções do exercício

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

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

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) 
Editar e executar o código