吉安感知网项目-前端
张含笑
2026-01-29 9fb4de853fc8aab2000f312ba2b8907e3dc9a61d
applications/drone-command/src/views/areaManage/partition/shapeTools/draw/DrawBufferCircleTool.js
@@ -8,10 +8,19 @@
      super(viewer)
      this.tooltip = new MapTooltip(viewer)
      this.dataSource = null
      this.circleEntity = null
      this.centerCartesian = null
      this.radius = 0
      this.radiusLevel1 = 0
      this.radiusLevel2 = 0
      this.radiusLevel3 = 0
      this.circleLevel1Entity = null
      this.circleLevel2Entity = null
      this.circleLevel3Entity = null
      this.circleLevel1OutlineEntity = null
      this.circleLevel2OutlineEntity = null
      this.circleLevel3OutlineEntity = null
      this.isDrawing = false
      this.drawStep = 0
      this.isCompleted = false
   }
   start() {
@@ -20,54 +29,159 @@
      this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
      this.handler.setInputAction(click => this.handleLeftClick(click), Cesium.ScreenSpaceEventType.LEFT_CLICK)
      this.handler.setInputAction(movement => this.handleMouseMove(movement), Cesium.ScreenSpaceEventType.MOUSE_MOVE)
      this.drawStep = 0
      this.isCompleted = false
      this.tooltip.show('单击设置中心点', { x: 0, y: 0 })
   }
   handleLeftClick(click) {
      if (this.isCompleted) return
      const position = this.getPositionFromScreen(click.position)
      if (!position) return
      if (!this.centerCartesian) {
      if (this.drawStep === 0) {
         this.centerCartesian = position
         this.radius = 1
         this.radiusLevel1 = 1
         this.radiusLevel2 = 1
         this.radiusLevel3 = 1
         this.isDrawing = true
         this.drawStep = 1
         this.createEntities()
         this.tooltip.show('单击结束绘制', click.position)
         this.tooltip.show('单击设置一级缓冲圆', click.position)
         return
      }
      if (this.isDrawing) {
      if (this.drawStep === 1) {
         const radius = Math.max(1, this.getSurfaceDistance(this.centerCartesian, position))
         this.radiusLevel1 = radius
         this.radiusLevel2 = radius
         this.radiusLevel3 = radius
         this.drawStep = 2
         this.tooltip.show('单击设置二级缓冲圆', click.position)
         return
      }
      if (this.drawStep === 2) {
         const radius = Math.max(1, this.getSurfaceDistance(this.centerCartesian, position))
         this.radiusLevel2 = Math.max(radius, this.radiusLevel1)
         this.radiusLevel3 = this.radiusLevel2
         this.drawStep = 3
         this.tooltip.show('单击设置三级缓冲圆', click.position)
         return
      }
      if (this.drawStep === 3) {
         const radius = Math.max(1, this.getSurfaceDistance(this.centerCartesian, position))
         this.radiusLevel3 = Math.max(radius, this.radiusLevel2)
         this.isDrawing = false
         const positions = buildEllipsePositions(this.centerCartesian, this.radius, this.radius)
         this.notify('getPolygonPositions', positions)
         this.drawStep = 0
         this.isCompleted = true
         const positions = buildEllipsePositions(this.centerCartesian, this.radiusLevel3, this.radiusLevel3)
         this.notify('getPolygonPositions', {
            positions,
            meta: {
               center: this.centerCartesian,
               bufferRadii: [this.radiusLevel1, this.radiusLevel2, this.radiusLevel3],
            },
         })
         this.tooltip.hide()
         this.clearPreviewEntities()
      }
   }
   handleMouseMove(movement) {
      if (!this.isDrawing) {
         this.tooltip.move(movement.endPosition)
         return
      }
      if (this.isCompleted) return
      this.tooltip.move(movement.endPosition)
      if (!this.isDrawing) return
      const position = this.getPositionFromScreen(movement.endPosition)
      if (!position) return
      this.radius = Math.max(1, this.getSurfaceDistance(this.centerCartesian, position))
      this.tooltip.move(movement.endPosition)
      if (this.drawStep === 1) {
         const radius = Math.max(1, this.getSurfaceDistance(this.centerCartesian, position))
         this.radiusLevel1 = radius
         this.radiusLevel2 = radius
         this.radiusLevel3 = radius
         return
      }
      if (this.drawStep === 2) {
         const radius = Math.max(1, this.getSurfaceDistance(this.centerCartesian, position))
         this.radiusLevel2 = Math.max(radius, this.radiusLevel1)
         this.radiusLevel3 = this.radiusLevel2
         return
      }
      if (this.drawStep === 3) {
         const radius = Math.max(1, this.getSurfaceDistance(this.centerCartesian, position))
         this.radiusLevel3 = Math.max(radius, this.radiusLevel2)
      }
   }
   createEntities() {
      if (this.circleEntity) return
      this.circleEntity = this.dataSource.entities.add({
      if (this.circleLevel1Entity || this.circleLevel2Entity || this.circleLevel3Entity) return
      this.circleLevel1Entity = this.dataSource.entities.add({
         position: new Cesium.CallbackProperty(() => this.centerCartesian, false),
         ellipse: {
            semiMajorAxis: new Cesium.CallbackProperty(() => this.radius, false),
            semiMinorAxis: new Cesium.CallbackProperty(() => this.radius, false),
            material: Cesium.Color.fromBytes(45, 140, 240, 99),
            outline: true,
            outlineColor: Cesium.Color.fromBytes(45, 140, 240, 255),
            outlineWidth: 2,
            semiMajorAxis: new Cesium.CallbackProperty(() => this.radiusLevel1, false),
            semiMinorAxis: new Cesium.CallbackProperty(() => this.radiusLevel1, false),
            material: Cesium.Color.fromBytes(247, 20, 20, 128),
            outline: false,
            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
         },
      })
      this.circleLevel1OutlineEntity = this.dataSource.entities.add({
         polyline: {
            positions: new Cesium.CallbackProperty(() => {
               if (!this.centerCartesian || this.radiusLevel1 <= 0) return []
               const positions = buildEllipsePositions(this.centerCartesian, this.radiusLevel1, this.radiusLevel1)
               return positions.length ? [...positions, positions[0]] : positions
            }, false),
            clampToGround: true,
            width: 2,
            material: Cesium.Color.fromBytes(255, 0, 0, 255),
         },
      })
      this.circleLevel2Entity = this.dataSource.entities.add({
         position: new Cesium.CallbackProperty(() => this.centerCartesian, false),
         ellipse: {
            semiMajorAxis: new Cesium.CallbackProperty(() => this.radiusLevel2, false),
            semiMinorAxis: new Cesium.CallbackProperty(() => this.radiusLevel2, false),
            material: Cesium.Color.fromBytes(255, 235, 59, 128),
            outline: false,
            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
         },
      })
      this.circleLevel2OutlineEntity = this.dataSource.entities.add({
         polyline: {
            positions: new Cesium.CallbackProperty(() => {
               if (!this.centerCartesian || this.radiusLevel2 <= 0) return []
               const positions = buildEllipsePositions(this.centerCartesian, this.radiusLevel2, this.radiusLevel2)
               return positions.length ? [...positions, positions[0]] : positions
            }, false),
            clampToGround: true,
            width: 2,
            material: Cesium.Color.fromBytes(255, 235, 59, 255),
         },
      })
      this.circleLevel3Entity = this.dataSource.entities.add({
         position: new Cesium.CallbackProperty(() => this.centerCartesian, false),
         ellipse: {
            semiMajorAxis: new Cesium.CallbackProperty(() => this.radiusLevel3, false),
            semiMinorAxis: new Cesium.CallbackProperty(() => this.radiusLevel3, false),
            material: Cesium.Color.fromBytes(25, 178, 230, 128),
            outline: false,
            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
         },
      })
      this.circleLevel3OutlineEntity = this.dataSource.entities.add({
         polyline: {
            positions: new Cesium.CallbackProperty(() => {
               if (!this.centerCartesian || this.radiusLevel3 <= 0) return []
               const positions = buildEllipsePositions(this.centerCartesian, this.radiusLevel3, this.radiusLevel3)
               return positions.length ? [...positions, positions[0]] : positions
            }, false),
            clampToGround: true,
            width: 2,
            material: Cesium.Color.fromBytes(0, 251, 255, 255),
         },
      })
   }
@@ -75,7 +189,12 @@
   clearPreviewEntities() {
      if (!this.dataSource) return
      this.dataSource.entities.removeAll()
      this.circleEntity = null
      this.circleLevel1Entity = null
      this.circleLevel2Entity = null
      this.circleLevel3Entity = null
      this.circleLevel1OutlineEntity = null
      this.circleLevel2OutlineEntity = null
      this.circleLevel3OutlineEntity = null
   }
   getSurfaceDistance(startCartesian, endCartesian) {
@@ -107,7 +226,11 @@
      this.tooltip?.destroy()
      this.tooltip = null
      this.centerCartesian = null
      this.radius = 0
      this.radiusLevel1 = 0
      this.radiusLevel2 = 0
      this.radiusLevel3 = 0
      this.isDrawing = false
      this.drawStep = 0
      this.isCompleted = false
   }
}