2
votes

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!

1

1 Answers

0
votes

You could just do what you have done plus creating fake layers in overlays part and removing your main layer groups from it. Add your main layer groups in your cluster. Then you can simply detect which layer is removed or added with the layer events of the map like the following. So, all your markers are in the one cluster and you can remove or add your layers.

note that, this is java code, translate it to your own.

map.on("overlayadd", (e) =>{
  if (e.layer === fake_layer_4_5) markersCluster.addLayer(main_layer_4_5);
  if (e.layer === fake_layer_5_6) markersCluster.addLayer(main_layer_5_6);
});
map.on("overlayremove", (e) =>{
  if (e.layer === fake_layer_4_5) markersCluster.removeLayer(main_layer_4_5);
  if (e.layer === fake_layer_5_6) markersCluster.removeLayer(main_layer_5_6);
});