Select to view content in your preferred language

Legend Dijit not refreshing correctly

1184
2
Jump to solution
06-21-2013 01:47 PM
NilsBabel
Frequent Contributor
I have a web application built with v3.5.  I have some Jquery functionality that changes the map being built based on user input.  My problem is 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 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?
0 Kudos
1 Solution

Accepted Solutions
derekswingley1
Deactivated User
I would go with approach #1??? destroying and re-creating your map will be easier to manage than removing and then adding new layers to a map.

As for your legend not updating, are you using map.addLayers (notice that layers is plural)? Using addLayers should fire onLayersAddResult. It would be helpful if you could post your code showing exactly what you're doing.

Here's an example showing using legend.destroy() to get rid of a legend and then re-create it:  http://jsfiddle.net/UKNrr/ Notice that the dom element for the legend is created as part of the call to the legend constructor.

View solution in original post

0 Kudos
2 Replies
derekswingley1
Deactivated User
I would go with approach #1??? destroying and re-creating your map will be easier to manage than removing and then adding new layers to a map.

As for your legend not updating, are you using map.addLayers (notice that layers is plural)? Using addLayers should fire onLayersAddResult. It would be helpful if you could post your code showing exactly what you're doing.

Here's an example showing using legend.destroy() to get rid of a legend and then re-create it:  http://jsfiddle.net/UKNrr/ Notice that the dom element for the legend is created as part of the call to the legend constructor.
0 Kudos
NilsBabel
Frequent Contributor
Notice that the dom element for the legend is created as part of the call to the legend constructor.


That's what I was missing with legend.destroy().  I didn't know how to recreate the div.  Thanks a bunch.
0 Kudos