How to get selected layers in control.layers?

Is there a way to select all selected layers in a .layers control with the letter api? I can do this using jquery, like this is $ ('Leaflet-Control-layers-selector: checked') but maybe there is an api? enter image description here Thanks

+5
source share
1 answer

There is no API for this, but you can easily create it yourself:

// Add method to layer control class L.Control.Layers.include({ getActiveOverlays: function () { // Create array for holding active layers var active = []; // Iterate all layers in control this._layers.forEach(function (obj) { // Check if it an overlay and added to the map if (obj.overlay && this._map.hasLayer(obj.layer)) { // Push layer to active array active.push(obj.layer); } }); // Return array return active; } }); var control = new L.Control.Layers(...), active = control.getActiveOverlays(); 
+4
source

Source: https://habr.com/ru/post/1268458/


All Articles