Select to view content in your preferred language

Legend Widget -- Layers added dynamically

990
4
Jump to solution
01-29-2013 03:21 PM
GregKnight
Deactivated User
Hi all,

Are there any examples out there that illustrate the logic associated with using the Legend Dijit with layers that are added to the map after the map is instantiated?  All the examples seem to focus on displaying legends for layers that are known at runtime.  In my application the layers are not necessarily known at runtime.  Specifically, I am having some difficulty with the logic associated with

1) initializing the legend at runtime when its possible that no operational layers are loaded

and

2) later refreshing (or starting) the legend with (ad-hoc) operational layers that are added after the map is initially loaded

Are there any examples out there of this nature?  If not, could someone provide an outline of the logic necessary here?

Many thanks,
Greg
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Notable Contributor
Hi Greg,

The legend widget has a refresh property that you can use to update it with new layer information. Here's an example that shows how to refresh the legend with  new layer info.


http://jsfiddle.net/vguu3/

View solution in original post

0 Kudos
4 Replies
ShreyasVakil
Frequent Contributor
I don't know if you have already seen this sample, but it does update the legend dynamically when we update the visibility of layers.
Legend - toggle services
0 Kudos
GregKnight
Deactivated User
Hi Shreyas,

Yes, I have seen that sample.  In that sample, the layers are known at runtime however -- and their layerInfo is pushed into an object which is passed to the legend dijit when it is instantiated. 

var quakeLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/EarthquakesFromLastSevenDays/MapServer",{id:'quakes'});
legendLayers.push({layer:quakeLayer,title:'Earthquakes'});
var fireLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/MapServer",{id:'fire'});
legendLayers.push({layer:fireLayer,title:"Fire"});


Functionally, this is similar to what Im looking for, but I dont have the luxury of knowing the layerInfo at runtime.  What would I do in this case?

Many thanks,
Greg

BTW -  I love the updates to the JS API pages...  fantastic!
0 Kudos
KellyHutchins
Esri Notable Contributor
Hi Greg,

The legend widget has a refresh property that you can use to update it with new layer information. Here's an example that shows how to refresh the legend with  new layer info.


http://jsfiddle.net/vguu3/
0 Kudos
GregKnight
Deactivated User
Hi Kelly,

That definitely helps.  Ill revisit my code later today.

Thank you,
Greg
0 Kudos