How to get the change event of a layer in OL3 using ol.control.LayerSwitcher

0

I'm using Open Layers 3 with the ol.control.LayerSwitcher plugin and I wanted to know how I can get it when I change a layer in the switcher. I was looking for a lot on the internet and I can not find a solution to this. The idea is when the change or the selection of a layer is made using the plugin ol.control.LayerSwitcher perform an action such as an alert.

If someone could give me a help or an alternative to this. If you know of another alternative that does not use ol.control.LayerSwitcher and determine the change event of a specific layer, I would like to know.

Let's say I have three layers in the switcher.

  • Layer1
  • Layer2
  • Layer3.

By default it is in Layer1. If I change or select the Layer2 I would like it to execute an alert. But as long as Layer2 is selected. That would have to be an event.

    
asked by josego 07.01.2017 в 15:39
source

1 answer

1

Objects that inherit from the class ol.layer.Base issue the event 'change : visible 'when your visibility changes.

Make sure as in the LayerSwitcher examples that the Layers have the type : 'base' property for this to work.

Use something like this then:

[Layer1, Layer2, Layer3].forEach(function(l){
  l.on('change:visible', function(e){
    console.log('Mi capa ${l.name} está visible ? ${l.getVisible()}')
  }
})
    
answered by 07.01.2017 / 17:26
source