How to Dynamically Check on/off a service layer?

1875
7
Jump to solution
01-08-2019 06:31 AM
ShaningYu
Frequent Contributor

In my JavaScript App, there are 5 Group layers, each of which contains 4-5 sub-layers, are loaded from an ArcGIS server. 

In debugging, I check the Object  this.map, but I don't know how to loop the service layers, e.g. How to Dynamically Check a service layer is checked on/off.  Thanks if you can help.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TimMcGee1
Occasional Contributor III

The API docs that I linked has sample code on how to use "setVisibleLayers". Here's a few other comments.

lyr.visibleLayers‍; // is an array of ids of the sub layers that are currently visible.,


lyr.setVisibleLayers([1,2,3]); // Sets the sub layers with ids 1, 2 and 3 as the only visible layers.


lyr.setVisibleLayers([-1]); //Sets all layers as not visible.

View solution in original post

7 Replies
TimMcGee1
Occasional Contributor III

The documentation for the JavaScript API provides an example for looping through the layers in the map: Map | API Reference | ArcGIS API for JavaScript 3.27 

0 Kudos
ShaningYu
Frequent Contributor

Tim: Thanks for your response.  I can access to the layer.  But I can't go to the next level.  For instance, in my App, there are 2 map service layers.  The layer0 is the basemap from ESRI, the other one contains 2 dozens of layers.  How can a sublayer be accessed?  E.g.

var sLyr = this.map.getLayer( ???);

or

var _layer = this.map.getLayer( this.maplayerId[1];

for (var i = 0; i < _layer.layerInfos.length; i++) {

  var lyr = this.map.getLayer( ???);

}

Thanks if you can provide additional help.

0 Kudos
TimMcGee1
Occasional Contributor III

Sounds like you would want to use the `layerInfos` array, the `visibleLayers` array and the `setVisibleLayers` method of a Dynamic MapService layer to access the sub layers, and affect their visibility : ArcGISDynamicMapServiceLayer | API Reference | ArcGIS API for JavaScript 3.27 

0 Kudos
ShaningYu
Frequent Contributor

From MapImageLayer - Toggle sublayer visibility | ArcGIS API for JavaScript 4.10 , I know it can use  sublayers in V4.1.  However for the V3.2x, I am still not able to do something like that dynamically check on/off a layer using JS code.  Could you provide a sample or some detail?  Thanks.

0 Kudos
TimMcGee1
Occasional Contributor III

The API docs that I linked has sample code on how to use "setVisibleLayers". Here's a few other comments.

lyr.visibleLayers‍; // is an array of ids of the sub layers that are currently visible.,


lyr.setVisibleLayers([1,2,3]); // Sets the sub layers with ids 1, 2 and 3 as the only visible layers.


lyr.setVisibleLayers([-1]); //Sets all layers as not visible.
ShaningYu
Frequent Contributor

Tim: Thanks a lot.

0 Kudos
ShaningYu
Frequent Contributor

Tim: I posted a thread at https://community.esri.com/message/825059-how-to-hide-a-map-service-layer. I reviewed cmv docs but found no solution. Could you provide me your hint for the solution? Thanks.

0 Kudos