Current visible layers

544
2
06-26-2014 02:19 AM
CharlesGant
New Contributor III
All,

Is there a built in method or function within the API that when executed will return an array of currently visible layers?

I have some functions written to refresh my map at certain time intervals, and also on command via a evt button.  I need to know which layers are currently visible before the refresh script runs so that I can turn them all back on.  You might be asking, why do they go off?  Well, they don't all go off, just certain csv layers.  I have 5 csv layers that are each created with a separate function.  Within the function, at the end is a .hide() command so that when the page is loaded the data is already created, but not visible.  Then the layer of choice is just turned on with another evt button.  However, if the user has one of these layers activated and the refresh function runs, which recreates the csv layers to populate new data (data is updated every five minutes), it will be invisible.  Thus, the need to know ahead of time which are visible so I can loop through them at the end of the refresh function to .show() them again. 

Thanks!
0 Kudos
2 Replies
ScottGunn
New Contributor III
Are these actual CSVLayer() layers?  If so, they should have a "visible" property:
https://developers.arcgis.com/javascript/jsapi/csvlayer-amd.html#visible

so reading yourCSVLayerName.visible should return true or false.

Actually, other types of layers have the same "visible" property:
https://developers.arcgis.com/javascript/jsapi/featurelayer-amd.html#visible
https://developers.arcgis.com/javascript/jsapi/arcgisdynamicmapservicelayer-amd.html#visible

So you can just write some logic to check for that like:

if (!yourCSVLayerName.visible) {
yourCSVLayerName.show();
}


Or you could do something like this:

function getLayerProperties() {
  for(var j = 0; j < map.layerIds.length; j++) {
    var layer = map.getLayer(map.layerIds);
    if (!layer.visible) {  //if layer is not visible
      layer.show();
    }
  }
}
}
0 Kudos
CharlesGant
New Contributor III
Thanks Scott!

Will see what I can do with this.  Greatly appreciated.

Are these actual CSVLayer() layers?  If so, they should have a "visible" property:
https://developers.arcgis.com/javascript/jsapi/csvlayer-amd.html#visible

so reading yourCSVLayerName.visible should return true or false.

Actually, other types of layers have the same "visible" property:
https://developers.arcgis.com/javascript/jsapi/featurelayer-amd.html#visible
https://developers.arcgis.com/javascript/jsapi/arcgisdynamicmapservicelayer-amd.html#visible

So you can just write some logic to check for that like:

if (!yourCSVLayerName.visible) {
yourCSVLayerName.show();
}


Or you could do something like this:

function getLayerProperties() {
  for(var j = 0; j < map.layerIds.length; j++) {
    var layer = map.getLayer(map.layerIds);
    if (!layer.visible) {  //if layer is not visible
      layer.show();
    }
  }
}
}
0 Kudos