addLayerGroupConditional
Arguments
- map
A map widget object created from
leaflet
.- groups
A character vector of layer group names already added to the map. These layer groups will be conditionally displayed based on the specified
conditions
.- conditions
A named list of conditions for displaying layer groups. Each list element should have:
A name: a JavaScript function as string that defines the condition.
A value: the layer group name(s) from the
layers
parameter to be shown when the condition evaluates toTRUE
.
Example:
See also
https://github.com/Solfisk/Leaflet.LayerGroup.Conditional for more details about the plugin.
Other LayerGroupConditional Plugin:
clearConditionalLayers()
,
removeConditionalLayer()
Examples
library(leaflet)
library(sf)
library(leaflet.extras2)
breweries91 <- st_as_sf(breweries91)
lines <- st_as_sf(atlStorms2005)
polys <- st_as_sf(leaflet::gadmCHE)
groups <- c("atlStorms", "breweries", "gadmCHE")
leaflet() %>%
addTiles() %>%
# leafem::addMouseCoordinates() %>%
addPolylines(data = lines, label = ~Name, group = groups[1]) %>%
addCircleMarkers(data = breweries91, label = ~brewery, group = groups[2]) %>%
addPolygons(data = polys, label = ~NAME_1, group = groups[3]) %>%
addLayerGroupConditional(
groups = groups,
conditions = list(
"(zoomLevel) => zoomLevel < 4" = groups[1],
"(zoomLevel) => zoomLevel >= 4 & zoomLevel < 6 " = groups[2],
"(zoomLevel) => zoomLevel >= 6" = groups[3]
)
) %>%
hideGroup(groups) %>%
addLayersControl(
overlayGroups = groups,
options = layersControlOptions(collapsed = FALSE)
)