In the process of arcGIS map display, it is very common to display legends. The question is, how to show it.

Generally, just give a legend control and you’re done.

But, like the dynamic layer (ArcGISDynamicMapServiceLayer) layer of the show, is the entire service load, and then specify whether some layer can be seen, in this way to show these layers.

A dynamic layer service often has many layers, children, children, want or want, a tree structure. If the entire layer service is stuffed into the legend control, the resulting legend is also a tree structure.

var dynamicLayer = new ArcGISDynamicMapServiceLayer(mapServer, { id: serverName });
//legend is a legend control
legend.layerInfos.push({
    layer: dynamicLayer,// The entire layer service is stuffed into the legend control
    title: ""}); legend.refresh();Copy the code

Typically, legend controls are placed in a corner of the map, a small piece, the content of the tree structure takes up a lot of space; And the layer of the tree structure, many layers are no legend, display in legend control blank, very ugly.

My solution is not to stuff the dynamic layers into the legend control, but to show the legend of the layers that are displayed.

var fl1 = new FeatureLayer(layerUrl1, {});FeatureLayer (FeatureLayer) can be loaded separately
var fl2 = new FeatureLayer(layerUrl2, {});
legend.layerInfos.push({
    layer: fl1,
    title: "".id: id,
});
legend.layerInfos.push({
    layer: fl2,
    title: "".id: id,
});
legend.refresh();
Copy the code

After this, the legend is no longer a tree structure, but a parallel structure, more beautiful, but also in line with people’s use habits.