How to loop through and turn off ALL layers

2041
3
Jump to solution
03-05-2020 12:27 PM
CamCode
New Contributor III

The below finds my layer and shows visible property in the console, when I toggle it there from 'true' or 'false' it changes.

myApp.mapview.map.findLayerById('cool_layer'); 

enter image description here

But when I run the below line, I can toggle my layers on or off individually, but how could I loop through all of them, and turn them all off? I am trying tocreate a 'reset' button.

myApp.mapview.map.findLayerById('cool_layer').visible = true;
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Cam,

   Your issue is that the id of the layer is not the layer name normally it is something like:

view.map.findLayerById("Hazards_Uptown_Charlotte_665").visible = false;

If you do not know the exact layer id then you first have to loop through the maps layers and check the layers title or name to see if it matches 'cool layer' and get that layers actual id.

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Cam,

  Loop through the myApp.mapView.map.layers collection and set the visibility property to false.

          view.map.layers.map(function(lyr){
            console.info(lyr);
            lyr.visible = false;
          });
CamCode
New Contributor III

Thanks Robert, any idea on how to do something similar with all my filtering? How could I programatically find the following in the console, similar to how I am searching my layers: i.e. featureLayerView.filter i.eFeatureFilter | ArcGIS API for JavaScript 4.14 - I can't find this in the console with the similar syntax i.e. below 

myApp.mapview.map.findLayerById('cool_layer').visible = true;
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Cam,

   Your issue is that the id of the layer is not the layer name normally it is something like:

view.map.findLayerById("Hazards_Uptown_Charlotte_665").visible = false;

If you do not know the exact layer id then you first have to loop through the maps layers and check the layers title or name to see if it matches 'cool layer' and get that layers actual id.