Leaflet.js - Finding out selected overlay

2k Views Asked by At

I do have two layer groups between the user can switch in a layer control panel. This is working fine so far.

How can I find out the currently selected overlay (0 == a or 1 == b)? I call the function that paints a layer group every two minutes (window.setTimeout("myFunction()", 120 * 1000);), and would like to make sure to "redraw" only the selected layer group.

This is how I add the control for the two overlays to the map:

var a = new L.LayerGroup();
var b = new L.LayerGroup();

var overlays = {
        "Option A": a,
        "Option B": b
    };

L.control.layers(overlays).addTo(map);
1

There are 1 best solutions below

1
IvanSanchez On

There are several methods. You can listen to the map events which add or remove layers; you can add more event handlers to your layer control panel.

My personal choice would be to call map.hasLayer(a), which returns a boolean and is quite self-explaining. See the hasLayer() documentation.