Integrates the LayerGroup Collision plugin into a Leaflet map, which hides overlapping markers and only displays the first added marker in a collision group. Markers must be static; dynamic changes, dragging, and deletions are not supported. The function transforms spatial data into GeoJSON format and uses `L.DivIcon`, allowing you to pass HTML content and CSS classes to style the markers.
Usage
addLayerGroupCollision(
map,
group = NULL,
className = NULL,
html = NULL,
margin = 5,
data = getMapData(map)
)
Arguments
- map
the map to add awesome Markers to.
- group
the name of the group. It needs to be single string.
- className
A single CSS class or a vector of CSS classes.
- html
A single HTML string or a vector of HTML strings.
- margin
defines the margin between markers, in pixels
- data
the data object from which the argument values are derived; by default, it is the
data
object provided toleaflet()
initially, but can be overridden
Examples
library(leaflet)
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
library(leaflet.extras2)
df <- sf::st_as_sf(atlStorms2005)
#> Loading required package: sp
df <- suppressWarnings(st_cast(df, "POINT"))
df$classes <- sample(x = 1:5, nrow(df), replace = TRUE)
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
leaflet::addLayersControl(overlayGroups = c("Labels")) %>%
addPolylines(data = sf::st_as_sf(atlStorms2005), label = ~Name) %>%
addLayerGroupCollision(
data = df, margin = 40,
html = ~ paste0(
'<div style="width: max-content; background-color: #cbc0c04f" class="custom-html">',
'<div class="title">', Name, "</div>",
'<div class="subtitle">MaxWind: ', MaxWind, "</div>",
"</div>"
),
className = ~ paste0("my-label my-label-", classes),
group = "Labels"
)