无人机管理后台前端(已迁走)
shuishen
2025-04-14 6c6f73db4de17e6e6607a0fbecd1af0a079f5e91
src/components/map-container/mapContainer.vue
@@ -2,8 +2,8 @@
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2024-10-25 15:07:51
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2025-04-10 13:04:30
 * @FilePath: \drone-web-manage\src\components\map-container\MapContainer.vue
 * @LastEditTime: 2025-04-14 23:09:33
 * @FilePath: \drone-web-manage\src\components\map-container\mapContainer.vue
 * @Description: 
 * 
 * Copyright (c) 2024 by shuishen, All Rights Reserved. 
@@ -20,10 +20,13 @@
<script setup>
import { nextTick, onMounted, onUnmounted } from 'vue'
import { read } from 'xlsx'
window.$viewer = null
window.$Cesium = null
let pointLayer = null
let polylineLayer = null
const { VITE_APP_BASE } = import.meta.env
// import * as Cesium from 'cesium'
// import 'cesium/Build/Cesium/Widgets/widgets.css'
@@ -36,40 +39,14 @@
})
watch(
    [() => isViewerReady.value, () => rowDetails],
    ([ready, detail]) => {
        if (ready && detail?.location) {
            const [lng, lat] = detail.location
            if (!lng || !lat) return
            let [newLng, newLat] = DC.CoordTransform.GCJ02ToWGS84(lng, lat)
            window.$viewer?.zoomToPosition(new DC.Position(
                newLng,
                newLat,
                1000,
                0,
                -90,
                0
            ), () => {
            })
            let point = new DC.Point(new DC.Position(newLng, newLat))
            pointLayer.addOverlay(point)
        }
    },
    { deep: true, immediate: true } // 初始化时立即执行
)
function initMap () {
async function initMap () {
    if (window.$viewer) return
    nextTick(() => {
        DC.ready({
    await DC.ready({
            // Cesium: Cesium,
            baseUrl: `${VITE_APP_BASE}/libs/dc-sdk/resources/`,
        }).then(() => {
    })
            window.$Cesium = DC.getLib('Cesium')
            // 天地图地图
@@ -96,20 +73,88 @@
            pointLayer = new DC.VectorLayer('pointLayer')
            window.$viewer?.addLayer(pointLayer)
    polylineLayer = new DC.VectorLayer('polylineLayer')
    window.$viewer?.addLayer(polylineLayer)
            isViewerReady.value = true
}
/**
 * 初始化标注添加
 * @param type 类型
 * @param data 数据
 */
const initAddEntity = (type, data) => {
    watch(() => isViewerReady.value,
        (ready) => {
            if (ready) {
                type === 'point' ? addPoint(data) : addPolyline(data)
            }
        },
        { deep: true, immediate: true } // 初始化时立即执行
    )
}
/**
 * 添加点标注
 * @param data 数据  数据格式 [lng, lat]
 */
function addPoint (data) {
    const [lng, lat] = data
    if (!lng || !lat) return
    let [newLng, newLat] = DC.CoordTransform.GCJ02ToWGS84(lng, lat)
    let point = new DC.Point(new DC.Position(newLng, newLat))
    pointLayer.addOverlay(point)
    window.$viewer?.zoomTo(pointLayer)
}
/**
 * 添加点标注
 * @param data 数据  数据格式 [[lng, lat], [lng, lat], [lng, lat]]
 */
function addPolyline (data) {
    if (data.length === 0) return
    const positionStr = data.map(item => {
        const [lng, lat] = item
        let [newLng, newLat] = DC.CoordTransform.GCJ02ToWGS84(lng, lat)
        return `${newLng}, ${newLat}`
    }).join(';')
    let polyline = new DC.Polyline(positionStr)
    polyline.setStyle({
        width: 20,
        material: new DC.PolylineLightingTrailMaterialProperty({
            color: DC.Color.BLUE,
            speed: 5.0
        }),
        clampToGround: true
        })
    })
    polylineLayer.addOverlay(polyline)
    window.$viewer?.zoomTo(polylineLayer)
}
onMounted(() => {
    nextTick(() => {
    initMap()
    })
})
onUnmounted(() => {
    if (pointLayer) {
        window.$viewer?.removeLayer(pointLayer)
        pointLayer = null
    }
    if (polylineLayer) {
        window.$viewer?.removeLayer(polylineLayer)
        polylineLayer = null
    }
    window.$viewer?.entities.removeAll()
@@ -130,6 +175,10 @@
        cesiumContainer.remove() // 移除与地图相关的DOM元素
    }
})
defineExpose({
    initAddEntity
})
</script>
<script>