传入一个 mapserver 的图层, 该图层在公布时曾经定义了图例!!!!!
前端代码👇👇👇👇👇
<!-- 图例列表 -->
<div id="legend-wrapper">
<div id="legend-box"></div>
</div>
此处须要留神的时每次渲染不同 mapserver 生成图例须要革除上一次的图例,提前先 removeChild 再 appendChild 很实用!!!!
async createLegend(layer) {
let that = this;
if (layer) {
let Legend = await arcgisPackage.Legend;
let legendBox = document.getElementById("legend-box");
let legendWrapper = document.getElementById("legend-wrapper");
legendWrapper.removeChild(legendBox)
var elem=document.createElement('div');
elem.setAttribute('id','legend-box');
legendWrapper.appendChild(elem)
legendBox = document.getElementById("legend-box");
let legend = new Legend({
view: this.view,
layerInfos: [
{
layer: layer,
title: "图例",
},
],
container: legendBox,
});
that.view.ui.add(legend, "top-left");
legendWrapper.appendChild(legendBox);
console.log(legend,legendBox);
}
},