fix: 修复绘制航线双击结束判定逻辑。

pull/8/head
cbwu 11 months ago
parent 0b9b0c2432
commit fc4c74e21a

@ -2,7 +2,7 @@
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-27 08:43:44
* @LastEditors: cbwu
* @LastEditTime: 2024-04-16 19:00:18
* @LastEditTime: 2024-04-22 11:23:47
* @Description: Polyline
*/
import {
@ -14,6 +14,7 @@ import {
Entity,
CallbackProperty,
PolylineDashMaterialProperty,
Cartesian2,
HeightReference,
} from 'cesium'
import {
@ -27,17 +28,8 @@ import EditGeometry from '@/utils/map/draw/editGeometry'
import { getDistance } from '@/utils/map/geocomputation.ts'
import { Angle } from '@/utils/map/angle.ts'
import { TextLabel } from '@/utils/map/geometry/textLabel.ts'
type EntityOptions = {
id?: string
name?: string
show?: boolean
pixelSize?: number
color?: Color
fillColor?: Color
fill?: boolean
width?: number
outlineWidth?: number
}
import { EntityOptions } from '@/types/entityoptions.ts'
export default class CreatePolyline {
viewer: Viewer
handler: ScreenSpaceEventHandler
@ -57,6 +49,9 @@ export default class CreatePolyline {
clickTimeout: any
altitudeOffset: number = 20 //相对高度
vDashLinePosition: Cartesian3[][] = [] //垂直辅助线坐标数组
// 存储第一次点击的信息
firstClickPosition: Cartesian2 | null = null
firstClickTime: number | null = null
// layer: CustomDataSource
defaultStyle: EntityOptions = {
// id: 'Polyline' + String(PolylineEntity.id),
@ -111,17 +106,9 @@ export default class CreatePolyline {
private leftClickCallBack = (
event: ScreenSpaceEventHandler.PositionedEvent,
) => {
const pickedObject = this.viewer.scene.pick(event.position)
if (pickedObject) {
//点击同一位置,返回
if (
pickedObject.id.id ===
this.polyline?.controlPointsID[this.positions.length - 1]
) {
console.log('********click the same point')
return
}
}
//根据屏幕坐标判断是否为同一点
if (this.isSamplePosition(event.position.clone())) return
let cartesian3 = cartesian2ToCartesian3(this.viewer, event.position)
if (cartesian3 != undefined) {
if (!this.polyline) {
@ -152,24 +139,24 @@ export default class CreatePolyline {
this.viewer.scene.requestRender() //刷新
// 计算2点距离
if (this.positions.length >= 2 && this.bMeasure) {
let distance = getDistance(
const distance = getDistance(
this.positions[this.positions.length - 1],
this.positions[this.positions.length - 2],
)
this.totalDistance += distance
// 计算2点方位角
let azimuth = Angle.getAzimuth(
const azimuth = Angle.getAzimuth(
this.positions[this.positions.length - 2],
this.positions[this.positions.length - 1],
)
// 计算2点的中间点
let midPoint = Cartesian3.midpoint(
const midPoint = Cartesian3.midpoint(
this.positions[this.positions.length - 1],
this.positions[this.positions.length - 2],
new Cartesian3(),
)
// 添加label
let labelText = `距离: ${distance.toFixed(2)}km, 方位角: ${azimuth}°`
const labelText = `距离: ${distance.toFixed(2)}km, 方位角: ${azimuth}°`
new TextLabel(this.viewer, midPoint, labelText)
}
}
@ -272,4 +259,28 @@ export default class CreatePolyline {
},
})
}
//判断点击是否同一位置
isSamplePosition(clickPosition: Cartesian2) {
if (this.firstClickPosition) {
const dist = Cartesian2.distance(this.firstClickPosition, clickPosition)
if (dist <= 3) {
console.log('********click the same point0')
return true
}
}
this.firstClickPosition = clickPosition
// const pickedObject = this.viewer.scene.pick(clickPosition)
// if (pickedObject) {
// //点击同一位置,返回
// if (
// pickedObject.id.id ===
// this.polyline?.controlPointsID[this.positions.length - 1]
// ) {
// console.log('********click the same point')
// return
// }
// }
return false
}
}

@ -2,7 +2,7 @@
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-28 16:22:58
* @LastEditors: cbwu
* @LastEditTime: 2024-04-13 10:45:37
* @LastEditTime: 2024-04-22 11:32:00
* @Description:
*/
import {
@ -41,7 +41,11 @@ export abstract class BaseGeometry extends CustomDataSource {
* @param bAddControlPoint
* @bAddControlPoint
*/
public addPoint(pos: Cartesian3, index: number = -1, bAddControlPoint: boolean = true,) {
public addPoint(
pos: Cartesian3,
index: number = -1,
bAddControlPoint: boolean = true,
) {
if (index === -1) {
//插入尾部
this.positions.push(pos)

@ -1,4 +1,20 @@
import {Cartesian3, Entity, Color, Viewer, Cartesian2, HorizontalOrigin, VerticalOrigin, HeightReference} from "cesium";
/*
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-04-16 18:59:56
* @LastEditors: cbwu
* @LastEditTime: 2024-04-22 09:11:33
* @Description:
*/
import {
Cartesian3,
Entity,
Color,
Viewer,
Cartesian2,
HorizontalOrigin,
VerticalOrigin,
HeightReference,
} from 'cesium'
/**
@ -6,59 +22,64 @@ import {Cartesian3, Entity, Color, Viewer, Cartesian2, HorizontalOrigin, Vertica
Zhaipeixiu
*/
export type textLabelOptions = {
showBackground?: boolean, //显示背景
backgroundColor?: Color, //背景色
backgroundPadding?: any, //padding值
fillColor: Color,
horizontalOrigin?: any, //水平对齐方式
verticalOrigin?: any, //竖直对齐方式
outlineColor?: any,
outlineWidth?: any
showBackground?: boolean //显示背景
backgroundColor?: Color //背景色
backgroundPadding?: any //padding值
fillColor: Color
horizontalOrigin?: any //水平对齐方式
verticalOrigin?: any //竖直对齐方式
outlineColor?: any
outlineWidth?: any
}
export class TextLabel{
_viewer: Viewer|undefined = undefined
_defaultStyle: textLabelOptions = {
showBackground: true, //显示背景
backgroundColor: Color.BLACK, //背景色
backgroundPadding: new Cartesian2(10, 10), //padding值
fillColor: Color.WHITE,
outlineColor: Color.WHITESMOKE,
outlineWidth: 1.0,
horizontalOrigin: HorizontalOrigin.CENTER,//对齐方式
verticalOrigin: VerticalOrigin.CENTER,
}
_cesiumLabel: Entity|undefined = undefined
export class TextLabel {
_viewer: Viewer | undefined = undefined
_defaultStyle: textLabelOptions = {
showBackground: true, //显示背景
backgroundColor: Color.BLACK, //背景色
backgroundPadding: new Cartesian2(10, 10), //padding值
fillColor: Color.WHITE,
outlineColor: Color.WHITESMOKE,
outlineWidth: 1.0,
horizontalOrigin: HorizontalOrigin.CENTER, //对齐方式
verticalOrigin: VerticalOrigin.CENTER,
}
_cesiumLabel: Entity | undefined = undefined
// 构造函数新建label并显示
constructor(viewer: Viewer, position: Cartesian3, text: string, options?: textLabelOptions) {
this._viewer = viewer
this._defaultStyle = {...this._defaultStyle, ...options}
// 构造函数新建label并显示
constructor(
viewer: Viewer,
position: Cartesian3,
text: string,
options?: textLabelOptions,
) {
this._viewer = viewer
this._defaultStyle = { ...this._defaultStyle, ...options }
this._cesiumLabel = new Entity({
position: position,
name: 'default',
label: {
text: text,
scale: .6,
pixelOffset: new Cartesian2(20, -20),
showBackground: this._defaultStyle.showBackground, //显示背景
backgroundColor: this._defaultStyle.backgroundColor, //背景色
backgroundPadding: this._defaultStyle.backgroundPadding, //padding值
fillColor: this._defaultStyle.fillColor,
outlineColor: this._defaultStyle.outlineColor,
outlineWidth: this._defaultStyle.outlineWidth,
horizontalOrigin: this._defaultStyle.horizontalOrigin, //对齐方式
verticalOrigin: this._defaultStyle.verticalOrigin,
heightReference: HeightReference.CLAMP_TO_GROUND
}
})
this._viewer.entities.add(this._cesiumLabel)
}
this._cesiumLabel = new Entity({
position: position,
name: 'default',
label: {
text: text,
scale: 0.6,
pixelOffset: new Cartesian2(20, -20),
showBackground: this._defaultStyle.showBackground, //显示背景
backgroundColor: this._defaultStyle.backgroundColor, //背景色
backgroundPadding: this._defaultStyle.backgroundPadding, //padding值
fillColor: this._defaultStyle.fillColor,
outlineColor: this._defaultStyle.outlineColor,
outlineWidth: this._defaultStyle.outlineWidth,
horizontalOrigin: this._defaultStyle.horizontalOrigin, //对齐方式
verticalOrigin: this._defaultStyle.verticalOrigin,
heightReference: HeightReference.CLAMP_TO_GROUND,
},
})
this._viewer.entities.add(this._cesiumLabel)
}
//移除label
public remove(){
if(this._cesiumLabel !== undefined && this._viewer!==null)
this._viewer.entities.remove(this._cesiumLabel)
}
//移除label
public remove() {
if (this._cesiumLabel !== undefined && this._viewer !== null)
this._viewer.entities.remove(this._cesiumLabel)
}
}

Loading…
Cancel
Save