2
votes

I have problem in leaflet events. I want use this event: [link]https://leafletjs.com/reference-1.6.0.html#map-overlayadd this event over lay add (Fired when an overlay is selected through the layer control).

I want when selected any layers through the layer control save layer name in var Name. How can I this? I have 3 layers.

2

2 Answers

1
votes
var name;
map.on('overlayadd', function(e){
   name = e.name;
   console.log(name);
})
1
votes

This code work for me. Thanks from Falke Design.

For baselayer:

var SelectedLayer='OSM';  // default value 
map.on('baselayerchange', function(eo) {
  if (eo.name === 'ge3') {
    SelectedLayer='ge3';}
  if (eo.name === 'ge2') {
    SelectedLayer='ge2';

  } else if (eo.name !== 'ge2' && eo.name !== 'ge3' ) {
    SelectedLayer=eo.name;
  }
  console.log('SelectedLayer>>>>>>>>>>>>>>',SelectedLayer);
});

For overlayers:

var name;
map.on('overlayadd', function(e){
   name = e.name;
   console.log(name);
})