Files
sdk4.0/src/Measure/MeasureLocation/index.js

183 lines
5.6 KiB
JavaScript
Raw Normal View History

2025-07-03 13:54:01 +08:00
/**
* @name: index
* @author: Administrator
* @date: 2022-07-22 16:13
* @descriptionindex
* @update: 2022-07-22 16:13
*/
import Measure from "../index";
import { Proj } from "../../Tools/proj";
import { getCoordinateSystem } from "../../Global/global";
class MeasureLocation extends Measure {
/**
* @constructor
* @param sdk
* @description 坐标测量
* */
constructor(sdk) {
super(sdk, {text: ""});
this.defaultColor = "#f11515"
this.locationID = this.randomString()
this.position = new Cesium.Cartesian3()
this.text = ""
}
static createLocation(that) {
that.viewer.entities.add(new Cesium.Entity({
id: that.locationID,
show: false,
position: new Cesium.CallbackProperty(() => {
return that.position
}, false),
label: {
text: new Cesium.CallbackProperty(() => {
return that.text
}, false),
//标注文字描述
font: '22px Microsoft YaHei',
fillColor: new Cesium.Color.fromCssColorString(that.defaultColor),
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
//标注的遮挡距离设置为100则视角与标注的距离大于100米时会有遮挡
// distanceDisplayCondition: this.distanceDisplayCondition,
// scale: this.options.label.scale,
horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
pixelOffset: new Cesium.Cartesian2(
-100,
-50
),
// disableDepthTestDistance: this.disableDepthTestDistance,
},
billboard: {
image: that.getSourceRootPath() + "/img/location.png",
color: Cesium.Color.fromCssColorString(`rgba(255,255,255,0.99)`),
disableDepthTestDistance: Number.POSITIVE_INFINITY,
// scaleByDistance: new Cesium.NearFarScalar(
// 2000,
// 1,
// 1000000,
// 0
// ),
scale: 1,
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
width: 48,
height: 48
}
}))
}
static create_point(that) {
let id = that.randomString()
that.viewer.entities.add(
new Cesium.Entity({
id: id,
position: new Cesium.CallbackProperty(() => {
return that.position
}, false),
billboard: {
image: that.getSourceRootPath() + '/img/point.png',
color: Cesium.Color.fromCssColorString(`rgba(255,255,255,0.99)`),
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
color: Cesium.Color.WHITE.withAlpha(0.99)
}
})
)
return id
}
/**
* 开始测量
*/
start() {
if (!YJ.Measure.GetMeasureStatus()) {
super.start()
this.cache_id = MeasureLocation.create_point(this)
MeasureLocation.createLocation(this)
let leftEvent = (movement, cartesian) => {
this.position = cartesian
let entity = this.viewer.entities.getById(this.locationID)
if(entity) {
entity.show = true
}
let p = this.cartesian3Towgs84(cartesian, this.viewer)
let coordinateSystem = getCoordinateSystem()
if(coordinateSystem==='EPSG:4326') {
this.text = `经度:${Number(p.lng.toFixed(8))}\n纬度:${Number(p.lat.toFixed(8))}\n海拔:${Number(p.alt.toFixed(2))}`
}
else {
let result = this.convert([{x: p.lng, y: p.lat, z: p.alt}], 'EPSG:4326', coordinateSystem)
this.text = `x${Number(result.points[0].x.toFixed(8))}\ny${Number(result.points[0].y.toFixed(8))}\nz${Number(result.points[0].z.toFixed(2))}`
}
this.end()
}
this.event.mouse_left(leftEvent)
this.event.mouse_right((movement, cartesian) => {
this.destroy()
this.end()
})
this.event.mouse_move((movement, cartesian) => {
this.tip.setPosition(cartesian, movement.endPosition.x, movement.endPosition.y)
let entity = this.viewer.entities.getById(this.locationID)
if(entity) {
entity.show = true
}
this.position = cartesian
let p = this.cartesian3Towgs84(cartesian, this.viewer)
let coordinateSystem = getCoordinateSystem()
if(coordinateSystem==='EPSG:4326') {
this.text = `经度:${Number(p.lng.toFixed(8))}\n纬度:${Number(p.lat.toFixed(8))}\n海拔:${Number(p.alt.toFixed(2))}`
}
else {
let result = this.convert([{x: p.lng, y: p.lat, z: p.alt}], 'EPSG:4326', coordinateSystem)
this.text = `x${Number(result.points[0].x.toFixed(8))}\ny${Number(result.points[0].y.toFixed(8))}\nz${Number(result.points[0].z.toFixed(2))}`
}
})
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.destroy()
this.end()
}
else {
leftEvent(pos, cartesian)
}
})
})
}
}
/**
* 清除测量
*/
destroy() {
this.remove_entity(this.locationID)
this.remove_entity(this.cache_id)
super.destroy()
}
/**
* 结束测量
*/
end() {
super.end()
}
}
export default MeasureLocation