Adds customizable DivIcon markers to a Leaflet map. The function can accept either spatial data (lines or points) in the form of a Simple Feature (sf) object or numeric vectors for latitude and longitude coordinates. It allows for the application of custom HTML content and CSS classes to each marker, providing high flexibility in marker design.
Usage
addDivicon(
map,
lng = NULL,
lat = NULL,
layerId = NULL,
group = NULL,
popup = NULL,
popupOptions = NULL,
label = NULL,
labelOptions = NULL,
className = NULL,
html = NULL,
options = markerOptions(),
clusterOptions = NULL,
clusterId = NULL,
divOptions = list(),
data = getMapData(map)
)
Arguments
- map
The Leaflet map object to which the DivIcon markers will be added.
- lng
a numeric vector of longitudes, or a one-sided formula of the form
~x
wherex
is a variable indata
; by default (if not explicitly provided), it will be automatically inferred fromdata
by looking for a column namedlng
,long
, orlongitude
(case-insensitively)- lat
a vector of latitudes or a formula (similar to the
lng
argument; the nameslat
andlatitude
are used when guessing the latitude column fromdata
)- layerId
the layer id
- group
the name of the group the newly created layers should belong to (for
clearGroup
andaddLayersControl
purposes). Human-friendly group names are permitted–they need not be short, identifier-style names. Any number of layers and even different types of layers (e.g. markers and polygons) can share the same group name.- popup
a character vector of the HTML content for the popups (you are recommended to escape the text using
htmlEscape()
for security reasons)- popupOptions
A Vector of
popupOptions
to provide popups- label
a character vector of the HTML content for the labels
- labelOptions
A Vector of
labelOptions
to provide label options for each label. DefaultNULL
- className
A single CSS class or a vector of CSS classes to apply to the DivIcon markers.
- html
A single HTML string or a vector of HTML strings to display within the DivIcon markers.
- options
A list of extra options for the markers. See
markerOptions
for more details.- clusterOptions
if not
NULL
, markers will be clustered using Leaflet.markercluster; you can usemarkerClusterOptions()
to specify marker cluster options- clusterId
the id for the marker cluster layer
- divOptions
A list of extra options for Leaflet DivIcon.
- 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(sf)
library(leaflet)
library(leaflet.extras2)
# Sample data
df <- sf::st_as_sf(atlStorms2005)
df <- suppressWarnings(st_cast(df, "POINT"))
df <- df[sample(1:nrow(df), 50, replace = FALSE), ]
df$classes <- sample(x = c("myclass1", "myclass2", "myclass3"), nrow(df), replace = TRUE)
df$ID <- paste0("ID_", 1:nrow(df))
leaflet() %>%
addTiles() %>%
addDivicon(
data = df,
html = ~ paste0(
'<div class="custom-html">',
'<div class="title">', Name, "</div>",
'<div class="subtitle">MaxWind: ', MaxWind, "</div>",
"</div>"
),
label = ~Name,
layerId = ~ID,
group = "Divicons",
popup = ~ paste(
"ID: ", ID, "<br>",
"Name: ", Name, "<br>",
"MaxWind:", MaxWind, "<br>",
"MinPress:", MinPress
),
options = markerOptions(draggable = TRUE)
)