代码迁移
This commit is contained in:
197
src/Measure/MeasureTyArea/index.js
Normal file
197
src/Measure/MeasureTyArea/index.js
Normal file
@ -0,0 +1,197 @@
|
||||
/**
|
||||
* @name: index
|
||||
* @author: Administrator
|
||||
* @date: 2022-07-11 17:28
|
||||
* @description:index
|
||||
* @update: 2022-07-11 17:28
|
||||
*/
|
||||
import Measure from "../index"
|
||||
|
||||
/**@extends Measure*/
|
||||
class MeasureTyArea extends Measure {
|
||||
/**
|
||||
* @constructor
|
||||
* @param sdk
|
||||
* @description 投影面积测量
|
||||
* */
|
||||
constructor(sdk, options = {}) {
|
||||
super(sdk, options);
|
||||
this.options.lineColor = '#ffdf53'
|
||||
this.polygon_id = ""
|
||||
}
|
||||
|
||||
static create_polygon(that) {
|
||||
let id = that.randomString()
|
||||
|
||||
let scaleByDistance = new Cesium.NearFarScalar(2000, 1, 100000, 0)
|
||||
|
||||
let e = that.viewer.entities.add(
|
||||
new Cesium.Entity({
|
||||
id: id,
|
||||
label: {
|
||||
text: new Cesium.CallbackProperty(() => {
|
||||
return that.text
|
||||
}, false),
|
||||
//标注文字描述
|
||||
font: '20px Microsoft YaHei',
|
||||
fillColor: Cesium.Color.fromCssColorString('#ffffff'),
|
||||
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
|
||||
//标注的遮挡距离设置为100则视角与标注的距离大于100米时会有遮挡
|
||||
// distanceDisplayCondition: this.distanceDisplayCondition,
|
||||
|
||||
disableDepthTestDistance: Number.POSITIVE_INFINITY,
|
||||
|
||||
// scaleByDistance,
|
||||
scale: 1,
|
||||
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
|
||||
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
|
||||
},
|
||||
position: new Cesium.CallbackProperty(() => {
|
||||
return that.center
|
||||
}, false),
|
||||
polygon: {
|
||||
classificationType: Cesium.ClassificationType.BOTH,
|
||||
hierarchy: new Cesium.CallbackProperty((e) => {
|
||||
return new Cesium.PolygonHierarchy(that.positions)
|
||||
}, false),
|
||||
material: new Cesium.Color.fromCssColorString(that.options.color || that.defaultColor),
|
||||
zIndex: 99999999
|
||||
},
|
||||
polyline: {
|
||||
positions: new Cesium.CallbackProperty(() => {
|
||||
if (that.positions.length)
|
||||
return that.positions.concat(that.positions[0])
|
||||
return that.positions
|
||||
}, false),
|
||||
width: 2,
|
||||
material: new Cesium.PolylineDashMaterialProperty({
|
||||
color: new Cesium.Color.fromCssColorString(that.options.lineColor || that.defaultColor),
|
||||
dashLength: 20, //短划线长度
|
||||
}),
|
||||
clampToGround: true,
|
||||
zIndex: 99999999
|
||||
}
|
||||
})
|
||||
)
|
||||
return id
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始测量
|
||||
*/
|
||||
start() {
|
||||
if (!YJ.Measure.GetMeasureStatus()) {
|
||||
super.start()
|
||||
this.ids = []
|
||||
this.positions = []
|
||||
this.text = ""
|
||||
this.center = new Cesium.Cartesian3()
|
||||
this.cachePositions = []
|
||||
let height = 0
|
||||
let lastArea = 0
|
||||
let lastcneter
|
||||
|
||||
let leftEvent = (movement, car) => {
|
||||
if (this.ids.length === 0) {
|
||||
this.polygon_id = MeasureTyArea.create_polygon(this)
|
||||
}
|
||||
this.cachePositions.push({ ...car })
|
||||
this.ids.push(this.create_point({ ...car }, false))
|
||||
let po = this.cartesian3Towgs84({ ...car }, this.viewer)
|
||||
if (po.alt > height) {
|
||||
height = po.alt
|
||||
}
|
||||
|
||||
this.positions = this.cachePositions.concat({ ...car })
|
||||
this.tip.setPosition({ ...car }, movement.position.x, movement.position.y)
|
||||
if (this.positions.length > 2) {
|
||||
let arr = []
|
||||
this.positions.forEach(item => {
|
||||
let p = this.cartesian3Towgs84(item, this.viewer)
|
||||
arr.push({ lng: p.lng, lat: p.lat })
|
||||
})
|
||||
let center = this.computeCenter(arr)
|
||||
let area = this.computeArea(arr)
|
||||
lastArea = area
|
||||
this.center = new Cesium.Cartesian3.fromDegrees(center.lng, center.lat, height)
|
||||
lastcneter = this.center
|
||||
this.text = "投影面积:" + area + " ㎡"
|
||||
}
|
||||
}
|
||||
this.event.mouse_left(leftEvent)
|
||||
this.event.mouse_move((movement, car) => {
|
||||
this.tip.setPosition({ ...car }, movement.endPosition.x, movement.endPosition.y)
|
||||
this.positions = this.cachePositions.concat({ ...car })
|
||||
if (this.positions.length > 2) {
|
||||
let arr = []
|
||||
this.positions.forEach(item => {
|
||||
let p = this.cartesian3Towgs84(item, this.viewer)
|
||||
arr.push({ lng: p.lng, lat: p.lat })
|
||||
})
|
||||
let center = this.computeCenter(arr)
|
||||
let area = this.computeArea(arr)
|
||||
this.center = new Cesium.Cartesian3.fromDegrees(center.lng, center.lat, height)
|
||||
this.text = "投影面积:" + area + " ㎡"
|
||||
}
|
||||
})
|
||||
this.event.mouse_right((movement, car) => {
|
||||
this.positions = this.cachePositions
|
||||
this.center = lastcneter
|
||||
if (this.positions.length < 3) {
|
||||
this.text = ""
|
||||
let error = '面积计算至少需要三个坐标!'
|
||||
console.warn(error)
|
||||
window.ELEMENT && window.ELEMENT.Message({
|
||||
message: error,
|
||||
type: 'warning',
|
||||
duration: 1500
|
||||
});
|
||||
this.destroy()
|
||||
}
|
||||
else {
|
||||
this.text = "投影面积:" + lastArea + " ㎡"
|
||||
}
|
||||
this.end()
|
||||
})
|
||||
this.event.gesture_pinck_start((movement, cartesian) => {
|
||||
let startTime = new Date()
|
||||
let pos = {
|
||||
position: {
|
||||
x: (movement.position1.x + movement.position2.x) / 2,
|
||||
y: (movement.position1.y + movement.position2.y) / 2
|
||||
}
|
||||
}
|
||||
this.event.gesture_pinck_end(() => {
|
||||
let endTime = new Date()
|
||||
if (endTime - startTime >= 500) {
|
||||
// 长按取消
|
||||
this.positions = this.cachePositions
|
||||
this.end()
|
||||
}
|
||||
else {
|
||||
leftEvent(pos, cartesian)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除测量
|
||||
*/
|
||||
destroy() {
|
||||
[this.polygon_id, ...this.ids].forEach(id => {
|
||||
this.remove_entity(id)
|
||||
})
|
||||
super.destroy()
|
||||
}
|
||||
|
||||
/**
|
||||
* 结束测量
|
||||
*/
|
||||
end() {
|
||||
super.end()
|
||||
}
|
||||
}
|
||||
|
||||
export default MeasureTyArea
|
Reference in New Issue
Block a user