mars3d在写label标签的时候,html文本内容是调取接口,接口数据实时更新,怎样让接口数据发生变化的时候,页面数据也跟着变化
export const addCoverCustom = async ({ map }) => {
const graphicLayer = mapLayerCollection.value.custom_layer.initLayer(map);
const labels = await mapLayerCollection.value.custom_layer.formatter();
if (labels?.length) {
// 使用 Promise.all 等待所有异步操作完成
await Promise.all(
labels.map(async (label) => {
if (label.type == 1) {
const graphic = new mars3d.graphic.WallEntity({
positions: label.positions,
attr: {
id: label.properties.id,
},
style: {
closure: true,
diffHeight: 10,
materialOptions: {
image: "img/textures/fence.png",
color: '#d115d6',
count: 2,
speed: 10,
bloom: true
}
},
});
graphicLayer.addGraphic(graphic);
demo(map, graphicLayer, label);
} else {
const graphic = new mars3d.graphic.PolygonPrimitive({
positions: label.positions,
attr: {
id: label.properties.id,
},
style: {
color: label.properties.color,
opacity: 0.4,
clampToGround: true,
classificationType: Cesium.ClassificationType.BOTH,
label: {
text: label.properties.name,
clampToGround: false,
visibleDepth: false,
font_size: 16,
},
},
});
graphicLayer.addGraphic(graphic);
}
try {
const res = await getParkNum({ parkName: label.properties.name });
const data = res.data.records[0];
updateHTMLContent(label, data);
} catch (error) {
console.error(error);
}
})
);
}
};
function updateHTMLContent(label, data) {
const divElementTotal = document.getElementById('idTotal');
const divElementCurrent = document.getElementById('idCurrent');
if (divElementTotal && divElementCurrent) {
divElementTotal.innerText = data.parkNum;
divElementCurrent.innerText = data.currentnum;
}
}
const demo = (map, graphicLayer, label) => {
const idTotal = 'TotalNum';
const idCurrent = 'CurrentNum';
const graphic = new mars3d.graphic.DivGraphic({
position: label.geometry.coordinates,
stopPropagation: true,
attr: {
id: label.properties.id,
},
style: {
html:
`<div class="park-car-label">
<div class="title">${label.properties.name}</div>
<div class="content">
<div id="idTotal" style="padding-bottom: 8px;">1</div>
<div id="idCurrent">1</div>
</div>
</div>`,
horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100, 3999),
label: {
backgroundColor: '#000',
color: "#fff"
}
},
});
graphic.on(mars3d.EventType.click, function (event) {
if (event.graphic.attr.id) {
dataStatus.setDialog({
status: true,
type: dialogTypeEnum.park_car,
condition: {
id: event.graphic.attr.id.toString(),
parkCar: label.properties
},
});
} else {
ElMessage({
showClose: true,
message: '未关联',
type: 'warning',
offset: 50,
});
}
map.flyToGraphic(graphic, {
radius: 300,
heading: 359,
pitch: -47.2,
duration: 2,
});
});
graphicLayer.addGraphic(graphic);
};