Hi Jay, there are very likely other methodologies to hide a layer from the legend, however the example I have below has worked for me.
For example, in one of our applications I hide our aerials when the legend loads into the map since it acts as one of our basemaps.
The function loads, as you mentioned, when the layers-add-result
event occurs on the initial map load (so a one time on load event), like so:
myMap.on("load", function () {
myMap.on("layers-add-result", loadLegend);
}
The loadLegend function uses defined id names, and in the case below fsa2015, which can be defined for any layer type (e.g. FeatureLayer, ArcGISDynamicMapServiceLayer
, ArcGISTiledMapServiceLayer, WebTiledLayer,
etc.) like so:
var fsa2015 = esri.layers.ArcGISTiledMapServiceLayer("url", {
id: "fsa2015"
});
The loadLegend function is below, using the defined id name. Note: Dependencies, which you also mentioned, include: dojo/_base/array
(arrayUtils
) and esri.diji.Legend
:
function loadLegend (evt) {
var layerInfo = arrayUtils.myMap(evt.layers, function (layer, index) {
if (layer.layer.id === "fsa2015") { return { }; } //Hides FSA aerial layer
else { return { layer: layer.layer, title: layer.layer.name }; } //Else return as-is
});
myLegend= esri.dijit.Legend({
map: myMap,
layerInfos: layerInfo
}, "myLegendDiv");
myLegend.startup();
myLegend.refresh();
}
}
In Esri's documentation, layer
is required in the layerInfos object, so without it being specified the layer won't load in the legend when active in the map.
Hope that helps!
More information on the Legend's LayerInfos from Esri's documentation.