1

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?

  • As your questions are very programming-oriented, you may consider switching to Stack Overflow, even though posting these questions here is also legitimate. Please provide more context about the objects in your code, what you try to achieve and why, or you may end up with many unanswered posts. E.g. in this case, what is "input", what is "control"? – ghybs Nov 30 '15 at 4:53
  • ghybs thank you for your correct. I will try to learn how to ask a question. – Hu Kenneth Nov 30 '15 at 5:14
3

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 or addOverlay) are referenced in this._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 with input.layerId = L.stamp(layer), then retrieve it when needed by reading that id and getting the corresponding reference:

_onInputClick: function (event){
    var id = event.currentTarget.layerId;
    var layer = this._layers[id];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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