Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Getting the following error when running my app:

Error: Tried to register widget with id==legendDiv but that id is already registered

Not sure how to get rid of it. My javascript for the legend is:

//Add the Legend
      legendLayers.push({
          layer: layer,
          title: ''
      });
      dojo.connect(map, 'onLayersAddResult', function (results) {
          var legend = new esri.dijit.Legend({
              map: map,
              layerInfos: legendLayers
          }, "legendDiv");
          legend.startup();
      });
      map.addLayers([layer]);

      if (layer.loaded) {
          buildLayerList(layer);
      } else {
          dojo.connect(layer, "onLoad", buildLayerList);
      }

and my HTML is:

<div dojoType="dijit.TitlePane" title="Legend" closable="false"  open="false" style="float:right;">
                <div dojoType="dijit.layout.ContentPane" style="width:180px; overflow:auto;">
                <span id="legendDiv"></span>
                </div>
    </div>

Can anybody suggest a workaround or a fix to the error. I've done some searching and it looks like I might need to do a destroy? Not sure how to do that.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Add the legend on map load, the onLayersAddResult is firing multiple times which means it is trying to create more than one legend of the same name. Connect to the onLoad event

dojo.connect(map, 'onLoad', function (results) {

share|improve this answer
    
This eliminated the error that I described, so I'll mark it as the answer, however, it then triggers 2 "TypeError: Unable to get value of the property 'then': object is null or undefined" errors. Thanks for your help. –  Craig Dec 3 '12 at 20:03
    
Apologies, yes you can use the onLayersAddResult but the cause of the error is trying to load the legend twice. There is some of your code missing, I'm not sure what buildLayerList is but I suspect that this is also calling the legend to startup. I've put together a fiddle here showing a legend using onLayersAddResult jsfiddle.net/dave_wilton/cYXph –  David Wilton Dec 4 '12 at 8:29
    
Turns out I was using map.addLayers() twice... so it was firing the legend twice. Thanks for the fiddle. –  Craig Dec 4 '12 at 13:08
add comment

var legend = new esri.dijit.Legend({ map: map, layerInfos: legendLayers }, "legendDiv");

works two times. You must call it only one time. Whatever you can fix the "id is already register" error with dijit's destroy function. For this example

if(legend){ legend.destroy(); }

legend = new esri.dijit.Legend({ map: map, layerInfos: legendLayers }, "legendDiv");

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.