/* * @Author: shuishen 1109946754@qq.com * @Date: 2022-10-19 14:30:47 * @LastEditors: shuishen 1109946754@qq.com * @LastEditTime: 2022-10-19 14:41:28 * @FilePath: \srs-police-affairs\src\utils\1.js * @Description: * * Copyright (c) 2022 by shuishen 1109946754@qq.com, All Rights Reserved. */ export default class EntityDraw { drawPolygonLayer = null drawPointLayer = null activeShapePoints = [] activeShape = null constructor() { this.initLayer() } initLayer () { // 添加面图层 this.drawPolygonLayer = new global.DC.VectorLayer('drawPolygonLayer') global.viewer.addLayer(this.drawPolygonLayer) // 添加点图层 this.drawPointLayer = new global.DC.VectorLayer('drawPointLayer') global.viewer.addLayer(this.drawPointLayer) } activate () { this.deactivate() //鼠标左键点击事件 鼠标左键点击拾取需要编辑的对象 this.initLeftClickEventHandler() } deactivate () { global.viewer.on(global.DC.MouseEventType.DB_CLICK, e => { return }) } registerEvents () { global.viewer.tooltip.enable = true global.viewer.on(global.DC.MouseEventType.CLICK, this.leftEvent) global.viewer.on(global.DC.MouseEventType.MOUSE_MOVE, this.moveEvent) global.viewer.on(global.DC.MouseEventType.RIGHT_CLICK, this.rightEvent) } unRegisterEvents () { global.viewer.tooltip.enable = false global.viewer.off(global.DC.MouseEventType.CLICK, this.leftEvent) global.viewer.off(global.DC.MouseEventType.MOUSE_MOVE, this.moveEvent) global.viewer.off(global.DC.MouseEventType.RIGHT_CLICK, this.rightEvent) } drawGraph (positionData) { let graph graph = global.viewer.entities.add({ polygon: { hierarchy: positionData, material: new global.DC.Namespace.Cesium.ColorMaterialProperty( global.DC.Namespace.Cesium.Color.NAVAJOWHITE.withAlpha(0.7) ), } }) return graph } leftEvent (event) { let point = new global.DC.Point(new global.DC.Position(event.wgs84SurfacePosition.lng, event.wgs84SurfacePosition.lat)) point.setStyle({ pixelSize: 10, color: global.DC.Color.RED, //颜色 outlineColor: global.DC.Color.WHITE, //边框颜色 outlineWidth: 2, //边框大小, }) this.drawPointLayer.addOverlay(point) if (this.activeShapePoints.length === 0) { this.activeShapePoints.push(event.surfacePosition) const dynamicPositions = new global.DC.Namespace.Cesium.CallbackProperty(function () { return new global.DC.Namespace.Cesium.PolygonHierarchy(this.activeShapePoints) }, false) this.activeShape = this.drawGraph(dynamicPositions) } this.activeShapePoints.push(event.surfacePosition) } moveEvent (event) { global.viewer.tooltip.showAt(event.windowPosition, '左击选择点位,右击结束') if (this.activeShapePoints.length >= 2) { this.activeShapePoints.pop() this.activeShapePoints.push(event.surfacePosition) } } rightEvent (event) { if (this.activeShapePoints.length < 4) { global.viewer.tooltip.showAt(event.windowPosition, '不能绘制成面,请继续添加点') return } this.terminateShape() } terminateShape () { this.activeShapePoints.pop() this.drawGraph(this.activeShapePoints) global.viewer.entities.remove(this.activeShape) this.drawPointLayer.clear() this.drawPointLayer.remove() this.drawPointLayer = null this.activeShape = undefined this.activeShapePoints = [] this.unRegisterEvents() } }