From 6c6f73db4de17e6e6607a0fbecd1af0a079f5e91 Mon Sep 17 00:00:00 2001
From: shuishen <1109946754@qq.com>
Date: Mon, 14 Apr 2025 23:13:06 +0800
Subject: [PATCH] 地图组件调整,增加添加线方法 调用地图组件调整,更换方法加载

---
 src/components/map-container/mapContainer.vue |  177 +++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 113 insertions(+), 64 deletions(-)

diff --git a/src/components/map-container/mapContainer.vue b/src/components/map-container/mapContainer.vue
index c8651f0..e74385f 100644
--- a/src/components/map-container/mapContainer.vue
+++ b/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,80 +39,122 @@
 })
 
 
-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({
-            // Cesium: Cesium,
-            baseUrl: `${VITE_APP_BASE}/libs/dc-sdk/resources/`,
-        }).then(() => {
-            window.$Cesium = DC.getLib('Cesium')
-
-            // 天地图地图
-            const imageryProvider_standZh = new window.$Cesium.UrlTemplateImageryProvider({
-                url: 'https://t{s}.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=e45274b0235bb913eceb393aabbf9c9c',
-                subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
-                maximumLevel: 18,
-                credit: 'stand_zj',
-            })
-            const imageryProvider_stand = new window.$Cesium.UrlTemplateImageryProvider({
-                url: 'https://t{s}.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=e45274b0235bb913eceb393aabbf9c9c',
-                subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
-                // format: 'image/jpeg',
-                // show: true,
-                maximumLevel: 18,
-                credit: 'stand_tc',
-            })
-
-            window.$viewer = new DC.Viewer('viewer-container')
-            window.$viewer.locationBar.enable = false
-
-            window.$viewer?.imageryLayers.addImageryProvider(imageryProvider_stand)
-            window.$viewer?.imageryLayers.addImageryProvider(imageryProvider_standZh)
-
-            pointLayer = new DC.VectorLayer('pointLayer')
-            window.$viewer?.addLayer(pointLayer)
-
-            isViewerReady.value = true
-        })
+    await DC.ready({
+        // Cesium: Cesium,
+        baseUrl: `${VITE_APP_BASE}/libs/dc-sdk/resources/`,
     })
+
+    window.$Cesium = DC.getLib('Cesium')
+
+    // 天地图地图
+    const imageryProvider_standZh = new window.$Cesium.UrlTemplateImageryProvider({
+        url: 'https://t{s}.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=e45274b0235bb913eceb393aabbf9c9c',
+        subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
+        maximumLevel: 18,
+        credit: 'stand_zj',
+    })
+    const imageryProvider_stand = new window.$Cesium.UrlTemplateImageryProvider({
+        url: 'https://t{s}.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=e45274b0235bb913eceb393aabbf9c9c',
+        subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
+        // format: 'image/jpeg',
+        // show: true,
+        maximumLevel: 18,
+        credit: 'stand_tc',
+    })
+
+    window.$viewer = new DC.Viewer('viewer-container')
+    window.$viewer.locationBar.enable = false
+
+    window.$viewer?.imageryLayers.addImageryProvider(imageryProvider_stand)
+    window.$viewer?.imageryLayers.addImageryProvider(imageryProvider_standZh)
+
+    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(() => {
-    initMap()
+    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>

--
Gitblit v1.9.3