AnsweredAssumed Answered

Remove layer from Table of Contents

Question asked by ho0mwp on Oct 8, 2014
Latest reply on Oct 9, 2014 by ho0mwp

I am using the following code to remove a layer from javascript map but how do I also remove it from my toc?

 

for (var j = 0, jl = map.layerIds.length; j < jl; j++) { 

            var currentLayer = map.getLayer(map.layerIds[j]);

    if(currentLayer.id == 'StoresResultsLayer'){

     map.removeLayer(map.getLayer(map.layerIds[j]));

    

    };   

};

 

I tried this but I don't get the whole splice thing.

 

     toc.layerInfos.splice(3, 0);// remove it.

     currentLayer = null;

 

 

I create it using this code:

 

      var StoresResultsLayerURL = "My layer link here";
      var StoresResultsLayerOptions = {
        "id": "StoresResultsLayer",
        "opacity": .99,
        "showAttribution": false
      };
      var StoresResultsLayer = new esri.layers.ArcGISDynamicMapServiceLayer(StoresResultsLayerURL, StoresResultsLayerOptions);

var layerDefinitions = [];
//layerDefinitions[0] = varQuery;
//layerDefinitions[1] = varQuery;
//layerDefinitions[2] = varQuery;
layerDefinitions[3] = varQuery;
layerDefinitions[4] = varQuery;
StoresResultsLayer.setLayerDefinitions(layerDefinitions);

      StoresResultsLayer.setVisibleLayers([3, 4]);
  
var h = StoresResultsLayer.on("load", function () { 
         toc.layerInfos.splice(3, 0, { 
            layer: StoresResultsLayer, 
            title: "Stores Results" 
          }); 
          toc.refresh(); 
          toc.on('load', function (evt) { 
            toc.findTOCNode(StoresResultsLayer, 0).hide(); 
            toc.findTOCNode(StoresResultsLayer, 1).hide();
   toc.findTOCNode(StoresResultsLayer, 2).hide();

          }); 
          h.remove(); 
        });   
  
   //dlMCY_Stores.hide();    

//---  End Add Layer to TOC ---//     
map.addLayer(StoresResultsLayer);

Outcomes