Creates a Leaflet Feature Group that adds its child layers into a parent group when added to a map (e.g. through L.Control.Layers).
Typical usage is to dynamically add/remove groups of markers from Marker Cluster.
Leaflet.markercluster plugin provides beautiful animated Marker Clustering functionality.
Size: 2 kB minified, < 1 kB gzipped.
Requires Leaflet ^1.0.0
Optional: Leaflet.markercluster plugin
- SubGroup with standard L.Control.Layers and MarkerCluster
- SubGroup with standard L.Control.Layers to display Nested Groups
<!-- after Leaflet script --> <script src="leaflet.featuregroup.subgroup.js"></script>
var map = L.map("map"), parentGroup = L.markerClusterGroup(options), // Could be any other Layer Group type. // This is where the magic happens! mySubGroup = L.featureGroup.subGroup(parentGroup, arrayOfMarkers); parentGroup.addTo(map); mySubGroup.addTo(map);
Now adding the sub-group to the map adds clustered markers!
It should virtually be compatible with any LayerGroup plugin, not only MarkerCluster.
Installing the sub-plugin
- Download the "
leaflet.featuregroup.subgroup.js" file from the
- Place the file alongside your page.
- Add the
scripttag (see Quick Guide > HTML) to your page after Leaflet script.
You can alternatively use the free unpkg CDN service, but keep in mind that it "is a free, best-effort service and cannot provide any uptime or support guarantees".
<!-- After Leaflet script --> <script src="https://firstname.lastname@example.org/dist/leaflet.featuregroup.subgroup.js"></script>
Add this package to your project:
$ npm install leaflet.featuregroup.subgroup --save
Lnamespace, so you do not need to store it into a local variable or import a namespace.
require("leaflet.featuregroup.subgroup"); // Or with ES6: import "leaflet.featuregroup.subgroup";
Simply use the
L.featureGroup.subGroup factory instead of your regular
var mySubGroup = L.featureGroup.subGroup(parentGroup); mySubGroup.addTo(map);
Do not forget to add the parent group to your map.
||Creates a sub-group with events, optionally given a parent group and an initial array of child layers.|
||Changes the parent group into which child markers are added to / removed from.|
||Removes the current sub-group from map before changing the parent group. Re-adds the sub-group to map if it was before changing.|
||Returns the current parent group.|
If you change the parent group while the sub-group and/or its child markers are still on map, unexpected behaviour of the previous and/or new parent groups can happen.
Make sure the sub-group and its child layers are removed from map before changing the parent group, or use the
setParentGroupSafe method instead.
Leaflet.FeatureGroup.SubGroup is distributed under the BSD 2-clause "Simplified" License, like Leaflet.