Legend does not refresh

790
1
Jump to solution
04-17-2012 11:49 AM
SamirGambhir
Occasional Contributor III
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();
          }
         
        });
}
0 Kudos
1 Solution

Accepted Solutions
SamirGambhir
Occasional Contributor III
Hi all,
I found the solution to this issue. My JavScript console gave the following warning:
Tried to register widget with id==legend but that id is already registered

The solution is to destroy any previously registered widget and recreate it. I added this code (marked in red) right before where I define the parameters of the legendDijit widget:
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){
if(legendDijit){
legendDijit.destroyRecursive(true);
}
legendDijit = new esri.dijit.Legend({
map:mapM,
layerInfos:layerInfo
},"legend");
legendDijit.startup();
legendDijit.refresh();
}

});
}

Thanks
Samir

View solution in original post

0 Kudos
1 Reply
SamirGambhir
Occasional Contributor III
Hi all,
I found the solution to this issue. My JavScript console gave the following warning:
Tried to register widget with id==legend but that id is already registered

The solution is to destroy any previously registered widget and recreate it. I added this code (marked in red) right before where I define the parameters of the legendDijit widget:
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){
if(legendDijit){
legendDijit.destroyRecursive(true);
}
legendDijit = new esri.dijit.Legend({
map:mapM,
layerInfos:layerInfo
},"legend");
legendDijit.startup();
legendDijit.refresh();
}

});
}

Thanks
Samir
0 Kudos