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.
Solved! Go to Solution.
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.
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
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.
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
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.
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.
Tim: Thanks a lot.
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.