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.

I have a web application built with v3.5 of the JS API. I have some Jquery functionality that changes the map being built based on user input. My problem is that I can't get the legend to stay in sync with the map. On the first selection I build a map with new esri.Map and I create a new esri.dijit.Legend on the map's onLayersAddResult event. With the first selection everything works and is pretty snappy. When a user chooses the next map setup I've tried two different techniques:

  1. The first is to build a brand new map. I call map.destroy() on the old one to get rid of it. I create a new map and add my layers to it but a new legend does not get created on the onLayersAddResult. The event fires but it seems it cannot create a new legend. If i use legend.destroy() before creating a new one, it blows away the whole div and can't create the legend either.
  2. The second technique is not to create a new map. I remove all the layers from the map and add the new layers to the map. Then I point legend.map to the map and use legend.refresh() with the new layers as an argument. This works, sort of. It seems legend.refresh is really slow and if my second map has more than 3 or 4 layers in it (which it does) it will never draw. In fact it hijacks the whole app and everything is frozen.

So can anyone give me some tips on how to use the legend dijit correctly? Either get rid of it properly so I can build a new one or have it refresh correctly and promptly with new map layers?

share|improve this question
    
Can you post a jsfiddle with the issue in it? –  CLJ Jun 24 '13 at 19:39
    
What version of ArcGIS Server are you using? The documentation says that is the layers are 10 or older then ArcGIS.com needs access to your layers to create the legend. Is that a possible issue? developers.arcgis.com/en/javascript/jsapi/legend.html –  Jamie Jun 24 '13 at 19:44
    
I'm using ArcGIS Server 10.1. I'll see if I can put up some of the functionality in jsfiddle. –  NBabel Jun 24 '13 at 20:03
    
Barebones jsfiddle - jsfiddle.net/JUkwV This just contains the javascript init functionality. When a user chooses a map we set the right case value and fire off dojo.ready(init) –  NBabel Jun 24 '13 at 20:15
    
A couple thoughts: 1) In technique #2, have you tried calling legend.startup() again (instead of legend.refresh())? 2) Do you need to specify layerInfos to the legend constructor and refresh? If you just want to display all legends from all map layers, you can leave out the layerInfos argument altogether. 3) If you are concerned about destroying the "legendDiv", try creating another nested div (e.g., "legendDivInner") using dojo's dom-construct that can be safely destroyed when legend.destroy() is called. Then you can recreate the div as needed. –  greenlaw Jun 24 '13 at 20:34
show 1 more comment

1 Answer

According to the documentation, you should not need to call refresh() on the legend dijit, unless you specify the layerinfos when constructing the legend.

Calling refresh is only necessary when layerInfos is used in the Legend constructor, otherwise the legend will refresh the layers automatically.

https://developers.arcgis.com/en/javascript/jsapi/legend.html

share|improve this answer
    
Thanks, greenlaw answered the question above. legend.destroy is what I need to use, just didn't know how to recreate the div. –  NBabel Jun 26 '13 at 16:48
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.