| <template> |
| <div> |
| <span @click="drawClick">绘制多边形</span> |
| <span @click="drawCircleClick">绘制圆形</span> |
| </div> |
| </template> |
| |
| <script> |
| |
| let polygonEntity = null; |
| let viewer; |
| let entities = []; |
| let positions = []; |
| let centerPositionEntity = null; |
| export default { |
| name: "AirspaceMgEdit", |
| data() { |
| return { |
| |
| airSpaceTypeList: [], |
| leftClickHandler: null, |
| }; |
| }, |
| |
| mounted() { |
| viewer = window.viewer; |
| }, |
| methods: { |
| |
| handleRightDoubleClick() { |
| if (positions.length < 3) { |
| return this.$message.warning("请最少选择三个点"); |
| } |
| |
| this.removeInputAction(); |
| |
| if (positions.length > 2) { |
| polygonEntity = viewer.entities.add({ |
| polygon: { |
| hierarchy: positions, |
| material: Cesium.Color.fromCssColorString("#ff0000"), |
| }, |
| }); |
| const centerPosition = |
| Cesium.BoundingSphere.fromPoints(positions).center; |
| centerPositionEntity = viewer.entities.add({ |
| position: centerPosition, |
| label: { |
| text: "区域名称", |
| scaleByDistance: new Cesium.NearFarScalar(800, 2.0, 900, 0.6), |
| scale: 0.8, |
| verticalOrigin: Cesium.VerticalOrigin.CENTER, |
| pixelOffset: new Cesium.Cartesian2(), |
| }, |
| }); |
| this.createAfterPolyline(); |
| entities.push(polygonEntity); |
| entities.push(centerPositionEntity); |
| } |
| |
| this.createAfterPolyline(); |
| }, |
| drawCircleClick() { |
| this.removeInputAction(); |
| this.clearMarker(); |
| this.$message.success("请点击选择第一个点"); |
| const _this = this; |
| this.leftClickHandler = viewer.screenSpaceEventHandler.setInputAction( |
| function (event) { |
| |
| const position = viewer.scene.pickPosition(event.position); |
| if (position) { |
| |
| _this.creatPoint(position); |
| } |
| _this.$message.success("请选择第二个点"); |
| if (positions.length == 2) { |
| |
| _this.createPolyline(position); |
| |
| _this.createCircle(); |
| |
| viewer.screenSpaceEventHandler.removeInputAction( |
| Cesium.ScreenSpaceEventType.LEFT_CLICK, |
| _this.leftClickHandler |
| ); |
| } |
| }, |
| Cesium.ScreenSpaceEventType.LEFT_CLICK |
| ); |
| }, |
| |
| createCircle() { |
| const radius = Cesium.Cartesian3.distance(positions[0], positions[1]); |
| const center = positions[0]; |
| try { |
| polygonEntity = viewer.entities.add({ |
| position: center, |
| label: { |
| text: "圆形区域", |
| scaleByDistance: new Cesium.NearFarScalar(800, 2.0, 900, 0.6), |
| scale: 0.8, |
| verticalOrigin: Cesium.VerticalOrigin.CENTER, |
| pixelOffset: new Cesium.Cartesian2(20, 20), |
| }, |
| ellipse: { |
| semiMinorAxis: radius, |
| semiMajorAxis: radius, |
| material: Cesium.Color.fromCssColorString("#1b6ef3"), |
| outline: true, |
| outlineColor: Cesium.Color.WHITE, |
| }, |
| }); |
| entities.push(polygonEntity); |
| } catch (error) { |
| console.log(error); |
| } |
| }, |
| |
| drawClick() { |
| const _this = this; |
| |
| this.leftClickHandler = viewer.screenSpaceEventHandler.setInputAction( |
| function (e) { |
| |
| const position = viewer.scene.pickPosition(e.position); |
| if (position) { |
| _this.creatPoint(position); |
| _this.createPolyline(position); |
| } |
| }, |
| Cesium.ScreenSpaceEventType.LEFT_CLICK |
| ); |
| var rightClickTimer; |
| var isRightDoubleClick = false; |
| this.rightClickHandler = viewer.screenSpaceEventHandler.setInputAction( |
| function (click) { |
| |
| if (click.button === Cesium.ScreenSpaceEventType.RIGHT_BUTTON) { |
| if (rightClickTimer) { |
| |
| |
| clearTimeout(rightClickTimer); |
| isRightDoubleClick = true; |
| _this.handleRightDoubleClick(); |
| } else { |
| |
| rightClickTimer = setTimeout(function () { |
| if (!isRightDoubleClick) { |
| |
| } |
| |
| rightClickTimer = undefined; |
| isRightDoubleClick = false; |
| }, 300); |
| } |
| } |
| }, |
| Cesium.ScreenSpaceEventType.RIGHT_CLICK |
| ); |
| }, |
| |
| |
| removeInputAction() { |
| viewer.screenSpaceEventHandler.removeInputAction( |
| Cesium.ScreenSpaceEventType.LEFT_CLICK, |
| this.leftClickHandler |
| ); |
| viewer.screenSpaceEventHandler.removeInputAction( |
| Cesium.ScreenSpaceEventType.RIGHT_CLICK, |
| this.rightClickHandler |
| ); |
| }, |
| createAfterPolyline() { |
| if (positions.length > 1) { |
| const lineEntity = viewer.entities.add({ |
| polyline: { |
| positions: [positions[0], positions[positions.length - 1]], |
| width: 2, |
| material: Cesium.Color.RED, |
| }, |
| }); |
| entities.push(lineEntity); |
| |
| const distance = Cesium.Cartesian3.distance( |
| positions[0], |
| positions[positions.length - 1] |
| ); |
| |
| const center = Cesium.Cartesian3.add( |
| positions[positions.length - 1], |
| positions[0], |
| new Cesium.Cartesian3() |
| ); |
| |
| const newcenter = Cesium.Cartesian3.divideByScalar(center, 2, center); |
| |
| var labelEntity = viewer.entities.add({ |
| position: newcenter, |
| label: { |
| text: distance.toFixed(2) + "m", |
| font: "18px sans-serif", |
| fillColor: Cesium.Color.WHITE, |
| outlineColor: Cesium.Color.BLACK, |
| outlineWidth: 2, |
| style: Cesium.LabelStyle.FILL_AND_OUTLINE, |
| pixelOffset: new Cesium.Cartesian2(0, -20), |
| }, |
| }); |
| entities.push(labelEntity); |
| } |
| }, |
| |
| creatPoint(position) { |
| |
| const pointEntity = viewer.entities.add({ |
| position: position, |
| point: { |
| pixelSize: 20, |
| color: Cesium.Color.RED, |
| outlineColor: Cesium.Color.WHITE, |
| outlineWidth: 2, |
| }, |
| label: { |
| text: (positions.length + 1).toString(), |
| scale: 0.5, |
| font: "bold 24px Microsoft YaHei", |
| fillColor: Cesium.Color.WHITE, |
| horizontalOrigin: Cesium.VerticalOrigin.CENTER, |
| verticalOrigin: Cesium.VerticalOrigin.CENTER, |
| disableDepthTestDistance: Number.POSITIVE_INFINITY, |
| showBackground: false, |
| outlineWidth: 0, |
| }, |
| }); |
| |
| entities.push(pointEntity); |
| positions.push(position); |
| }, |
| |
| createPolyline(position) { |
| |
| if (positions.length > 1) { |
| var lineEntity = viewer.entities.add({ |
| polyline: { |
| positions: [positions[positions.length - 2], position], |
| width: 2, |
| material: Cesium.Color.RED, |
| }, |
| }); |
| entities.push(lineEntity); |
| |
| const distance = Cesium.Cartesian3.distance( |
| position, |
| positions[positions.length - 2] |
| ); |
| |
| const center = Cesium.Cartesian3.add( |
| positions[positions.length - 2], |
| position, |
| new Cesium.Cartesian3() |
| ); |
| const newcenter = Cesium.Cartesian3.divideByScalar(center, 2, center); |
| |
| var labelEntity = viewer.entities.add({ |
| position: newcenter, |
| label: { |
| text: distance.toFixed(2) + "m", |
| font: "18px sans-serif", |
| fillColor: Cesium.Color.WHITE, |
| outlineColor: Cesium.Color.BLACK, |
| outlineWidth: 2, |
| style: Cesium.LabelStyle.FILL_AND_OUTLINE, |
| pixelOffset: new Cesium.Cartesian2(0, -20), |
| }, |
| }); |
| entities.push(labelEntity); |
| } |
| }, |
| |
| clearMarker() { |
| if (entities) { |
| for (let i = 0; i < entities.length; i++) { |
| const element = entities[i]; |
| viewer.entities.remove(element); |
| } |
| entities = []; |
| positions = []; |
| |
| polygonEntity = null; |
| } |
| }, |
| }, |
| }; |
| </script> |