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!
The following code doesn't work?
legendDijit.destroy();
what if you also add the following ?
dojo.byId(Name of the Div your legend is displayed in).innerHTML='';
Let me know if that helps!
Tim
Thanks,
legendDijit.destroy will not work in the "funcalldel" function (where the user deletes the map) because when I run it, the variable legendDijit is not recognized and produces an error.
--------------------------
I inserted
dojo.byId("legendDiv").innerHTML='';
into the delete function, and it removed the legend, but I get an error when I try to create another map:
> Tried to register widget with id==legendDiv but that id is already registered
So I think I have to use legendDijit.destroy somewhere, but not sure how.
Make sure at the beginning of your code type
var legendDijit;
and remove the var infront of the legendDijit in your function.
The way you have it the legendDijit is not a global variable.
Ok, using the correct variable scope, I can use the destroy method, but now after I use the delete function, when creating another map, I get these two errors when I call the funcall function again:
TypeError: p.byId(...) is null
and
TypeError: Unable to draw graphic (geometry:null, symbol:null): c is null
Thanks again for your help
Did you incorporate this? dojo.byId("legendDiv").innerHTML='';
If so remove it, you don't need it. Maybe that will help?
I removed it, still the same error. Can't seem to figure out what these errors mean.
What's strange is that it creates the map the first time, but after I delete and run funcall again, it throws the errror!
Can you post your full code or add it to a jsfiddle?
The variable selector is at the bottom of the left pane. Some combinations have no data attached.
Use female/child bearing and female/adult combinations to create the choropleth maps. They have data attached.
Thx again.
Are you able to get to the code? It's (probably unnecessarily) long, but the code in question is at the bottom of the <script> section.