357 lines
11 KiB
JavaScript
357 lines
11 KiB
JavaScript
import MouseTip from '../MouseTip'
|
||
import MouseEvent from '../Event'
|
||
import Draw from './draw'
|
||
|
||
/**
|
||
* @extends Draw
|
||
*/
|
||
|
||
class DrawPolyline extends Draw {
|
||
/**
|
||
* @constructor
|
||
* @param [options] {object} 线属性
|
||
* @param [options.color=rgba(185,14,14,0.58)] {object} 线属性
|
||
*
|
||
* */
|
||
constructor(sdk, options = {}) {
|
||
super(sdk, options)
|
||
this.options.curve = options.curve || false
|
||
let number = Number(options.number)
|
||
if (!isNaN(number)) {
|
||
if (number < 2) {
|
||
this.options.number = 2
|
||
}
|
||
else {
|
||
this.options.number = number
|
||
}
|
||
}
|
||
else {
|
||
this.options.number = Infinity
|
||
}
|
||
}
|
||
|
||
static create_polyline(that, viewer = that.viewer) {
|
||
that.entityHasCreated = true
|
||
let id = that.randomString()
|
||
viewer.entities.add(
|
||
new Cesium.Entity({
|
||
id: id,
|
||
polyline: {
|
||
positions: new Cesium.CallbackProperty(() => {
|
||
if (that.options.curve) {
|
||
let positions = that.smoothHandle(that.positions)
|
||
return positions
|
||
}
|
||
else {
|
||
return that.positions
|
||
}
|
||
}, false),
|
||
width: 5,
|
||
material: Cesium.Color.fromCssColorString(that.color),
|
||
clampToGround: true,
|
||
zIndex: 99999999
|
||
}
|
||
})
|
||
)
|
||
return id
|
||
}
|
||
|
||
// 平滑处理
|
||
smoothHandle(positions) {
|
||
if (positions.length > 1) {
|
||
let newPositions = []
|
||
let time = []
|
||
for (let i = 0; i < positions.length; i++) {
|
||
time.push(i / (positions.length - 1))
|
||
}
|
||
let spline = new Cesium.CatmullRomSpline({
|
||
times: time,
|
||
points: positions
|
||
});
|
||
let length = positions.length * 20
|
||
for (let i = 0; i <= length; i++) {
|
||
let cartesian3 = spline.evaluate(i / length);
|
||
newPositions.push(cartesian3);
|
||
}
|
||
return newPositions
|
||
}
|
||
else {
|
||
return positions
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @desc 开始动态获绘制线
|
||
* @method start
|
||
* @param cb {function} 回调函数
|
||
* @memberOf DrawPolyline
|
||
* @example draw.start((err,positions)=>{
|
||
*
|
||
* })
|
||
* */
|
||
start(cb) {
|
||
if (YJ.Measure.GetMeasureStatus()) {
|
||
cb('上一次测量未结束')
|
||
} else {
|
||
super.start()
|
||
let into
|
||
YJ.Measure.SetMeasureStatus(true)
|
||
this.tip = new MouseTip(this.tipText || '左键确定,右键结束;CTRL+右键撤销', this.sdk)
|
||
this.event = new MouseEvent(this.sdk)
|
||
this.positions = []
|
||
this.points_ids = [] //存放左键点击时临时添加的point的id
|
||
|
||
let cache_positions = []
|
||
let car = undefined
|
||
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.entityHasCreated) {
|
||
let polyline_id = DrawPolyline.create_polyline(this, this.viewer)
|
||
this.points_ids.push(polyline_id)
|
||
}
|
||
cache_positions.push(cartesian)
|
||
this.points_ids.push(this.create_point(cartesian, this.viewer))
|
||
if (cache_positions.length >= this.options.number) {
|
||
let positions = []
|
||
cache_positions.forEach((item) => {
|
||
positions.push(this.cartesian3Towgs84(item, this.viewer))
|
||
})
|
||
let smoothPos
|
||
if (this.options.curve) {
|
||
let pos = this.smoothHandle(cache_positions)
|
||
smoothPos = []
|
||
for (let i = 0; i < pos.length; i++) {
|
||
smoothPos[i] = this.cartesian3Towgs84(pos[i], this.viewer)
|
||
}
|
||
}
|
||
cb(null, positions, smoothPos)
|
||
this.end()
|
||
}
|
||
else {
|
||
// cb(cache_positions.length)
|
||
cb(null, cache_positions)
|
||
}
|
||
})
|
||
this.event.mouse_right((movement, cartesian) => {
|
||
if (into === '2D') {
|
||
return
|
||
}
|
||
let positions = []
|
||
cache_positions.forEach((item) => {
|
||
positions.push(this.cartesian3Towgs84(item, this.viewer))
|
||
})
|
||
let smoothPos
|
||
if (this.options.curve) {
|
||
let pos = this.smoothHandle(cache_positions)
|
||
smoothPos = []
|
||
for (let i = 0; i < pos.length; i++) {
|
||
smoothPos[i] = this.cartesian3Towgs84(pos[i], this.viewer)
|
||
}
|
||
}
|
||
cb(null, positions, smoothPos)
|
||
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()
|
||
cb(cache_positions.length)
|
||
}
|
||
})
|
||
|
||
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()
|
||
this.positions = cache_positions.concat(cartesian)
|
||
cb(cache_positions.length)
|
||
}
|
||
})
|
||
|
||
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) {
|
||
let positions = []
|
||
cache_positions.forEach((item) => {
|
||
positions.push(this.cartesian3Towgs84(item, this.viewer))
|
||
})
|
||
let smoothPos
|
||
if (this.options.curve) {
|
||
let pos = this.smoothHandle(cache_positions)
|
||
smoothPos = []
|
||
for (let i = 0; i < pos.length; i++) {
|
||
smoothPos[i] = this.cartesian3Towgs84(pos[i], this.viewer)
|
||
}
|
||
}
|
||
cb(null, positions, smoothPos)
|
||
this.end()
|
||
}
|
||
else {
|
||
this.tip.setPosition(
|
||
cartesian,
|
||
(movement.position1.x + movement.position2.x) / 2,
|
||
(movement.position1.y + movement.position2.y) / 2
|
||
)
|
||
if (!this.entityHasCreated) {
|
||
let polyline_id = DrawPolyline.create_polyline(this, this.viewer)
|
||
this.points_ids.push(polyline_id)
|
||
}
|
||
cache_positions.push(cartesian)
|
||
this.points_ids.push(this.create_point(cartesian, this.viewer))
|
||
this.positions = cache_positions.concat(cartesian)
|
||
cb(cache_positions.length)
|
||
}
|
||
})
|
||
})
|
||
|
||
|
||
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.entityHasCreated) {
|
||
let polyline_id = DrawPolyline.create_polyline(this, this._sdk2D.viewer)
|
||
this.points_ids.push(polyline_id)
|
||
}
|
||
cache_positions.push(cartesian)
|
||
this.points_ids.push(this.create_point(cartesian, this._sdk2D.viewer))
|
||
})
|
||
this.event2D.mouse_right((movement, cartesian) => {
|
||
if (into === '3D') {
|
||
return
|
||
}
|
||
let positions = []
|
||
cache_positions.forEach((item) => {
|
||
positions.push(this.cartesian3Towgs84(item, this.viewer))
|
||
})
|
||
let smoothPos
|
||
if (this.options.curve) {
|
||
let pos = this.smoothHandle(cache_positions)
|
||
smoothPos = []
|
||
for (let i = 0; i < pos.length; i++) {
|
||
smoothPos[i] = this.cartesian3Towgs84(pos[i], this.viewer)
|
||
}
|
||
}
|
||
cb(null, positions, smoothPos)
|
||
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()
|
||
cb(cache_positions.length)
|
||
}
|
||
})
|
||
|
||
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()
|
||
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) {
|
||
let positions = []
|
||
cache_positions.forEach((item) => {
|
||
positions.push(this.cartesian3Towgs84(item, this.viewer))
|
||
})
|
||
let smoothPos
|
||
if (this.options.curve) {
|
||
let pos = this.smoothHandle(cache_positions)
|
||
smoothPos = []
|
||
for (let i = 0; i < pos.length; i++) {
|
||
smoothPos[i] = this.cartesian3Towgs84(pos[i], this.viewer)
|
||
}
|
||
}
|
||
cb(null, positions, smoothPos)
|
||
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.entityHasCreated) {
|
||
let polyline_id = DrawPolyline.create_polyline(this, this._sdk2D.viewer)
|
||
this.points_ids.push(polyline_id)
|
||
}
|
||
cache_positions.push(cartesian)
|
||
this.points_ids.push(this.create_point(cartesian, this._sdk2D.viewer))
|
||
this.positions = cache_positions.concat(cartesian)
|
||
cb(cache_positions.length)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
export default DrawPolyline
|