How can I determine when an overlay in the LayerGroup has been switched on/off?

5,221 views
Skip to first unread message

Patrick Doody

unread,
Jan 23, 2013, 10:11:29 AM1/23/13
to leafl...@googlegroups.com
I have a set of overlay layers in an L.layers group. When they are toggled on/off, I need various things to happen. I have yet to find a way to determine if these overlays are on/off. There are no methods to check for visibility of the layer groups, or on the markers contained in each of these groups. Is there anything I can do to check if an overlay group in the layers control is on/off, short of navigating through the DOM to get the value of the checkbox?

Thanks,
Patrick

Stephan Meijer

unread,
Jan 23, 2013, 10:45:17 AM1/23/13
to leafl...@googlegroups.com
Just from the top of my head, please check and validate!
 
var myLayer = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png', {
    key: 'API-key',
    styleId: 997
}).addTo(map);

map.on('layerremove', function(event) {
     if(event.layer == myLayer) {
         // do your events.
     }
});

map.on('layeradd', function(event) {
     if(event.layer == myLayer) {
         // do your events.
     }
});

map.addLayer(myLayer);


Op woensdag 23 januari 2013 16:11:29 UTC+1 schreef Patrick Doody het volgende:

Stephan Meijer

unread,
Jan 23, 2013, 10:49:26 AM1/23/13
to leafl...@googlegroups.com
Oh, and checking if an layer is turned on in the Layer Control, is equal to checking if it exists on the map. 

map.hasLayer(myLayer);


Op woensdag 23 januari 2013 16:45:17 UTC+1 schreef Stephan Meijer het volgende:

Patrick Doody

unread,
Jan 23, 2013, 10:56:17 AM1/23/13
to leafl...@googlegroups.com
Perfect! HasLayer() is exactly what I needed. I missed this! Thanks Stephan.

Arnie Shore

unread,
Jan 23, 2013, 11:09:21 AM1/23/13
to leafl...@googlegroups.com
Great! I'd been looking for a layer-control event, and this meets my needs.

AS

On 1/23/13, Stephan Meijer <stephan...@gmail.com> wrote:
> Just from the top of my head, please check and validate!
>
>
> var myLayer = ...
Reply all
Reply to author
Forward
0 new messages