1timur

subLayer display list

Discussion created by 1timur on Feb 17, 2013
Latest reply on Feb 18, 2013 by 1timur
I am having a hard time setting up the subLayers of a dynamicMap Service layer to display as a checkbox list, so that the sublayers can be turned on or off by the user.

var map, layer, legendLayers = [];

      function init() {
        map = new esri.Map("map", {
          zoom: 1
        });
  var basemapUrl = "http://--------------------------";
        var basemap = new esri.layers.ArcGISTiledMapServiceLayer(basemapUrl);
        layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://---------------------------",{id:'County'});
  legendLayers.push({layer:layer,title:"Berkeley County"});
 
  //add the legend
        dojo.connect(map,'onLayersAddResult',function(results){
            var legend = new esri.dijit.Legend({
              map:map,
              layerInfos:legendLayers
            },"legendDiv");
            legend.startup();
        });
  map.addLayers([basemap,layer]);
  dojo.connect(map,'onLayersAddResult',function(results){
       
        //add check boxes
        dojo.forEach(legendLayers,function(layer){        
          var layerName = layer.subLayerIds;
          var checkBox = new dijit.form.CheckBox({
            name: "checkBox" + layer.subLayerIds,
            value: layer.subLayerIds,
            checked: layer.layer.visible,
            onChange: function(evt) {
              var clayer = map.getLayer(this.value);
              clayer.setVisibility(!clayer.visible);
              this.checked = clayer.visible;
            }[/COLOR]
          });

          //add the check box and label to the toc
          dojo.place(checkBox.domNode,dojo.byId("toggle"),"after");
          var checkLabel = dojo.create('label',{'for':checkBox.name, innerHTML:layerName},checkBox.domNode,"after");
            dojo.place("<br />",checkLabel,"after");
          });
        });
      }

Outcomes