Skip to content Skip to sidebar Skip to footer

Display Layers At Certain Zoom Levels In R Leaflet

I am working on an interactive map with the R package 'leaflet'. I would like to change automatically the visible layers depending on the zoom level. For example, I would like to h

Solution 1:

The group argument in most of the "addElement()" type functions becomes very important for managing how a map works. I recommend it, and you can do a lot of neat stuff by carefully thinking about how you group your data.

By calling groupOptions(), you can set the zoom levels for whatever layers you like. Below I have added the zoom levels you specified, but feel free to play around with it to adjust it to your needs.

#Creating a leaflet object with points and polygons

leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(lng=quakes$long,
                   lat=quakes$lat,
                   col="blue",
                   radius=3,
                   stroke=FALSE,
                   fillOpacity = 0.7,
                   #options = markerOptions(minZoom=15, maxZoom=20), # Oldcode
                   group = "Quake Points") %>%                       # Newcode
  addPolygons(data= oceania,
              col="red") %>%                        
  groupOptions("Quake Points", zoomLevels = 15:20)                   # Newcode

Post a Comment for "Display Layers At Certain Zoom Levels In R Leaflet"