Can I get the current layer in control's event?
if (obj.overlay) {
input = document.createElement('input');
input.type = 'checkbox';
input.defaultChecked = checked;
} else {
input = this._createRadioElement('leaflet', checked);
}
L.DomEvent.on(input, 'click', this._onInputClick, this);
_onInputClick:function(e){
var id =e. currentTarget;
}
When user click the checkbox, it will trigger _onInputClick event. This control will be created under a leaflet layer. I want to get the current layer. For example, I can get the current control like e.currentTarget. Is it possible to get the current layer by Leaflet library?
Best Answer
I guess you are building a Leaflet Control based on
L.Control.Layers
?In that case, you can simply continue using a similar scheme as L.Control.Layers to store then retrieve a reference to the corresponding layer.
L.Control.Layers stores just the layer's leaflet_id as a property of the input element:
input.layerId = L.stamp(obj.layer);
All layers known by the Layers Control (i.e. added at instantiation or later on by
addBaseLayer
oraddOverlay
) are referenced inthis._layers
object.On input click event, the Layers Control loops through all inputs and retrieves the corresponding layers(s):
obj = this._layers[input.layerId];
So in your case, if you use the same scheme, store reference to all layers of interest in
this._layers
, attach the id the input refers to withinput.layerId = L.stamp(layer)
, then retrieve it when needed by reading that id and getting the corresponding reference: