Show Layer (or not) in Legend?

1523
3
01-30-2017 06:24 PM
SBerg_VHB
Occasional Contributor

Hi, does anyone know the API for instructing a new layer being added to a JS map, not to be included in the Legend dijit?

var featureLayer = new FeatureLayer(op.url, {
mode: FeatureLayer.MODE_SELECTION,
outFields: ["*"]
});
featureLayer.setSelectionSymbol(new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
new Color([0, 255, 255]), 4));

this.map.addLayer(featureLayer);//how to avoid having this layer show up in the legend?

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus

Sam,

   If you want to exclude a certain layer from the legend dijit then you have to manually set the legend layerInfos property and exclude that particular layer from the layerInfos:

https://developers.arcgis.com/javascript/3/jsapi/legend-amd.html#layerinfos 

SteveCole
Frequent Contributor

While I think Robert's reply is the most appropriate, there is one other possibility. While creating your Legend instance, there is an option called autoUpdate which controls whether or not your legend widget will automatically add or drop legend items if layers are added or dropped from the map. The default setting is TRUE so you could change this to FALSE in order to keep your other layer from getting added to your legend.

In this case, the legend would only reflect what was initially added to the map. If you needed to update the legend at some other point, you can still call the refresh method.

Rhys-Donoghue
New Contributor III

hi Guys, 

I know this is an old post but adding this here in case it helps someone.  The simple way to handle this is use the listMode property in the constructor for the layer, e.g.

const layer = new GraphicsLayer({
   listMode: "hide"
});

See  Layer | ArcGIS API for JavaScript 4.14 

Cheers,

Rhys

https://nz.linkedin.com/in/rhys-donoghue