leaflet-panel-layers icon indicating copy to clipboard operation
leaflet-panel-layers copied to clipboard

addOverlay() - does not add layer._events, add and remove

Open sweco-sekrsv opened this issue 3 years ago • 0 comments

Hi!

I'm facing a problem with using addOverlay(). Using this function the added layer does not contain the leaflet layer._events. They can look like this:

_events:
add: [{…}]
remove: [{…}]

Since these are not added for example the leaflet events overlayadd and overlayremove does not get fired when a layer have been added through addOverlay()

is there any fix/workaround for this? I need to use the addOverlay() function.

Code that does not add the layer.events

var emptyLayer = new L.GeoJSON();
overlays = [{
        group: "Test group",
        collapsed: true,
        layers: []
    }

];

var layerControl = L.control.panelLayers(baseLayers, overlays, {
    collapsibleGroups: true,
    compact: true,
    collapsed: false
}).addTo(map);

layerControl.addOverlay({layer: emptyLayer, active: false}, 'Test addoverlay', 'Test group"');

Code that does add the layer._events (but I can't use this method since I need to add layers dynamically)

var emptyLayer = new L.GeoJSON();
overlays = [{
        group: "Test group",
        collapsed: true,
        layers: [{
            name: 'Test',
            layer: emptyLayer,
            active: false
        }]
    }

];

var layerControl = L.control.panelLayers(baseLayers, overlays, {
    collapsibleGroups: true,
    compact: true,
    collapsed: false
}).addTo(map);

Using the default Leaflet control does work (adds the layer._events):

var emptyLayer = new L.GeoJSON();
var layerControl = L.control.layers(null, null).addTo(map);
layerControl.addOverlay(emptyLayer,'Test');

sweco-sekrsv avatar Nov 12 '20 07:11 sweco-sekrsv