For a project I'm currently working on, I'm trying to add multiple marker layers to a Leaflet map, so the end-user can choose which markers he or she wants to see. As there are a lot of markers in total, I also want to cluster these together up to a certain zoom level. The problem is, however, that when I divide my markers over several layer groups, they are all clustered seperately.
Specifically, when I plot the markers as one layer group, using clusterOptions in the call to addMarkers, clustering works as I want it to. When I use multiple layer groups for my makers in order to give the end-user the option to pick which markers he/she wants to see, clusters are made within each layer group, whereas I want them all to be clustered together.
To make this more tangible, I came up with the following reproducible example:
# Library
library(leaflet)
library(dplyr)
# Data
df <- quakes
# Plot
leaflet(df) %>%
setView(lng=181.462, lat=-23.64275, zoom=4) %>%
addProviderTiles("Stamen.Terrain") %>%
addCircleMarkers(~long, ~lat, weight=2, color="Red", radius=2,
data=filter(df, mag>=4 & mag<5),
clusterOptions=markerClusterOptions(disableClusteringAtZoom=6),
group="Magnitude between 4 and 5") %>%
addCircleMarkers(~long, ~lat, weight=2, color="Blue", radius=2,
data=filter(df, mag>=5 & mag<6),
clusterOptions=markerClusterOptions(disableClusteringAtZoom=6),
group="Magnitude between 5 and 6")%>%
addLayersControl(
overlayGroups=c("Magnitude between 4 and 5", "Magnitude between 5 and 6"),
options=layersControlOptions(collapsed=FALSE)
)
If you run this code in R, you will get a map which shows earthquakes in the vicinity of Fiji. I added two layer groups to show earthquakes with different magnitudes: one group with magnitudes between 4 and 5 and one group with magnitudes between 5 and 6. I also added clusters so the map isn't flooded with markers, but similarly to my project at work, clusters are made for each individual layer group. This results in seperate clusters which can be right next to eachother, whereas I want these to be put together in one cluster.
So what it comes down to is that I want all markers (from the multiple layer groups) to be put in the same cluster group, so they are all clustered together, instead of clustered seperately for each layer group. In other words I want to retain multiple marker groups for end-user options, but I want all these marker groups to be in the same cluster group, so they are clustered together when selected.
Any help would be very much appreciated!