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.

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
            }
          );
            map.addLayers([window.featureLayers[idx]]);
        });

This is the code to generate the legend:

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

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

        });

Whe I run the page this is what the legend looks like and I have no idea why: http://abload.de/img/legend22uj3.jpg 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
add comment

1 Answer

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 dojo.map. Adapting your code to do this would look something like this:

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

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 –  Serv 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. –  Serv May 9 '13 at 20:24
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.