cesium-plot-js/src/draw.ts

90 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-08-09 09:55:29 +00:00
import * as Utils from './utils';
export default class Draw {
cesium: any;
viewer: any;
arrowLengthScale: number = 5;
maxArrowLength: number = 2;
tailWidthFactor: number;
neckWidthFactor: number;
headWidthFactor: number;
headAngle: number;
neckAngle: number;
eventHandler: any;
clickListener: any;
2023-08-10 02:23:56 +00:00
entity: any;
renderingPoints: any;
geometryPoints: any;
2023-08-09 09:55:29 +00:00
constructor(cesium, viewer) {
this.cesium = cesium;
this.viewer = viewer;
this.tailWidthFactor = 0.1;
this.neckWidthFactor = 0.2;
this.headWidthFactor = 0.25;
this.headAngle = Math.PI / 8.5;
this.neckAngle = Math.PI / 13;
}
2023-08-10 02:23:56 +00:00
onClick() {
2023-08-09 09:55:29 +00:00
// 添加点击事件监听器
this.eventHandler = new this.cesium.ScreenSpaceEventHandler(this.viewer.canvas);
this.clickListener = this.eventHandler.setInputAction((evt) => {
2023-08-10 02:23:56 +00:00
const cartesian = this.pixelToCartesian(evt.position);
this.addPoint(cartesian);
this.onMouseMove();
2023-08-09 09:55:29 +00:00
}, this.cesium.ScreenSpaceEventType.LEFT_CLICK);
}
removeEventListener() {
this.eventHandler.removeInputAction(this.cesium.ScreenSpaceEventType.LEFT_CLICK, this.clickListener);
}
2023-08-10 02:23:56 +00:00
onMouseMove() {
this.eventHandler.setInputAction((evt) => {
const cartesian = this.pixelToCartesian(evt.endPosition);
const lnglat = this.cartesianToLnglat(cartesian);
const lastPoint = this.cartesianToLnglat(this.points[this.points.length - 1]);
const distance = Utils.MathDistance(lnglat, lastPoint);
2023-08-09 09:55:29 +00:00
2023-08-10 02:23:56 +00:00
if (distance < 0.0001) {
return false;
}
this.updateMovingPoint(cartesian);
}, this.cesium.ScreenSpaceEventType.MOUSE_MOVE);
}
setGeometryPoints(geometryPoints) {
this.geometryPoints = geometryPoints;
}
addToMap() {
2023-08-09 09:55:29 +00:00
const callback = () => {
2023-08-10 02:23:56 +00:00
return new this.cesium.PolygonHierarchy(this.geometryPoints);
2023-08-09 09:55:29 +00:00
};
2023-08-10 02:23:56 +00:00
if (!this.entity) {
this.entity = this.viewer.entities.add({
polygon: new this.cesium.PolygonGraphics({
hierarchy: new this.cesium.CallbackProperty(callback, false),
show: true,
// fill: true,
// material: this.cesium.Color.LIGHTSKYBLUE.withAlpha(0.8),
}),
});
}
2023-08-09 09:55:29 +00:00
}
cartesianToLnglat(cartesian) {
var lnglat = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
var lat = this.cesium.Math.toDegrees(lnglat.latitude);
var lng = this.cesium.Math.toDegrees(lnglat.longitude);
return [lng, lat];
}
2023-08-10 02:23:56 +00:00
pixelToCartesian(position) {
const ray = this.viewer.camera.getPickRay(position);
const cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
return cartesian;
}
2023-08-09 09:55:29 +00:00
}