Is tracking layer toggle events in Javascript 4.15 possible?

1188
2
Jump to solution
05-15-2020 09:24 AM
JamalWest2
Occasional Contributor

In my application I am adding a webmap where all layers are turned off. I am looking for a way to track the # of layers toggled on and the id/name of the layers. I have not been able to find an event triggered when the layers are toggled on/off. I am doing this because I want to be able to dynamically update the Swipe widget which I could do if I could find the event that fires when the layer is toggled.I also want to limit the number of layers toggled on at some point possibly. I am working in javascript 4.15. Just a note the layer toggle is not linked to the layer added/removed from map events. Also I am using the layerlist idget to toggle the layers on/off visibility.

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Jamal,

   Here is one way:

        view.when(function() {
          var layerList = new LayerList({
            view: view,
          });
          
          setTimeout(function() {
            view.layerViews.map(function(item){
              item.watch('visible', function(visible){
                console.info(visible, item.layer.title);
              }, item.layer.title);
            });
          }, 500);

          // Add widget to the top right corner of the view
          view.ui.add(layerList, "top-right");
        });

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Emeritus

Jamal,

   Here is one way:

        view.when(function() {
          var layerList = new LayerList({
            view: view,
          });
          
          setTimeout(function() {
            view.layerViews.map(function(item){
              item.watch('visible', function(visible){
                console.info(visible, item.layer.title);
              }, item.layer.title);
            });
          }, 500);

          // Add widget to the top right corner of the view
          view.ui.add(layerList, "top-right");
        });
JamalWest2
Occasional Contributor

Robert thank you! The item.watch is what I needed.

0 Kudos