LayerList Event Listener when layer is visible?

1136
2
07-22-2019 02:35 PM
boeskmgmoes
New Contributor II

I'm interested in being able to hide widgets such as the TimeSlider when displaying FeatureLayers through the LayerList. The goal would be to only show the TimeSlider when a relevant FeatureLayer is visible. On previous versions of the API I was able to do this by manually creating an eventListener for a CSS class associated with the LayerList children. This hasn't been possible with the most current version of the API (4.12). Is there any way to create some sort of EventListener or some sort of way of tracking when a specific layer has been turned on? 

I'm using version 4.12 of the ArcGIS Javascript API.

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Sure there is. You just add a watch to the visible property of the layer you are interested in.

0 Kudos
DavidWilson3
Occasional Contributor

Hi Boeskm,

Here is function I quickly wrote up that can get you a visible layer and then you can do whatever you want with the layer.

function getVisibleLayer() {
  var layerList = [Layer1, Layer2, Layer3];
  var visibleLayer = new FeatureLayer();
  for (i = 0; i < layerList.length; i++) {
    if (layerList[i].visible == true) {
        visibleLayer = layerList[i];
     }
   }
   return visibleLayer;
 }‍‍‍‍‍‍‍‍‍‍
0 Kudos