2

I have some points that I want to be clustered in a L.MarkerClusterGroup, which works well:

var cluster = new L.MarkerClusterGroup(options);
features.foreach(function(feature) {
    var layer = L.marker(feature, {title: feature.name});
    cluster.addLayer(layer);
});

And some geofences that that I don't want clustered, in a drawable layer, which also works well:

var easel = L.FeatureGroup().addTo(map);
map.draw = new L.Control.Draw({edit: {featureGroup: easel}});

How can I add the cluster featureGroup to the editable featureGroup, so that the points get clustered and are editable?

I have tried adding the individual feature layers into both feature groups, but then I see both the cluster and all the individual points at the same time. The editing would have to occur only if you've zoomed in far enough to see an individual marker of a cluster.

Ideally, it would be great I could have a parent featureGroup, and have those two featureGroups as children then make the parent drawable. But I haven't seen any documentation to suggest this is possible:

// psuedo code only, may not even be possible
parent = L.FeatureGroup().addTo(map);
easel = L.FeatureGroup().addTo(map);
cluster = new L.MarkerClusterGroup(options);
easel.addTo(parent);
cluster.addTo(parent);
map.draw = new L.Control.Draw({edit: {featureGroup: parent}});

1 Answer 1

3

You can use the Leaflet.FeatureGroup.SubGroup library for layers under easel as featureGroup under another featureGroup layer.

Example:

    var map = L.map("map"),
    parentGroup = L.markerClusterGroup(options), // Could be any other Layer Group type.
    // arrayOfMarkers refers to layers to be added under the parent group as sub group feature
    mySubGroup = L.featureGroup.subGroup(parentGroup, arrayOfMarkers);

parentGroup.addTo(map);
mySubGroup.addTo(map);

Reference:
https://github.com/ghybs/Leaflet.FeatureGroup.SubGroup

2
  • From reading that github page, I can see that "Now adding the sub-group to the map adds clustered markers". I want one of the child groups to not cluster (the geofences) and the other subgroup to cluster (the markers). So this plugin may not help.
    – JK.
    Commented Feb 14, 2018 at 3:03
  • It is mentioned It should virtually be compatible with any LayerGroup plugin, not only MarkerCluster.
    – Shiko
    Commented Feb 14, 2018 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.