import MouseTip from '../MouseTip' import MouseEvent from '../Event' import Draw from './draw' /** * @extends Draw*/ class DrawPolygon extends Draw { /** * @constructor * @param [options] {object} 面属性 * @param [options.color=rgba(185,14,14,0.58)] {object} 线属性 * */ constructor(sdk, options = {}) { super(sdk, options) this.polygonHasCreated = false } static create_polygon(that, viewer = that.viewer) { that.polygonHasCreated = true let id = that.randomString() viewer.entities.add( new Cesium.Entity({ id: id, polygon: { classificationType: Cesium.ClassificationType.BOTH, hierarchy: new Cesium.CallbackProperty((e) => { return new Cesium.PolygonHierarchy(that.positions) }), material: Cesium.Color.fromCssColorString(that.color), zIndex: 99999999 }, polyline: { positions: new Cesium.CallbackProperty((e) => { return that.positions.concat(that.positions[0]) }), width: 2, material: Cesium.Color.fromCssColorString('#c1c505').withAlpha(0.5), clampToGround: true, zIndex: 99999999 }, }) ) return id } /** * @desc 开始动态绘制面 * @method start * @param cb {function} 回调函数 * @memberOf DrawPolygon * @example draw.start((err,positions)=>{ * * }) * */ start(cb) { if (YJ.Measure.GetMeasureStatus()) { cb('上一次测量未结束') } else { this.polygonHasCreated = false super.start() YJ.Measure.SetMeasureStatus(true) let into this.tip = new MouseTip('左键确定,右键结束;CTRL+右键撤销', this.sdk) this.event = new MouseEvent(this.sdk) this.positions = [] this.points_ids = [] //存放左键点击时临时添加的point的id let cache_positions = [] let cache_84_position = [] this.event.mouse_left((movement, cartesian) => { if(into === '2D') { return } into = '3D' this.positions = cache_positions.concat({ ...cartesian }) this.tip.setPosition( cartesian, movement.position.x, movement.position.y ) if (!this.polygonHasCreated) { let polyline_id = DrawPolygon.create_polygon(this) this.points_ids.push(polyline_id) } cache_positions.push(cartesian) // console.log(cache_positions) cache_84_position.push(this.cartesian3Towgs84(cartesian, this.viewer)) // console.log(this.cartesian3Towgs84(cartesian)) this.points_ids.push(this.create_point(cartesian)) }) this.event.mouse_right((movement, cartesian) => { if(into === '2D') { return } // let positions = [] // console.log(cache_positions) // cache_positions.forEach((item) => { // let p = this.cartesian3Towgs84(item) // console.log(item) // positions.push(p) // }) cb(null, cache_84_position) this.end() }) this.event.mouse_move((movement, cartesian) => { if(into === '2D') { return } this.positions = cache_positions.concat({ ...cartesian }) this.tip.setPosition( cartesian, movement.endPosition.x, movement.endPosition.y ) }) this.event.mouse_right_keyboard_ctrl((movement, cartesian) => { if(into === '2D') { return } if (this.points_ids.length > 1) { this.remove_entity(this.points_ids.pop()) //移除point cache_positions.pop() cache_84_position.pop() } }) this.event.gesture_pinck_start_keyboard_ctrl(() => { if(into === '2D') { return } if (this.points_ids.length > 1) { this.remove_entity(this.points_ids.pop()) //移除point cache_positions.pop() cache_84_position.pop() this.positions = cache_positions.concat(cartesian) } }) this.event.gesture_pinck_start((movement, cartesian) => { if(into === '2D') { return } let startTime = new Date() this.event.gesture_pinck_end(() => { let endTime = new Date() if (endTime - startTime >= 500) { cb(null, cache_84_position) this.end() } else { this.tip.setPosition( cartesian, (movement.position1.x + movement.position2.x) / 2, (movement.position1.y + movement.position2.y) / 2 ) if (!this.polygonHasCreated) { let polyline_id = DrawPolygon.create_polygon(this) this.points_ids.push(polyline_id) } cache_positions.push(cartesian) // console.log(cache_positions) cache_84_position.push(this.cartesian3Towgs84(cartesian, this.viewer)) // console.log(this.cartesian3Towgs84(cartesian)) this.points_ids.push(this.create_point(cartesian)) this.positions = cache_positions.concat(cartesian) } }) }) if (!this._is2D && this._sdk2D) { this.event2D = new MouseEvent(this._sdk2D) this.event2D.mouse_left((movement, cartesian) => { if(into === '3D') { return } into = '2D' this.positions = cache_positions.concat({ ...cartesian }) this.tip.setPosition( cartesian, movement.position.x + this.viewer.canvas.width, movement.position.y ) if (!this.polygonHasCreated) { let polyline_id = DrawPolygon.create_polygon(this, this._sdk2D.viewer) this.points_ids.push(polyline_id) } cache_positions.push(cartesian) // console.log(cache_positions) cache_84_position.push(this.cartesian3Towgs84(cartesian, this.viewer)) // console.log(this.cartesian3Towgs84(cartesian)) this.points_ids.push(this.create_point(cartesian, this._sdk2D.viewer)) }) this.event2D.mouse_right((movement, cartesian) => { if(into === '3D') { return } // let positions = [] // console.log(cache_positions) // cache_positions.forEach((item) => { // let p = this.cartesian3Towgs84(item) // console.log(item) // positions.push(p) // }) cb(null, cache_84_position) this.end() }) this.event2D.mouse_move((movement, cartesian) => { if(into === '3D') { return } this.positions = cache_positions.concat({ ...cartesian }) this.tip.setPosition( cartesian, movement.endPosition.x + this.viewer.canvas.width, movement.endPosition.y ) }) this.event2D.mouse_right_keyboard_ctrl((movement, cartesian) => { if(into === '3D') { return } if (this.points_ids.length > 1) { this.remove_entity(this.points_ids.pop()) //移除point cache_positions.pop() cache_84_position.pop() } }) this.event2D.gesture_pinck_start_keyboard_ctrl(() => { if(into === '3D') { return } if (this.points_ids.length > 1) { this.remove_entity(this.points_ids.pop()) //移除point cache_positions.pop() cache_84_position.pop() this.positions = cache_positions.concat(cartesian) } }) this.event2D.gesture_pinck_start((movement, cartesian) => { if(into === '3D') { return } let startTime = new Date() this.event2D.gesture_pinck_end(() => { let endTime = new Date() if (endTime - startTime >= 500) { cb(null, cache_84_position) this.end() } else { this.tip.setPosition( cartesian, ((movement.position1.x + movement.position2.x) / 2) + this.viewer.canvas.width, (movement.position1.y + movement.position2.y) / 2 ) if (!this.polygonHasCreated) { let polyline_id = DrawPolygon.create_polygon(this, this._sdk2D.viewer) this.points_ids.push(polyline_id) } cache_positions.push(cartesian) // console.log(cache_positions) cache_84_position.push(this.cartesian3Towgs84(cartesian, this.viewer)) // console.log(this.cartesian3Towgs84(cartesian)) this.points_ids.push(this.create_point(cartesian, this._sdk2D.viewer)) this.positions = cache_positions.concat(cartesian) } }) }) } } } } export default DrawPolygon