From 3667807a7b7418efc090ee3fa6a6b734bc3080bf Mon Sep 17 00:00:00 2001
From: xieb <vip_xiaobin810@163.com>
Date: Wed, 13 Sep 2023 20:36:29 +0800
Subject: [PATCH] Merge branch 'develop' of http://s16s652780.51mypc.cn:49896/r/yskj/iot_drone_web into develop

---
 src/components/cesiumMap/cesium.vue |  181 ++++++++++++++++++++++++++++++++++-----------
 1 files changed, 136 insertions(+), 45 deletions(-)

diff --git a/src/components/cesiumMap/cesium.vue b/src/components/cesiumMap/cesium.vue
index e6750f5..946f26c 100644
--- a/src/components/cesiumMap/cesium.vue
+++ b/src/components/cesiumMap/cesium.vue
@@ -1,29 +1,35 @@
-<!--
- * @Author: 胡思旗 931347610@qq.com
- * @Date: 2023-08-22 17:50:30
- * @LastEditors: husq 931347610@qq.com
- * @LastEditTime: 2023-09-01 16:48:42
- * @FilePath: \Cloud-API-Demo-Web\src\components\cesiumMap\cesium.vue
- * @Description:
- *
- * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
--->
 <template>
-      <div class="height-100 width-100 cesium" id="cesiumContainer" @click="getCoordinate"></div>
+  <div class="height-100 width-100 cesium" id="cesiumContainer"></div>
+  <div v-if="centerConfig.type && !centerConfig.latitude" class="pointLongitude">在地图上点击绘制项目中心点</div>
+  <div class="switch">
+    <div class="2d" v-if="dimension === 3" @click="switchModel('2D')">2D</div>
+    <div class="3d" v-else @click="switchModel('3D')">3D</div>
+  </div>
 </template>
 
 <script setup lang="ts">
 import * as Cesium from 'cesium'
-import { onMounted, ref, onUnmounted } from 'vue'
-const viewer: { value: Cesium.Viewer | null | undefined } = ref()
+import { onMounted, ref, onUnmounted, } from 'vue'
+import { pointCenter, clickPoint } from '/@/hooks/use-center-point'
+import { useMyStore } from '/@/store'
+let viewer: Cesium.Viewer | null = null
+let handler: Cesium.ScreenSpaceEventHandler
+const dimension = ref(3)
+const { appContext } = getCurrentInstance()
+const global = appContext.config.globalProperties
+// const viewer: { value: Cesium.Viewer | null | undefined } = ref()
+const store = useMyStore()
+const centerConfig = computed(() => {
+  return store.state.map.centerConfig
+})
 const init = () => {
   // Cesium Token
-  const cesiumToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJhMjdmNGUxZC02YzY3LTQyZWUtOTNmYy1hNTI0MDRkZDY2ZmEiLCJpZCI6MTYxODgyLCJpYXQiOjE2OTI3NTQyNDV9.Pm7xTwPmKowPzFgJ0TsIKOtthigq86BLJX4c8M97Hhw'
+  const cesiumToken = import.meta.env.VITE_CESIUM_TOKEN
   Cesium.Ion.defaultAccessToken = cesiumToken
   Cesium.Camera.DEFAULT_VIEW_FACTOR = -0.45
   // 西南东北,默认显示中国
   Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(110, -25, 110, 90)
-  viewer.value = new Cesium.Viewer('cesiumContainer', {
+  viewer = new Cesium.Viewer('cesiumContainer', {
     infoBox: false, // 禁用沙箱,解决控制台报错
     animation: false, // 左下角的动画仪表盘
     baseLayerPicker: false, // 右上角的图层选择按钮
@@ -32,9 +38,15 @@
     sceneModePicker: false, // 模式切换按钮
     timeline: false, // 底部的时间轴
     navigationHelpButton: false, // 右上角的帮助按钮,
+    selectionIndicator: false, // 是否显示选择指示器
     baseLayer: false,
   })
+  handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
   loadLAYER()
+  viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK) // 禁用双击
+  // viewer.scene.screenSpaceCameraController.minimumZoomDistance = 100
+  viewer.scene.screenSpaceCameraController.maximumZoomDistance = 4500000
+  global.$viewer = viewer
 }
 // 加载图层、注解方法
 const loadLAYER = () => {
@@ -42,12 +54,12 @@
   const TDT_Token = 'c6eea7dad4fa1e2d1e32ec0e7c9735db'
   // 天地图地图
   const TDT_IMG_C = 'http://{s}.tianditu.gov.cn/img_c/wmts?service=wmts&request=GetTile&version=1.0.0' +
-        '&LAYER=img&tileMatrixSet=c&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}' +
-        '&style=default&format=tiles&tk=' + TDT_Token
+    '&LAYER=img&tileMatrixSet=c&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}' +
+    '&style=default&format=tiles&tk=' + TDT_Token
   // 天地图注记
   const TDT_ZJ = 'http://{s}.tianditu.gov.cn/cia_c/wmts?service=wmts&request=GetTile&version=1.0.0' +
-        '&LAYER=cia&tileMatrixSet=c&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}' +
-        '&style=default&format=tiles&tk=' + TDT_Token
+    '&LAYER=cia&tileMatrixSet=c&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}' +
+    '&style=default&format=tiles&tk=' + TDT_Token
   // 天地图图层加载
   const imageryProvider = new Cesium.WebMapTileServiceImageryProvider({
     url: TDT_IMG_C,
@@ -60,7 +72,7 @@
     tileMatrixLabels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'],
     maximumLevel: 50,
   })
-  viewer.value?.imageryLayers.addImageryProvider(imageryProvider)
+  viewer?.imageryLayers.addImageryProvider(imageryProvider)
   // 天地图中文注记加载
   const annotation = new Cesium.WebMapTileServiceImageryProvider({
     url: TDT_ZJ,
@@ -73,31 +85,18 @@
     tileMatrixLabels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'],
     maximumLevel: 50,
   })
-  viewer.value?.imageryLayers.addImageryProvider(annotation)
+  viewer?.imageryLayers.addImageryProvider(annotation)
 }
-// 相机设置
-const camera = () => {}
-// 获取地图点击坐标
-const getCoordinate = () => {
-  let longitude: number, latitude: number
-  viewer.value?.screenSpaceEventHandler.setInputAction((e: any) => {
-    const cartesian = viewer.value?.camera.pickEllipsoid(e.position, viewer.value?.scene.globe.ellipsoid)
-    if (cartesian) {
-      const cartographic = Cesium.Cartographic.fromCartesian(cartesian)
-      longitude = Cesium.Math.toDegrees(cartographic.longitude)
-      latitude = Cesium.Math.toDegrees(cartographic.latitude)
-    }
-    Point(longitude, latitude)
-    return { longitude, latitude }
-  }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
-}
-
-const Point = (longitude:number, latitude:number) => {
-  const entity = viewer.value?.entities.add({
+const Point = (longitude: number, latitude: number) => {
+  // 移除地图所有点 重新绘制
+  viewer?.entities.removeAll()
+  const entity = viewer?.entities.add({
     position: Cesium.Cartesian3.fromDegrees(longitude, latitude),
     point: {
-      pixelSize: 10,
-      color: Cesium.Color.YELLOW
+      pixelSize: 24,
+      color: Cesium.Color.GREEN,
+      outlineColor: Cesium.Color.WHITE,
+      outlineWidth: 2,
     }
   })
   // // 将相机定位到标记点的位置
@@ -107,21 +106,113 @@
   // })
 }
 
+// 切换为二三维模式
+const switchModel = (type: string) => {
+  switch (type) {
+    case '2D':
+      viewer.scene.camera.setView({
+        orientation: {
+          pitch: Cesium.Math.toRadians(-60),
+          heading: Cesium.Math.toRadians(0),
+        }
+      })
+      dimension.value = 2
+      break
+    case '3D':
+      viewer.scene.camera.setView({
+        orientation: {
+          pitch: Cesium.Math.toRadians(-90),
+          heading: Cesium.Math.toRadians(0),
+        }
+      })
+      dimension.value = 3
+  }
+}
+
+// 设置项目中所有的项目中心坐标
+watch(() => store.state.map.pointList, (newVal) => {
+  if (newVal) {
+    pointCenter(viewer, newVal)
+    clickPoint(viewer, newVal)
+  }
+}, {
+  deep: true
+})
+// 点击地图生成项目中心点
+watch(() => store.state.map.centerConfig.type, (newVal) => {
+  if (newVal === false || !store.state.map.centerConfig.type) {
+    viewer?.entities.removeAll()
+    handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
+  } else {
+    handler.setInputAction(function (e: any) {
+      const cartesian = viewer?.camera.pickEllipsoid(e.position, viewer?.scene.globe.ellipsoid)
+      if (cartesian) {
+        const cartographic = Cesium.Cartographic.fromCartesian(cartesian)
+        const longitude = Cesium.Math.toDegrees(cartographic.longitude)
+        const latitude = Cesium.Math.toDegrees(cartographic.latitude)
+        const data = {
+          longitude,
+          latitude,
+        }
+        store.commit('SET_CENTER_CONFIG_LATITUDE', data)
+        Point(longitude, latitude)
+      }
+    }, Cesium.ScreenSpaceEventType.LEFT_CLICK
+    )
+  }
+}, {
+  deep: true
+})
 onMounted(() => {
   init()
 })
 // 销毁地图模型
 onUnmounted(() => {
-  viewer.value = null
+  viewer = null
 })
 
 </script>
 
 <style scoped lang="scss">
-.cesium{
-  :deep(.cesium-viewer-bottom){
+.cesium {
+  :deep(.cesium-viewer-bottom) {
     display: none !important;
   }
 }
 
+.pointLongitude {
+  background-color: rgba(20, 20, 20, 0.792);
+  position: absolute;
+  z-index: 30;
+  bottom: 30px;
+  color: #fff;
+  left: 0;
+  right: 0;
+  margin: 0 80px;
+  padding: 15px 0;
+  text-align: center;
+  font-size: 16px;
+}
+
+.switch {
+  position: absolute;
+  background-color: #fff;
+  z-index: 30;
+  bottom: 80px;
+  font-size: 14px;
+  right: 20px;
+  cursor: pointer;
+  box-shadow: 0 0 4px 0 rgba(0, 0, 0, .6);
+  width: 28px;
+  height: 28px;
+  color: #4e4e4e;
+  font-weight: 600;
+  display: flex;
+  align-items: center;
+  justify-content: center;
+}
+
+.switch:hover {
+  color: #1180ff;
+}
 </style>

--
Gitblit v1.9.3