Skip to contents

addLayerGroupConditional

Usage

addLayerGroupConditional(map, groups = NULL, conditions = NULL)

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 where: - Each **name** is a JavaScript function (using JS) defining a condition for displaying a layer group. - Each **value** corresponds to a layer group name (or names) from the layers parameter. Example:


  condition = list(
    "(zoomLevel) => zoomLevel < 4" = "group1",
    "(zoomLevel) => zoomLevel >= 4 && zoomLevel < 6" = "group2",
    "(zoomLevel) => zoomLevel >= 6" = c("group3", "group4")
  )

See also

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)
  )