Leaflet Marker Map with Custom Icons and Clustering
library(leaflet)
library(sf)
## Warning: package 'sf' was built under R version 4.4.3
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
sample_data <- data.frame(
name = c("Location 1", "Location 2", "Location 3"),
lat = c(42.2808, 42.2750, 42.2900),
lng = c(-83.7430, -83.7400, -83.7500)
)
custom_icon <- makeIcon(
iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94
)
leaflet(sample_data) %>%
addTiles() %>%
addMarkers(
~lng, ~lat, popup = ~name,
icon = custom_icon,
clusterOptions = markerClusterOptions()
)