import MouseTip from '../MouseTip' import MouseEvent from '../Event' import Draw from './draw' const transformCartesianToWGS84 = cartesian => { let ellipsoid = Cesium.Ellipsoid.WGS84 let cartographic = ellipsoid.cartesianToCartographic(cartesian) const x = Cesium.Math.toDegrees(cartographic.longitude) const y = Cesium.Math.toDegrees(cartographic.latitude) const z = cartographic.height return { x, y, z } } /** * @extends Draw*/ class DrawStraightArrow extends Draw { /** * @constructor * @param sdk * @param [options] {object} 面属性 * @param [options.color=rgba(185,14,14,0.58)] {object} 线属性 * */ constructor(sdk, options = {}) { super(sdk, options) this.points = null this.polygonHasCreated = false } static polygon(that, viewer = that.viewer) { let id = that.randomString() return viewer.entities.add( new Cesium.Entity({ name: 'ArrowPolygon', id, polygon: { hierarchy: new Cesium.CallbackProperty(e => { let arr = that.computeStraightArrow(that.positions) for (let i = 0; i < arr.length; i++) { if (isNaN(arr[i].x)) { arr = [] break } } return new Cesium.PolygonHierarchy(arr) }, false), material: Cesium.Color.fromCssColorString(that.color), outline: true, outlineColor: Cesium.Color.GREEN, zIndex: 99999999 } }) ) } /** * @desc 开始动态绘制面 * @method start * @param cb {function} 回调函数 * @memberOf DrawPolygon * @example draw.start((err,positions)=>{ * * }) * */ start(cb) { let that = this // eslint-disable-next-line no-undef if (YJ.Measure.GetMeasureStatus()) { cb('上一次测量未结束') } else { super.start() // eslint-disable-next-line no-undef let into YJ.Measure.SetMeasureStatus(true) this.tip = new MouseTip('左键确定,右键取消;', that.sdk) this.event = new MouseEvent(that.sdk) this.positions = [] this.points_ids = [] //存放左键点击时临时添加的point的id let cache_positions = [] let cache_84_position = [] this.anchorpoints = [] this.event.mouse_left((movement, cartesian) => { if(into === '2D') { return } into = '3D' if (!cartesian || this.anchorpoints[0]===cartesian) return this.anchorpoints.push(cartesian) let p = this.cartesian3Towgs84(cartesian, this.viewer) p.lng = Number(p.lng.toFixed(8)) p.lat = Number(p.lat.toFixed(8)) if(cache_positions[0] && (p.lng === cache_positions[0].lng && p.lat === cache_positions[0].lat)) return; cache_positions.push(p) this.positions.push(p) // console.log(this.cartesian3Towgs84(cartesian)) this.points_ids.push(this.create_point(cartesian)) if (this.points_ids.length === 2) { let array = [cache_positions[0], cache_positions[1]] cb(null, array) this.end() } }) this.event.mouse_move((movement, cartesian) => { if(into === '2D') { return } this.tip.setPosition( cartesian, movement.endPosition.x, movement.endPosition.y ) if (!cartesian || this.points_ids.length === 0) { return } let p = this.cartesian3Towgs84(cartesian, this.viewer) this.positions = [this.positions[0], p]; if (this.points_ids.length === 1 && !Cesium.defined(this.arrowPolygon)) { this.arrowPolygon = DrawStraightArrow.polygon(this) } }) this.event.mouse_right((movement, cartesian) => { if(into === '2D') { return } cb(null) this.end() }) 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) { this.end() cb(false) } else { if (this.anchorpoints.length === 2) { this.anchorpoints.push(cartesian) cb(null, this.positions) this.end() } else { if (!cartesian || Cesium.defined(this.arrowPolygon)) return this.tip.setPosition( cartesian, (movement.position1.x + movement.position2.x) / 2, (movement.position1.y + movement.position2.y) / 2 ) this.anchorpoints.push(cartesian) this.arrowPolygon = DrawStraightArrow.polygon(this) cache_positions.push(this.cartesian3Towgs84(cartesian)) // console.log(this.cartesian3Towgs84(cartesian)) this.points_ids.push(this.create_point(cartesian)) } } }) }) if (!this._is2D && this._sdk2D) { this.event2D = new MouseEvent(this._sdk2D) this.event2D.mouse_left((movement, cartesian) => { if(into === '3D') { return } into = '2D' if (!cartesian || this.anchorpoints[0]===cartesian) return this.anchorpoints.push(cartesian) let p = this.cartesian3Towgs84(cartesian, this.viewer) p.lng = Number(p.lng.toFixed(8)) p.lat = Number(p.lat.toFixed(8)) if(cache_positions[0] && (p.lng === cache_positions[0].lng && p.lat === cache_positions[0].lat)) return; cache_positions.push(p) this.positions.push(p) // console.log(this.cartesian3Towgs84(cartesian)) this.points_ids.push(this.create_point(cartesian, this._sdk2D.viewer)) if (this.points_ids.length === 2) { let array = [cache_positions[0], cache_positions[1]] cb(null, array) this.end() } }) this.event2D.mouse_move((movement, cartesian) => { if(into === '3D') { return } this.tip.setPosition( cartesian, movement.endPosition.x + this.viewer.canvas.width, movement.endPosition.y ) if (!cartesian || this.points_ids.length === 0) { return } let p = this.cartesian3Towgs84(cartesian, this.viewer) this.positions = [this.positions[0], p]; if (this.points_ids.length === 1 && !Cesium.defined(this.arrowPolygon)) { this.arrowPolygon = DrawStraightArrow.polygon(this, this._sdk2D.viewer) } }) this.event2D.mouse_right((movement, cartesian) => { if(into === '3D') { return } cb(null) this.end() }) 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) { this.end() cb(false) } else { if (this.anchorpoints.length === 2) { this.anchorpoints.push(cartesian) cb(null, this.positions) this.end() } else { if (!cartesian || Cesium.defined(this.arrowPolygon)) return this.tip.setPosition( cartesian, (movement.position1.x + movement.position2.x) / 2 + this.viewer.canvas.width, (movement.position1.y + movement.position2.y) / 2 ) this.anchorpoints.push(cartesian) this.arrowPolygon = DrawStraightArrow.polygon(this, this._sdk2D.viewer) cache_positions.push(this.cartesian3Towgs84(cartesian)) // console.log(this.cartesian3Towgs84(cartesian)) this.points_ids.push(this.create_point(cartesian, this._sdk2D.viewer)) } } }) }) } } } end() { super.end(); this.viewer.entities.remove(this.arrowPolygon) if (!this._is2D && this._sdk2D) { this._sdk2D.viewer.entities.remove(this.arrowPolygon) } } } export default DrawStraightArrow