From 43e6b9ac2a96a5b644be74bf3170aa12abc87680 Mon Sep 17 00:00:00 2001 From: dongjiajun <1756465965@qq.com> Date: Fri, 5 Jul 2024 18:24:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=B0=E5=A2=9E=E7=82=B9?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ration/soilMoisture/index.vue | 16 ++++----- src/lib/cesiumUtils.js | 36 +++++++++++++++----- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/components/ration/soilMoisture/index.vue b/src/components/ration/soilMoisture/index.vue index 7a13c1b..89a7061 100644 --- a/src/components/ration/soilMoisture/index.vue +++ b/src/components/ration/soilMoisture/index.vue @@ -629,17 +629,15 @@ export default { // }); }, //选择点 - async selectPoint(){ + selectPoint(){ //启动新增点,点击地图,新增一个点 - let res = await cu.clickToAddPoint() - cu.addPoint(res.lng, res.lat, 10); - this.updatedPoint = { + cu.clickToAddPoint(point=>{ + this.updatedPoint = { ...this.currentSelectPoint, - lng: res.lng, - lat: res.lat + lng: point[0], + lat: point[1], } - console.log(this.updatedPoint,'查看点'); - console.log(this.simpleData[2],'查看simpleData'); + }) }, // 保存点 savePoint(){ @@ -657,6 +655,8 @@ export default { this.$refs.multipleTable.toggleAllSelection(); //关闭地图按钮 this.updatePointVisible = false; + //销毁监听 + cu.destroyClickToAddPoint() } }, computed: { diff --git a/src/lib/cesiumUtils.js b/src/lib/cesiumUtils.js index 2799674..afd431e 100644 --- a/src/lib/cesiumUtils.js +++ b/src/lib/cesiumUtils.js @@ -176,12 +176,16 @@ class CesiumUtils { this.viewer.entities.removeAll(); } //修改点的位置 - static clickToAddPoint(){ - return new Promise((resolve, reject) => { + static clickToAddPoint(callback){ + //清空之前的handler + if (this.handler) { + this.handler.destroy(); + } + // return new Promise((resolve, reject) => { var scene = this.viewer.scene; - var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas); + this.handler = new Cesium.ScreenSpaceEventHandler(scene.canvas); const that = this; - handler.setInputAction(function (movement) { + this.handler.setInputAction(function (movement) { // 清除历史单击点 // handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK); // movement.position 像素的x、y坐标 @@ -196,13 +200,29 @@ class CesiumUtils { var latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(6); let lng = Number(longitudeString); let lat = Number(latitudeString); - resolve({ lng, lat }); - }else{ - reject('未获取到坐标'); + const positions = [lng, lat]; + that.viewer.entities.removeById("theNewPoint"); + that.viewer.entities.add({ + id: "theNewPoint", + position: cartesian, + point: { + color: Cesium.Color.fromCssColorString('#9cf7e3'), + pixelSize: 10 + // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND + } + }); + //通过callback函数将数据传递到外部使用 + callback(positions); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); - }); + // }); } + //销毁监听 + static destroyClickToAddPoint(){ + this.handler.destroy(); + } + + //鼠标点击 static handleClick() { var scene = this.viewer.scene;