代码迁移
This commit is contained in:
206
src/Measure/MeasureAzimuth/index.js
Normal file
206
src/Measure/MeasureAzimuth/index.js
Normal file
@ -0,0 +1,206 @@
|
||||
import Measure from "../index";
|
||||
|
||||
class MeasureAzimuth extends Measure {
|
||||
/**
|
||||
* @constructor
|
||||
* @param sdk
|
||||
* @description 方位角测量
|
||||
* */
|
||||
constructor(sdk) {
|
||||
super(sdk, { text: "左键开始,右键取消" });
|
||||
this.cachePositions = []
|
||||
this.positions = []
|
||||
this.arcPositions = []
|
||||
this.line_id = ""
|
||||
this.label_id = ""
|
||||
this.arc_id = ""
|
||||
this.bearing = 0
|
||||
}
|
||||
|
||||
createPolyline() {
|
||||
let that = this
|
||||
let id = that.randomString()
|
||||
that.viewer.entities.add(new Cesium.Entity({
|
||||
id,
|
||||
polyline: {
|
||||
positions: new Cesium.CallbackProperty(() => {
|
||||
return that.positions
|
||||
}, false),
|
||||
clampToGround: true,
|
||||
width: 5,
|
||||
material: new Cesium.Color.fromCssColorString(that.options.color || that.defaultColor),
|
||||
zIndex: 99999999
|
||||
}
|
||||
}))
|
||||
return id
|
||||
}
|
||||
|
||||
end() {
|
||||
super.end();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
super.destroy();
|
||||
let arr = [this.line_id, this.label_id, this.arc_id]
|
||||
arr.forEach(id => {
|
||||
if (id)
|
||||
this.remove_entity(id)
|
||||
})
|
||||
}
|
||||
|
||||
cancel() {
|
||||
this.end()
|
||||
this.destroy()
|
||||
}
|
||||
|
||||
caculateAngle(line1 = [], line2 = []) {
|
||||
let c = this.cartesian3Towgs84(line2[1], this.viewer)
|
||||
let p2 = this.cartesian3Towgs84(line2[0], this.viewer)
|
||||
let center = turf.point([c.lng, c.lat])
|
||||
let point2 = turf.point([p2.lng, p2.lat])
|
||||
let bearing = this.rhumbBearing(p2, c)
|
||||
this.bearing = (180 + bearing).toFixed(2)
|
||||
|
||||
let distance = turf.rhumbDistance(center, point2, { units: 'kilometers' });
|
||||
let arc = turf.lineArc(center, (distance/3), 0, this.bearing);
|
||||
let arcPos = []
|
||||
for (let i = 0; i < arc.geometry.coordinates.length; i++) {
|
||||
arcPos.push(Cesium.Cartesian3.fromDegrees(arc.geometry.coordinates[i][0], arc.geometry.coordinates[i][1]))
|
||||
}
|
||||
this.arcPositions = arcPos
|
||||
}
|
||||
|
||||
|
||||
start() {
|
||||
if (!YJ.Measure.GetMeasureStatus()) {
|
||||
super.start();
|
||||
|
||||
let leftEvent = async (movement, car) => {
|
||||
if (this.ids.length === 0) {
|
||||
//需要创建一个线
|
||||
this.line_id = this.createPolyline()
|
||||
}
|
||||
this.tip.setPosition(car, movement.position.x, movement.position.y)
|
||||
if (this.cachePositions.length) {
|
||||
this.positions = this.cachePositions.concat(car)
|
||||
let p = this.cartesian3Towgs84(car, this.viewer)
|
||||
let pc = this.cartesian3Towgs84(this.positions[1], this.viewer)
|
||||
let from = turf.point([pc.lng, pc.lat]);
|
||||
let to = turf.point([p.lng, p.lat]);
|
||||
let options = { units: 'kilometers' };
|
||||
let distance = turf.rhumbDistance(from, to, options);
|
||||
|
||||
let bearing = 0;
|
||||
let destination = turf.destination(from, distance, bearing, options);
|
||||
this.positions[0] = Cesium.Cartesian3.fromDegrees(...destination.geometry.coordinates)
|
||||
}
|
||||
this.cachePositions.push(car)
|
||||
this.cachePositions.push(car)
|
||||
if (this.positions.length > 2) {
|
||||
//需要开始计算夹角
|
||||
this.caculateAngle(
|
||||
[this.positions[0], this.positions[1]],
|
||||
[this.positions[2], this.positions[1]])
|
||||
}
|
||||
if (this.ids.length >= 2) {
|
||||
//需要停止绘制
|
||||
this.end()
|
||||
return
|
||||
}
|
||||
|
||||
this.ids.push(this.create_point(car))
|
||||
this.ids.push(this.create_point(car))
|
||||
if (this.ids.length === 2) {
|
||||
this.label_id = Cesium.createGuid()
|
||||
this.arc_id = Cesium.createGuid()
|
||||
let p = this.cartesian3Towgs84(car, this.viewer)
|
||||
let res = await this.sampleHeightMostDetailed([p])
|
||||
|
||||
let arc = this.viewer.entities.add({
|
||||
id: this.arc_id,
|
||||
polyline: {
|
||||
positions: new Cesium.CallbackProperty(() => {
|
||||
return this.arcPositions
|
||||
}, false),
|
||||
clampToGround: true,
|
||||
width: 5,
|
||||
material: new Cesium.Color.fromCssColorString(this.options.color || this.defaultColor),
|
||||
zIndex: 99999999
|
||||
}
|
||||
})
|
||||
|
||||
let label = this.viewer.entities.add({
|
||||
id: this.label_id,
|
||||
position: Cesium.Cartesian3.fromDegrees(p.lng, p.lat, (res[0].height || 0) + 0.1),
|
||||
label: {
|
||||
text: new Cesium.CallbackProperty(() => {
|
||||
return "方位夹角:" + this.bearing + "°"
|
||||
}, false),
|
||||
font: '20px Microsoft YaHei',
|
||||
fillColor: Cesium.Color.fromCssColorString('#f1e605'),
|
||||
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
|
||||
//标注的遮挡距离设置为100则视角与标注的距离大于100米时会有遮挡
|
||||
// distanceDisplayCondition: this.distanceDisplayCondition,
|
||||
scale: 1,
|
||||
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
|
||||
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
|
||||
disableDepthTestDistance: Number.POSITIVE_INFINITY,
|
||||
}
|
||||
})
|
||||
//需要创建夹角的显示效果
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
this.event.mouse_left(leftEvent)
|
||||
this.event.mouse_move((movement, car) => {
|
||||
this.tip.setPosition(car, movement.endPosition.x, movement.endPosition.y)
|
||||
if (this.cachePositions.length) {
|
||||
this.positions = this.cachePositions.concat(car)
|
||||
let p = this.cartesian3Towgs84(car, this.viewer)
|
||||
let pc = this.cartesian3Towgs84(this.positions[1], this.viewer)
|
||||
let from = turf.point([pc.lng, pc.lat]);
|
||||
let to = turf.point([p.lng, p.lat]);
|
||||
let options = { units: 'kilometers' };
|
||||
let distance = turf.rhumbDistance(from, to, options);
|
||||
|
||||
let bearing = 0;
|
||||
let destination = turf.destination(from, distance, bearing, options);
|
||||
this.positions[0] = Cesium.Cartesian3.fromDegrees(...destination.geometry.coordinates)
|
||||
}
|
||||
if (this.positions.length > 2) {
|
||||
//需要开始计算夹角
|
||||
this.caculateAngle(
|
||||
[this.positions[0], this.positions[1]],
|
||||
[this.positions[2], this.positions[1]])
|
||||
}
|
||||
})
|
||||
this.event.mouse_right((movement, car) => {
|
||||
this.cancel()
|
||||
})
|
||||
|
||||
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.cancel()
|
||||
}
|
||||
else {
|
||||
leftEvent(pos, cartesian)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default MeasureAzimuth
|
Reference in New Issue
Block a user