//一、点 // DrawPoint /* 绘制点 */ class DrawPoint { constructor(arg) { this.viewer = arg.viewer; this.Cesium = arg.Cesium; // this.callback=arg.callback; this._point = null; //最后一个点 this._pointData = null;//点数据用于构造点 this._entities = []; //脏数据 this.DrawStartEvent = new Cesium.Event(); //开始绘制事件 this.DrawEndEvent = new Cesium.Event(); //结束绘制事件 /* 通用参数集合 */ this._param = { id: "DrawStraightArrow", polygonColor: 'rgba(0,255,0,0.5)', //面填充颜色 outlineColor: 'rgba(255, 255, 255, 1)', //边框颜色 outlineWidth: 1, //边框宽度 } /* 创建面材质 */ this.polygonMaterial = Cesium.Color.fromCssColorString(this._param.polygonColor); /* 创建线材质 */ // this.outlineMaterial = new Cesium.PolylineDashMaterialProperty({//曲线 // dashLength: 16, // color: Cesium.Color.fromCssColorString(this._param.outlineColor) // }); this.outlineMaterial = Cesium.Color.fromCssColorString(this._param.outlineColor); } //返回最后活动点 get point() { return this._point; } //加载点 addload(data) { return this.createPoint(data); } //返回点数据用于加载点 getData() { return this._pointData; } // 修改编辑调用计算 computePosition(data) { let $this = this var lnglatArr = []; for (var i = 0; i < data.length; i++) { var lnglat = $this.cartesianToLatlng(data[i]); lnglatArr.push(lnglat) } $this._pointData = lnglatArr; let point = lnglatArr[0] return $this.Cesium.Cartesian3.fromDegrees(point[0],point[1]) } //开始绘制 startCreate(drawType) { this.drawType = drawType; var $this = this; this.handler = new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas); this.handler.setInputAction(function (evt) { //单机开始绘制 var cartesian = $this.getCatesian3FromPX(evt.position); if (!cartesian) return; let latlon = $this.cartesianToLatlng(cartesian) var point = $this.createPoint(latlon); $this._pointData = cartesian; $this._point = point; // if(typeof $this.callback=="function"){ // $this.callback(point); // } $this.DrawEndEvent.raiseEvent($this._point, latlon, $this.drawType); $this.destroy() }, $this.Cesium.ScreenSpaceEventType.LEFT_CLICK); } //创建点 createPoint(cartesian) { var $this = this; var point = this.viewer.entities.add({ Type:'DrawPoint', Position:[cartesian], position: $this.Cesium.Cartesian3.fromDegrees(cartesian[0],cartesian[1]), id:cartesian.id || $this.objId, billboard: { image: "./static/poi2.png", // scaleByDistance: new Cesium.NearFarScalar(300, 1, 1200, 0.4), //设置随图缩放距离和比例 // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 10000), //设置可见距离 10000米可见 verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // clampToGround: true }, }); $this._entities.push(point); //加载脏数据 return point; } cartesianToLatlng(cartesian) { let ellipsoid = this.viewer.scene.globe.ellipsoid let cartographic = ellipsoid.cartesianToCartographic(cartesian); let lat = Cesium.Math.toDegrees(cartographic.latitude); let lng = Cesium.Math.toDegrees(cartographic.longitude); return [lng, lat]; } //销毁鼠标事件 destroy() { if (this.handler) { this.handler.destroy(); this.handler = null; } } //清空实体对象 clear() { for (var i = 0; i < this._entities.length; i++) { this.viewer.entities.remove(this._entities[i]); } this._entities = []; this._point = null; } getCatesian3FromPX(px) { var cartesian; var ray = this.viewer.camera.getPickRay(px); if (!ray) return null; cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene); return cartesian; } } export default DrawPoint;