Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

All my layer URLs and Feature Layers are stored in an array.

with an foreach loop, I am creating the FeatureLayers from the URLs,

dojo.forEach(window.layerUrls, function (info, idx) {
            window.featureLayers[idx] = new esri.layers.FeatureLayer(
            window.layerUrls[idx], {
                infoTemplate: infoTemplate,
                mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
                outFields: window.outFields

This is the code to generate the legend:

dojo.connect(, 'onLayersAddResult', function (results) {
            var legendDiv = dijit.byId('legendDiv');
            if (legendDiv) {
            var layerInfo =, function (layer, index) {
                return { layer: layer.layer, title: };

            var legendDijit = new esri.dijit.Legend({
                map: map,
                layerInfos: layerInfo
            }, 'legendDiv');


Whe I run the page this is what the legend looks like and I have no idea why: Here is the code related to this picture:

<div id="legendDiv_graphicsLayer6" class="esriLegendService" style="display: block;">
<div id="legendDiv_graphicsLayer4" class="esriLegendService" style="display: block;">
<div id="legendDiv_graphicsLayer3" class="esriLegendService" style="display: block;">
<div id="legendDiv_graphicsLayer5" class="esriLegendService" style="display: block;">
<div id="legendDiv_graphicsLayer2" class="esriLegendService" style="display: block;">
<div id="legendDiv_graphicsLayer1" class="esriLegendService" style="display: block;">
<div id="legendDiv_msg" style="display: none;">Keine Legende vorhanden</div>
<div id="legendDiv_msg">Legende wird erstellt...</div>
<div id="legendDiv_msg">Legende wird erstellt...</div>
<div id="legendDiv_msg">Legende wird erstellt...</div>
<div id="legendDiv_msg">Legende wird erstellt...</div>
<div id="legendDiv_msg">Legende wird erstellt...</div>

Additonally: when zooming the legend disappears and only one random FeatureLayer-legend remains

Where did I go wrong and why?

share|improve this question

The legend's layerInfos property has to be an array. You are passing in a function.

You're also attempting to create the legend on each loop iteration. What you want to do is loop through your feature layers and create an array that you can pass to layerInfos. The basic legend widget sample shows this using Adapting your code to do this would look something like this:

var layerInfos =, function(layer) {
  return { layer: layer, title: };
var legend = new esri.dijit.Legend({
  map: map,
  layerInfos: layerInfos
}, "legendDiv");

Note that you should do this after the feature layers have been added to the map so it's a good idea to use map.addLayers() and onLayersAddResult which is also shown in the sample linked above.

share|improve this answer
I incorporated these changes on my own, however the problem persists. I am getting the legend PLUS "legend is being created..." like in the screenshot /edit: updated original code – Marco May 8 '13 at 6:05
Please post a more complete code sample to reproduce the problem. – Derek Swingley May 8 '13 at 16:11
I will post the complete code as it is on monday. – Marco May 9 '13 at 20:24

Your Answer


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.