AnsweredAssumed Answered

Legend creation/destroy

Question asked by mborr on Aug 18, 2014
Latest reply on Aug 19, 2014 by mborr

Hi!

 

I'm stuck on this legend creation.

 

I am creating a legend for a unique renderer for feature service "ct" in function funcall (called by a button) It creates a choropleth map.

 

The user then deletes the new map (in function funcalldel), and can create another with a new renderer, and so on. (the code shown is truncated for clarity)

 

I can create a legend the first time, but I can't seem to use destroy method or any other way to delete the existing legend and create a new one.

 

function funcall(radioOne,radioTwo,radiohs,radioadult){     dijit.byId("create").setAttribute('disabled', true);              //map.removeLayer(ct)       ct = new esri.layers.FeatureLayer("http://gis-server.cc.wmich.edu/harcgis/rest/services/hdream/tract_sex_age2/MapServer/0",{         mode: esri.layers.FeatureLayer.MODE_ONDEMAND,         opacity: .7,         outFields: ["*"],  });       else if (mandf.checked && sen.checked) {                      var maxval=1049;     var field="MF_Senior_ ";                          var tracttemplate=new esri.InfoTemplate();        tracttemplate.setTitle("<b>Male/Female Senior (65+)</b><br>");         tracttemplate.setContent("<b>Population: </b>${MF_Senior_ }"                     );                     ct.setInfoTemplate(tracttemplate);                  }            var renderer = new esri.renderer.SimpleRenderer(new esri.symbol.SimpleFillSymbol().setOutline(new esri.symbol.SimpleLineSymbol().setWidth(0.5)));           renderer.setColorInfo({             field: field,             minDataValue: 0,             maxDataValue: maxval,             colors: [               new esri.Color([255, 255, 255]),               new esri.Color([0, 42, 255])             ]           });           var legendLayers = [];                     ct.setRenderer(renderer);                    map.addLayer(ct);         map.reorderLayer(ct,15);                legendLayers.push({layer:ct,title:'Population'});           var legendDijit = new esri.dijit.Legend({                     map: map,             layerInfos:legendLayers            }, "legendDiv");            legendDijit.startup();       };    function funcalldel(legendDijit){            if (ct.loaded = 'true') {map.removeLayer(ct)};     dijit.byId("create").setAttribute('disabled', false);           };                   dojo.ready(init);   </script>

I'm sure I need to destroy the legend somewhere. I tried that in "funcalldel", but an error appear because the variable "legendDijit" is not recognized.

 

Maybe use an if..then else?

 

Thanks!

Outcomes