无人机管理后台前端(已迁走)
rjg
2025-06-10 05fea3e893dae5a715c6ca78116df8da2d19a5eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!--
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2024-10-25 15:07:51
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2025-04-28 11:34:40
 * @FilePath: \drone-web-manage\src\components\map-container\mapContainer.vue
 * @Description: 
 * 
 * Copyright (c) 2024 by shuishen, All Rights Reserved. 
-->
<template>
    <div class="map-container">
        <div class="viewer-container ztzf-cesium" id="viewer-container">
            <div class="content">
                <slot name="content"></slot>
            </div>
        </div>
    </div>
</template>
 
<script setup>
import * as Cesium from 'cesium'
import { Cartesian3, Terrain, Viewer } from 'cesium'
import { PublicCesium } from '@/utils/cesium/publicCesium'
import ImageTrailMaterial from '@/utils/cesium/ImageTrailMaterial'
import { flyVisual } from '@/utils/cesium/mapUtil'
import * as turf from '@turf/turf'
 
import { nextTick, onMounted, onUnmounted } from 'vue'
import { read } from 'xlsx'
 
import startPng from '@/assets/map_images/Startingpointicon.png'
import endPng from '@/assets/map_images/EndPointicon.png'
import rwqfdImg from '@/assets/images/task/arrow-right-blue.png'
import newNumPoint from '@/assets/images/task/custom-point.png'
 
window.$viewer = null
window.$Cesium = null
let pointLayer = null
let polylineLayer = null
let pointHtmlLayer = null
 
const { VITE_APP_BASE } = import.meta.env
// import * as Cesium from 'cesium'
// import 'cesium/Build/Cesium/Widgets/widgets.css'
const isViewerReady = ref(false)
const { rowDetails } = defineProps({
    rowDetails: {
        type: Object,
        default: () => ({}),
    }
})
 
 
async function initMap () {
    if (window.$viewer) return
    window.$Cesium = new PublicCesium({ dom: 'viewer-container' }).getViewer()
    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
 
    window.$Cesium.entities.add({
        position: Cartesian3.fromDegrees(lng, lat),
        point: {
            pixelSize: 10,
            color: Cesium.Color.BLUE,
            outlineColor: Cesium.Color.WHITE,
            outlineWidth: 2
        }
    })
 
    // 定位到点位
    const points = [[lng, lat]]  // 确保格式为二维数组
    flyVisual(points, window.$Cesium, 4)
}
 
/**
 * 添加点标注
 * @param data 数据  数据格式 [[lng, lat], [lng, lat], [lng, lat]]
 */
function addPolyline (data) {
    if (polylineLayer) polylineLayer.clear()
    if (pointHtmlLayer) pointHtmlLayer.clear()
 
    if (data.length === 0) return
 
    const positionStr = data.map(item => {
        const [lng, lat] = item
 
        return Cartesian3.fromDegrees(Number(lng), Number(lat))
    })
    // 路径线
    window.$Cesium.entities.add({
        id: `polyline`,
        polyline: {
            width: 4,
            positions: positionStr,
            material: new ImageTrailMaterial({
                image: rwqfdImg,  // 使用导入的图片
                repeat: {
                    x: 10,  // 调整图片重复次数
                    y: 1
                },
                speed: 10,  // 调整速度
                color: Cesium.Color.WHITE  // 可以调整图片的颜色混合
            }),
            clampToGround: false,
        },
    })
    positionStr.forEach((point, index) => {
        let setting = {}
        if (index === 0) {
            setting = {
                position: point,
                billboard: {
                    image: startPng,
                    outlineWidth: 0,
                    width: 30,
                    height: 30,
                    scale: 1.0,
                },
            }
        } else if (index === positionStr.length - 1) {
            setting = {
                position: point,
                id: `point_${index}`,
                billboard: {
                    image: endPng,
                    outlineWidth: 0,
                    width: 30,
                    height: 30,
                    scale: 1.0,
                    verticalOrigin: Cesium.VerticalOrigin.TOP, // 添加这行确保图标正确显示
                    pixelOffset: new Cesium.Cartesian2(0, -20), // 根据需要调整偏移量
                },
            }
        } else {
            setting = {
                position: point,
                id: `point_${index}`,
                label: {
                    text: `${index}`,
                    font: 'bold 14px serif',
                    fillColor: Cesium.Color.WHITE,
                    pixelOffset: new Cesium.Cartesian2(1, 0), // 根据需要调整偏移量
                    eyeOffset: new Cesium.Cartesian3(0, 0, -10), // 使标签在点的上方
                },
                billboard: {
                    image: new Cesium.ConstantProperty(newNumPoint),
                    width: 30,
                    height: 30,
                },
                offset: new Cesium.Cartesian2(10, 30),
            }
        }
        window.$Cesium.entities.add(setting)
    })
    // 定位到点位
    flyVisual(data, window.$Cesium, 4)
}
 
onMounted(() => {
    nextTick(() => {
        initMap()
    })
})
 
onUnmounted(() => {
    if (pointLayer) {
        window.$viewer?.removeLayer(pointLayer)
        pointLayer = null
    }
 
    if (polylineLayer) {
        window.$viewer?.removeLayer(polylineLayer)
        polylineLayer = null
    }
 
    window.$viewer?.entities.removeAll()
    window.$viewer?.imageryLayers.removeAll()
    window.$viewer?.dataSources.removeAll()
    // let gl = window.$viewer.scene.context._originalGLContext
    // gl.canvas.width = 1
    // gl.canvas.height = 1
 
    window.$viewer && window.$viewer.setTerrain()
    window.$viewer && window.$viewer.destroy()
    window.$viewer = null
    delete window.$viewer
    window.$Cesium = null
    delete window.$Cesium
    var cesiumContainer = document.getElementById('viewer-container')
    if (cesiumContainer) {
        cesiumContainer.remove() // 移除与地图相关的DOM元素
    }
})
 
defineExpose({
    initAddEntity
})
</script>
 
<script>
export default {
    name: 'MapContainer'
}
</script>
 
<style lang="scss" scoped>
.map-container {
    position: relative;
    width: 100% !important;
    height: 100% !important;
    overflow: hidden;
}
 
.viewer-container {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 120%;
    height: 120%;
    transform: translate(-50%, -50%);
}
</style>