AnsweredAssumed Answered

Legend does not refresh

Question asked by samirgambhir on Apr 17, 2012
Latest reply on Apr 23, 2012 by samirgambhir
Hi all,
I have two feature layers, which I toggle to display either State or sub-State information. My legend div is working fine but it shows the legend for the first feature layer selected at the start of the application. Switching to the sub-State layer refreshes the map but not the legend. Not sure what is incorrect here. Here is my code:
function viewIndicator( {
....
if (unitStActiveM==true){
          stateFeatureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
          mapM.removeLayer(distFeatureLayer);
          dojo.connect(stateFeatureLayer, "onSelectionComplete", mapM.addLayers(stateFeatureLayer));
          showLegend();
          mapM.addLayers([stateFeatureLayer]);
        }
        else if (unitDistActiveM==true){
          distFeatureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
          mapM.removeLayer(stateFeatureLayer);
          dojo.connect(distFeatureLayer, "onSelectionComplete", mapM.addLayer(distFeatureLayer));
          showLegend();
          mapM.addLayers([distFeatureLayer]);
        }
       
        if (dijit.byId("legendDivHolder").domNode.style.visibility = "hidden"){
          dijit.byId("legendDivHolder").domNode.style.visibility = "visible";
         
        }
        else {
          dijit.byId("legendDivHolder").domNode.style.visibility = "visible";
        }
      }

function showLegend() {
       
        dojo.connect(mapM,"onLayersAddResult",function(results){
          var layerInfo = dojo.map(results, function(layer,index){
            if (unitStActiveM==true){
              return {layer:stateFeatureLayer, title: "stateFeatureLayer"};
            }
            else if (unitDistActiveM==true){
              return {layer:stateFeatureLayer, title: "distFeatureLayer"};
            }
          });
          if(layerInfo.length > 0){
            legendDijit = new esri.dijit.Legend({
              map:mapM,
              layerInfos:layerInfo
            },"legend");
            legendDijit.startup();
            legendDijit.refresh();
          }
         
        });
}

Outcomes