How to remove all layers from a Web AppBuilder App (Dev)

2892
8
Jump to solution
09-14-2016 04:18 PM
ChristopherSchreiber
Frequent Contributor

Hello all,

I have been working on  a custom widget to work with our application. One of the requirements of this widget is to turn off all of the layers on the map while it is open and then turn them back on once it is closed. I have tried a few different methods of doing this with no luck as of yet. Has anyone came across something like this?

Thanks!

Chris

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Chris,

   This snippet of code will turn off all the parent layers:

        require(["jimu/LayerInfos/LayerInfos"], lang.hitch(this, function(LayerInfos) {
          LayerInfos.getInstance(this.map, this.map.itemInfo).then(function(layerInfosObject){
             layerInfosObject.getLayerInfoArray().forEach(function(layerInfo) {
               layerInfo.setTopLayerVisible(false);
             });
          });
        }));

View solution in original post

8 Replies
RobertScheitlin__GISP
MVP Emeritus

Chris,

   This snippet of code will turn off all the parent layers:

        require(["jimu/LayerInfos/LayerInfos"], lang.hitch(this, function(LayerInfos) {
          LayerInfos.getInstance(this.map, this.map.itemInfo).then(function(layerInfosObject){
             layerInfosObject.getLayerInfoArray().forEach(function(layerInfo) {
               layerInfo.setTopLayerVisible(false);
             });
          });
        }));
ChristopherSchreiber
Frequent Contributor

Thanks Robert!

That solved my problem!

Is it possible to re-activate the layers that were visible before the widget was activated? 

Thank you!

Chris

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Chris,

   Sure you would have to maintain an array of layer visibility and then restore the layer visibility later using this array and setTopLayerVisible as true.

ChristopherSchreiber
Frequent Contributor

Thanks again Robert!

Chris

0 Kudos
SimonWebster
Regular Contributor

Robert, 

Is there a way to modify this code snippet so it does not apply to parents, but rather to all child layers?

Simon

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Simon,


  Not sure I understand. If you set the parent to be false all the children of that parent are no longer visible. If you set the child then just that hold is not visible. So you just get the id of the exact layer you want and use that. If you want to turn off multiple children then just use that line of code multiple times with their unique ids.

0 Kudos
SimonWebster
Regular Contributor

Robert,

The map I'm working with has a large number of child layers. In my mind, I run a for loop like the above to ensure that all child layers are not visible, then just switch the few on for the function required.

This said, I'm starting to dig around with some of the functions and get the feeling that it wont be as simple as an easy modification to the code above. Perhaps loading subLayerIds to an array then looping through them? Thoughts?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Simon,

  You need to look at traversal method of LayerInfos:

LayerInfos class—Web AppBuilder for ArcGIS (Developer Edition) | ArcGIS for Developers