<html> <head> <title>setVisibleLayers Question</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/dojo/dijit/themes/claro/claro.css"> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/esri/css/esri.css" /> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.2"></script> <script type="text/javascript"> dojo.require("esri.map"); function init() { var extent = new esri.geometry.Extent( { "xmin":1016752.0585392233,"ymin":1857876.6670321897,"xmax":1051557.2503922875,"ymax":1876696.1114766342 ,"spatialReference":{ "wkt": "PROJCS[\"NAD_1983_HARN_StatePlane_Illinois_East_FIPS_1201\",GEOGCS[\"GCS_North_American_1983_HARN\",DATUM[\"D_North_American_1983_HARN\",SPHEROID[\"GRS_1980\",6378137.0,298.257222101]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"False_Easting\",984250.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",-88.33333333333333],PARAMETER[\"Scale_Factor\",0.999975],PARAMETER[\"Latitude_Of_Origin\",36.66666666666666],UNIT[\"Foot_US\",0.3048006096012192]]" } } ); var map = new esri.Map("map",{extent: extent}); var service = new esri.layers.ArcGISDynamicMapServiceLayer("http://servicesbeta2.esri.com/arcgis/rest/services/Water_Network/MapServer"); // I've tried setting the visibleLayers before and after adding the ArcGISDynamicMapServiceLayer to the map // Here, I also tried the optional "false" to refresh, even though I hadn't added it to the map yet //service.setVisibleLayers([19,23]); map.addLayer(service); service.setVisibleLayers([19,23], false); } dojo.addOnLoad(init); </script> </head> <body> <div id="map" class="claro" style="width:1130px; height:611px; border:1px solid #000;"></div> </body> </html>
Solved! Go to Solution.
when you specify the index of a group layer, you are bringing all layers in that groupwas that basic principle I was not getting.
: initComponent: function(){ this.panel = Ext.create( "Ext.tree.Panel" ,{ store: this.parentTreeStore ,rootVisible: false ,hideHeaders: true : } ); this.panel.on("checkchange", this.treeOnCheckChange, this); this.add(this.panel); } ,treeOnCheckChange: function(node, checked, eOpts){ var service = this.getService(node); // if it is the service itself that is turned on/off // simply set its visibility as requested if (node.raw.service === service){ service.setVisibility(checked); return; } // if any group above the node is turned off, there // will be no change to the service's visibleLayers var tmpNode = node.parentNode; while (!tmpNode.parentNode.isRoot()){ if (tmpNode.get("checked") === false){ return; } tmpNode = tmpNode.parentNode; } var visibleLayers = this.getVisibleLayersFromTree(node); if (checked){ // adding to the visibleLayers visibleLayers.push.apply(visibleLayers, service.visibleLayers); } else { // removing from the visibleLayers // it's already been checked off in the tree so wasn't // returned in the getVisibleLayersFromTree call above visibleLayers.push(node.raw.id); var tmpVisibleLayers = []; for (var i = 0; i < service.visibleLayers.length; ++i){ if (visibleLayers.indexOf(service.visibleLayers) === -1){ tmpVisibleLayers.push(service.visibleLayers); } } visibleLayers = tmpVisibleLayers; } var pos = visibleLayers.indexOf(-1); if (pos != -1){ // if all layers were turned off previously, this item is still there visibleLayers.splice(pos, 1); } if (visibleLayers.length === 0){ visibleLayers.push(-1); } service.setVisibleLayers(visibleLayers); } // the parentTreeStore can contain multiple treeStores, each associated with a single // mapping service. The tree's root is just a container for those service children // and the service layer itself is attached to its uppermost node ,getService: function(child){ var parent = child.parentNode; while(!parent.isRoot()){ child = parent; parent = child.parentNode; } return child.raw.service; } ,getVisibleLayersFromTree: function(node){ var results = []; if (node.isLeaf()){ if (node.get("checked")){ results.push(node.raw.id); } } else { for (var i = 0; i < node.childNodes.length; i++){ var visibleLayers = this.getVisibleLayersFromTree(node.childNodes); if (visibleLayers.length > 0){ results.push.apply(results, visibleLayers); } } } return results; } :