I cannot get an ArcGIS Javascript API Legend widget to show up. An excerpt of my code is as follows. I believe I'm failing somewhere near the console.log that I call…undefined comes up in the console at this point. So for some reason evt.layers is not getting populated. Have I provided enough code? If you need more snippets I can copy/paste more.
parser.parse();
//initialize the map object
map = new Map("map", {
basemap:"gray",
center: [-96.53, 38.374],
zoom: 13
});
//access the hydro layer as a feature layer
var hydro = new FeatureLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Hydrography/Watershed173811/MapServer/0", {
mode: FeatureLayer.MODE_ONDEMAND,
outFields: ["*"]
});
//add hydro to map
map.on("layer-add-result", function (evt) {
console.log(evt.layers);
var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
return {layer:layer.layer, title:layer.layer.name};
});
if (layerInfo.length > 0) {
var legendDijit = new Legend({
map: map,
layerInfos: layerInfo
}, "legendDiv");
legendDijit.startup();
}
});
map.addLayers([hydro]);
});
Best Answer
To expand on your answer, you could fix it one of two ways:
This works because
layers-add-result
only fires when calling themap.addLayers()
function, which expects an array of map layers.You could have kept
layer-add-result
which fires any time a single map layer is added. To add a single layer, use the singular functionmap.addLayer(hydro)