First look at the Setvisiblelayers method:
setvisiblelayers (IDs, Donotrefresh? ) )defaultdefaultinlayerinfo
with a value of-1. if (Visible.length = = = 0) { Visible.push (-1);} Layer.setvisiblelayers (visible);
After testing, JS development, setvisiblelayers for features with layer groups cannot be directly manipulated.
For example:
The layer Layerinfo (red circle, Grouplayer) is show/hide after loading and does not work properly.
Workaround 1:
Do not increase the layer Group (Grouplayer) when publishing the layer, all layers under one layer, after the release of the call to normal use;
Workaround 2:
The first solution needs to put all the data on the same layer, do not set the group layer, for simple data, but a large number of data need to group, you need to consider the grouping problem, Grouplayer itself in the JS API after loading is Layerinfo,
The idea is: Remove the layer groups inside the Visiblelayer, and then call Setvisiblelayers (Visiblelayer).
1 functiongetvisiblelayers (layer, sublayerindex) {2Require (["Dojo/_base/array"],function(array) {3 varLayerinfos =Layer.layerinfos;4 vari;5 //array for setting visible layers6 varVisiblelayers = [-1];7 8 if(typeofSublayerindex!== "undefined") {9 varNewvis =!layerinfos[sublayerindex].defaultvisibility;Ten //reverse current visibility of sublayer OneLayerinfos[sublayerindex].defaultvisibility =Newvis; A } - - //For each sublayer the for(i = 0; i < layerinfos.length; i++) { - varinfo =Layerinfos[i]; - //push to visible layers if it ' s visible - if(info.defaultvisibility) { + Visiblelayers.push (info.id); - varNegative = Array.lastindexof (visiblelayers, 1); + if(Negative!==-1) { AVisiblelayers.splice (negative, 1); at } - } - } - //Now and the array of Visiblelayer Ids is assembled, description: Grouplayer is processed here - //strip off Ids of invisible child layers, and - //Ids of group layers (group layer Ids should not being submitted in //In . setvisible () or loss of toggle control madness ensues. - //Remove layers whos parents is not visible: to varNoinvisibleparents = []; + for(i = 0; i < visiblelayers.length; i++) { - varID =Visiblelayers[i]; the varHasparentsinvisiblearray = This. _allidspresent (layer, ID, visiblelayers); * if(Hasparentsinvisiblearray) { $ Noinvisibleparents.push (ID);Panax Notoginseng } - } the varNogroups = []; + for(varj = 0; J < Noinvisibleparents.length; J + +) { A varLyrinfo = This. _getlayerinfo (layer, noinvisibleparents[j]); the if(lyrinfo && Lyrinfo.sublayerids = = =NULL) { + Nogroups.push (Noinvisibleparents[j]); - } $ } $ //note:set-1 if array is empty. - if(!nogroups.length) { -Nogroups = [-1]; the } - returnnogroups;Wuyi }); the}
The code is slightly more complex, but it can be used as a reference for custom layer operations.
Note: Refer to the ArcGIS JS API layerlist source code
ArcGIS API arcgisdynamicmapservicelayer.setvisiblelayers is not valid for data with Grouplayer layer groups (for layerinfo).