View source: R/layerviewcontrol.R
customizeLayersControl | R Documentation |
This function enables customization of an existing layers control in a leaflet
map by adding custom views, home buttons,
opacity controls, and legends. It enhances the functionality of a layers control created with leaflet
or leaflet.extras
. It also allows to customize the layersControl appearance via CSS.
customizeLayersControl(
map,
view_settings,
home_btns = FALSE,
home_btn_options = list(),
setviewonselect = TRUE,
opacityControl = list(),
includelegends = TRUE,
addCollapseButton = FALSE,
layersControlCSS = list(),
increaseOpacityOnHover = FALSE
)
map |
A |
view_settings |
A list specifying the view settings for each layer. Each list element should contain either:
|
home_btns |
Logical. If |
home_btn_options |
A list of options to customize the home button appearance and behavior. Possible options include:
|
setviewonselect |
Logical. If |
opacityControl |
A list specifying the opacity control settings for each layer. Each list element should contain:
|
includelegends |
Logical. If |
addCollapseButton |
Logical. If True a button will be added on top of the
LayersControl which, when clicked, will expand/collapse the view. This is mainly
relevant when the original Control was set to |
layersControlCSS |
a list of valid CSS key-value pairs to modify the appearance of the layersControl. |
increaseOpacityOnHover |
Logical. If |
This function generates JavaScript that listens for overlayadd
or baselayerchange
events
and automatically sets the view or zoom level according to the specified view_settings
.
If home_btns
is enabled, a home button is added next to each layer in the layer control.
When clicked, it zooms the map to the predefined view of that layer.
The opacity control slider allows users to adjust the opacity of layers. The legend will be appended
to the corresponding layer control, matched by the layer's group name.
A modified leaflet
map object with extended layers control including view controls, home buttons, opacity controls, and legends.
library(sf)
library(leaflet)
library(leafem)
# Example data ##########
breweries91 <- st_as_sf(breweries91)
lines <- st_as_sf(atlStorms2005)
polys <- st_as_sf(leaflet::gadmCHE)
# View settings ##########
view_settings <- list(
"Base_tiles1" = list(
coords = c(20, 50),
zoom = 3
),
"Base_tiles2" = list(
coords = c(-110, 50),
zoom = 5
),
"breweries91" = list(
coords = as.numeric(st_coordinates(st_centroid(st_union(breweries91)))),
zoom = 8
),
"atlStorms2005" = list(
coords = as.numeric(st_bbox(lines)),
options = list(padding = c(110, 110))
),
"gadmCHE" = list(
coords = as.numeric(st_bbox(polys)),
options = list(padding = c(2, 2)),
fly = TRUE
)
)
# Opacity control settings ##########
opacityControl <- list(
"breweries91" = list(
min = 0,
max = 1,
step = 0.1,
default = 1,
width = '100%',
class = 'opacity-slider'
)
)
# Legends ##########
legends <- list(
"breweries91" = "<div>Legend for breweries</div>"
)
leaflet() |>
## Baselayer
addTiles(group = "Base_tiles1") |>
addProviderTiles("CartoDB", group = "Base_tiles2") |>
## Overlays
addCircleMarkers(data = breweries91, group = "breweries91") |>
addPolylines(data = lines, group = "atlStorms2005") |>
addPolygons(data = polys, group = "gadmCHE") |>
## LayersControl
addLayersControl(
baseGroups = c("Base_tiles1", "Base_tiles2"),
overlayGroups = c("breweries91", "atlStorms2005", "gadmCHE"),
options = layersControlOptions(collapsed = FALSE, autoZIndex = TRUE)
) |>
## Customize Layers Control
customizeLayersControl(
view_settings = view_settings,
home_btns = TRUE,
home_btn_options = list(
"Base_tiles1" = list(text = '🏡', cursor = 'ns-resize', class = 'homebtn'),
"Base_tiles2" = list(text = '❤️', cursor = 'pointer'),
"atlStorms2005" = list(text = '🌎', cursor = 'all-scroll'),
"breweries91" = list(text = '🌎', styles = 'background-color: red'),
"gadmCHE" = list(text = '🌎', styles = 'float: none;')
),
opacityControl = opacityControl,
includelegends = TRUE,
addCollapseButton = TRUE,
layersControlCSS = list("opacity" = 0.6),
increaseOpacityOnHover = TRUE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.