import * as Cesium from 'cesium';
|
import data2 from '@/assets/images/home/homeRight/data2.png';
|
import data1 from '@/assets/images/home/homeRight/data1.png';
|
import MapPopUpBox from '@/views/Home/useUavHome/MapPopUpBox.vue';
|
import { render } from 'vue';
|
import { useStore } from 'vuex';
|
import { getCenterPoint } from '@/utils/cesium/mapUtil';
|
import cesiumOperation from '@/utils/cesium-tsa';
|
import { HeadingPitchRange } from 'cesium';
|
import { getDeviceRegion, getDeviceRegionCount } from '@/api/home/aggregation';
|
import _ from 'lodash';
|
import { getDeviceInfoNum } from '@/api/home/machineNest';
|
import { getTotalJobNum } from '@/api/home';
|
|
/**
|
* 机巢聚合功能
|
*/
|
export const useUavHome = () => {
|
const { flyTo } = cesiumOperation();
|
|
let scalingJudgment = [
|
{ name: '县', splashedList: [], gJson: null, show: false, value: [0, 48651], height: 31753 },
|
{ name: '市', splashedList: [], gJson: null, show: false, value: [48651, 314863], height: 257731 },
|
{ name: '省', splashedList: [], gJson: null, show: false, value: [314863, 3796280000], height: 1987280 },
|
];
|
let viewer = null;
|
let active = null;
|
let handler = null;
|
let positionC3 = null;
|
let currentEntity = null;
|
const listenerHeight = () => {
|
determineScaling();
|
viewer.camera.moveEnd.addEventListener(determineScaling);
|
};
|
|
// 确定缩放比例
|
const determineScaling = index => {
|
console.log('确定缩放比例');
|
if (!viewer) return;
|
let height = viewer.camera.positionCartographic.height;
|
// 根据高度展示对应的 gJson
|
for (let [index, item] of scalingJudgment.entries()) {
|
if (!item.show) return;
|
if (height > item.value[0] && height <= item.value[1]) {
|
if (active === item.name) return;
|
active = item.name;
|
removeEntities();
|
removeLabel();
|
if (item.gJson && item.name !== '县') {
|
aggregation(item);
|
} else {
|
splashed(item);
|
}
|
break;
|
}
|
}
|
};
|
|
function convertToHierarchy(code) {
|
const codeStr = code.toString();
|
const provinceCode = codeStr.slice(0, 2) + '0000';
|
const cityCode = codeStr.slice(0, 4) + '00';
|
if (codeStr.slice(2, 4) === '00' && codeStr.slice(4, 6) === '00') {
|
return [provinceCode];
|
} else if (codeStr.slice(4, 6) === '00') {
|
return [provinceCode, cityCode];
|
}
|
return [provinceCode, cityCode, code];
|
}
|
|
const store = useStore();
|
const selectedAreaCode = computed(() => store.state.user.selectedAreaCode);
|
|
function getDeviceCount() {
|
return getDeviceRegionCount().then(res => {
|
return res?.data?.data || [];
|
});
|
}
|
|
function getDeviceList() {
|
return getDeviceRegion().then(res => {
|
return res?.data?.data || [];
|
});
|
}
|
|
watch(
|
selectedAreaCode,
|
async (newValue, oldValue) => {
|
if (newValue) {
|
const list = await getDeviceCount();
|
const splashedList = await getDeviceList();
|
console.log(list);
|
console.log(splashedList);
|
const code = newValue.slice(0, 6);
|
const hierarchy = convertToHierarchy(code);
|
const jsonPath = hierarchy.join('/');
|
const { VITE_APP_BASE } = import.meta.env;
|
const defaultDir = `${VITE_APP_BASE}public/geoJson/100000/`;
|
scalingJudgment = scalingJudgment.map(item => ({ ...item, show: true }));
|
if (hierarchy.length === 1) {
|
scalingJudgment[0].gJson = null;
|
scalingJudgment[0].splashedList = splashedList;
|
const gJson1 = await import(/* @vite-ignore */ `${defaultDir}${jsonPath}/indexDistrict.json`);
|
const gJson1Copy = JSON.parse(JSON.stringify(gJson1));
|
const gJson2 = await import(/* @vite-ignore */ `${defaultDir}${jsonPath}/index.json`);
|
const gJson2Copy = JSON.parse(JSON.stringify(gJson2));
|
const findFun = (item1, item) => Number(item1.region_code.slice(0, 6)) === item.properties.adcode;
|
scalingJudgment[1].gJson = {
|
...gJson1Copy,
|
features: gJson1Copy.features.map(item => ({
|
...item,
|
data: list.flatMap(item => item.childrens || []).find(item1 => findFun(item1, item)),
|
})),
|
};
|
scalingJudgment[2].gJson = {
|
...gJson2Copy,
|
features: gJson2Copy.features.map(item => ({ ...item, data: list.find(item1 => findFun(item1, item)) })),
|
};
|
const { lng, lat } = getCenterPoint(gJson2Copy.features.map(item => item.properties.center));
|
flyTo({ longitude: lng, latitude: lat }, 0, scalingJudgment[2].height);
|
}
|
if (hierarchy.length === 2) {
|
const gJson1 = await import(/* @vite-ignore */ `${defaultDir}${jsonPath}/index.json`);
|
scalingJudgment[0].gJson = null;
|
scalingJudgment[1].gJson = JSON.parse(JSON.stringify(gJson1));
|
scalingJudgment[2].gJson = null;
|
scalingJudgment[2].show = false;
|
const center = getCenterPoint(scalingJudgment[1].gJson.features.map(item => item.properties.center));
|
flyTo({ longitude: center.lng, latitude: center.lat }, 0, scalingJudgment[1].height);
|
}
|
if (hierarchy.length === 3) {
|
const gJson = await import(/* @vite-ignore */ `${defaultDir}${jsonPath.slice(0, -7)}/index.json`);
|
const gJson1 = gJson.features.find(
|
item => item.properties.adcode === Number(hierarchy[hierarchy.length - 1])
|
);
|
// scalingJudgment[0].gJson = { type: "FeatureCollection", features: [gJson1] }
|
scalingJudgment[0].gJson = null;
|
scalingJudgment[1].gJson = null;
|
scalingJudgment[1].show = false;
|
scalingJudgment[2].gJson = null;
|
scalingJudgment[2].show = false;
|
const center = gJson1.properties.center;
|
flyTo({ longitude: center[0], latitude: center[1] }, 0, scalingJudgment[0].height);
|
}
|
}
|
},
|
{ immediate: true, deep: true }
|
);
|
|
//散点机巢
|
function splashed(row) {
|
row.splashedList.forEach(item => {
|
viewer.entities.add({
|
position: Cesium.Cartesian3.fromDegrees(item.longitude, item.latitude),
|
label: {
|
// 随机整数
|
text: item.nickname,
|
font: '14pt monospace',
|
fillColor: Cesium.Color.WHITE,
|
outlineColor: Cesium.Color.BLACK,
|
outlineWidth: 2,
|
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
|
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
|
pixelOffset: new Cesium.Cartesian2(0, -9),
|
},
|
billboard: {
|
image: new Cesium.ConstantProperty(data1),
|
width: 24,
|
height: 24,
|
},
|
properties: {
|
customData: {
|
data: item,
|
},
|
},
|
});
|
});
|
}
|
|
// 聚合机巢
|
const aggregation = (item, flyTo) => {
|
if (!item.gJson) return;
|
const featuresList = item.gJson.features.map(item => ({
|
name: item.properties.name,
|
position: item.properties.center,
|
data: item.data,
|
id: item.region_code,
|
}));
|
// 遍历特征并添加实体
|
featuresList.forEach(feature => {
|
if (!feature.position) return;
|
const position = Cesium.Cartesian3.fromDegrees(feature.position[0], feature.position[1]);
|
viewer.entities.add({
|
id: feature.id,
|
position: position,
|
label: {
|
// 随机整数
|
text: feature.name + feature.data.total_device_count,
|
font: '14pt monospace',
|
fillColor: Cesium.Color.WHITE,
|
outlineColor: Cesium.Color.BLACK,
|
outlineWidth: 2,
|
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
|
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
|
pixelOffset: new Cesium.Cartesian2(0, -9),
|
},
|
billboard: {
|
image: new Cesium.ConstantProperty(data2),
|
width: 24,
|
height: 24,
|
},
|
properties: {
|
id: feature.id,
|
customData: {
|
data: feature.data,
|
},
|
},
|
});
|
});
|
// 加载新的 GeoJSON 数据
|
Cesium.GeoJsonDataSource.load(item.gJson).then(dataSource => {
|
viewer.dataSources.add(dataSource);
|
item.dataSource = dataSource; // 保存数据源以便后续删除
|
// 获取数据源中的实体
|
const entities = dataSource.entities.values;
|
entities.forEach(entity => {
|
entity.polygon.material = new Cesium.ColorMaterialProperty(
|
Cesium.Color.YELLOW.withAlpha(0) // 透明填充
|
);
|
entity.polygon.outline = new Cesium.ConstantProperty(true); // 显示边框
|
entity.polygon.outlineColor = new Cesium.ConstantProperty(Cesium.Color.YELLOW); // 黑色边框
|
});
|
flyTo &&
|
viewer.flyTo(dataSource, {
|
offset: new Cesium.HeadingPitchRange(
|
0, // heading: 0 (朝向不变)
|
Cesium.Math.toRadians(-90), // pitch: -90° (垂直向下)
|
0 // range: 0 (默认距离)
|
),
|
duration: 0.5,
|
});
|
});
|
};
|
|
// 获取弹框box
|
const getLabelDom = data => {
|
const vNode = h(MapPopUpBox, { data, removeLabel });
|
const tooltipContainer = document.createElement('div');
|
tooltipContainer.id = 'mapPopUpBox';
|
tooltipContainer.style.position = 'absolute';
|
tooltipContainer.style.transform = 'translateY(-50%)';
|
tooltipContainer.style.pointerEvents = 'none';
|
document.querySelector('.page-index').append(tooltipContainer);
|
render(vNode, tooltipContainer);
|
return tooltipContainer;
|
};
|
|
// 弹框位置刷新
|
const labelBox = () => {
|
let dom = document.querySelector('#mapPopUpBox');
|
if (!dom) {
|
const data = { ...currentEntity.requestData, ...currentEntity.customData };
|
console.log(data);
|
dom = getLabelDom(data);
|
}
|
const screenPosition = viewer.scene.cartesianToCanvasCoordinates(positionC3);
|
if (screenPosition) {
|
dom.style.left = `${screenPosition.x}px`;
|
dom.style.top = `${screenPosition.y}px`;
|
dom.style.display = 'block';
|
}
|
};
|
|
// 左键单机事件
|
const singleMachineEvent = async click => {
|
const pickedObject = viewer.scene.pick(click.position);
|
if (Cesium.defined(pickedObject) && pickedObject.id) {
|
const entity = pickedObject.id;
|
currentEntity = entity;
|
positionC3 = entity?.position?._value;
|
if (!positionC3) return;
|
removeLabel();
|
const customData = entity.properties.customData._value.data
|
console.log(customData);
|
if (customData.device_sn){
|
// todo
|
return
|
}
|
const areaCode = customData.region_code;
|
const res = await getDeviceInfoNum({ areaCode });
|
const resJob = await getTotalJobNum({ areaCode })
|
currentEntity = {
|
...entity,
|
requestData: {...res.data.data,jobNum:resJob.data.data},
|
customData: entity.properties.customData._value.data
|
};
|
viewer.scene.postRender.addEventListener(labelBox);
|
}
|
};
|
|
const removeDom = () => {
|
const dom = document.querySelector('#mapPopUpBox');
|
if (dom && dom.parentNode) {
|
dom.parentNode.removeChild(dom);
|
}
|
};
|
|
// 移除 点 和 gjson 实体
|
const removeEntities = () => {
|
viewer.dataSources?.removeAll(true);
|
viewer.entities?.removeAll();
|
};
|
// 移除弹框标签
|
const removeLabel = () => {
|
viewer.scene.postRender.removeEventListener(labelBox);
|
removeDom();
|
};
|
|
// 移除所有监听事件,变量置空
|
const removeAll = () => {
|
removeEntities();
|
removeLabel();
|
viewer.camera.moveEnd.removeEventListener(determineScaling);
|
handler?.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
handler?.destroy();
|
viewer = null;
|
active = null;
|
handler = null;
|
positionC3 = null;
|
};
|
const init = () => {
|
viewer = window.$viewer;
|
handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
|
handler.setInputAction(singleMachineEvent, Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
listenerHeight();
|
};
|
// onMounted(() => {
|
// nextTick(() => {
|
// viewer = window.$viewer;
|
// handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
|
// handler.setInputAction(singleMachineEvent, Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
// listenerHeight();
|
// });
|
// });
|
|
return { init, removeAll };
|
};
|