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

pull/8/head
cbwu 1 year ago
parent 0b9b0c2432
commit fc4c74e21a

@ -2,7 +2,7 @@
* @Author: cbwu 504-wuchengbo@htsdfp.com * @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-27 08:43:44 * @Date: 2024-03-27 08:43:44
* @LastEditors: cbwu * @LastEditors: cbwu
* @LastEditTime: 2024-04-16 19:00:18 * @LastEditTime: 2024-04-22 11:23:47
* @Description: Polyline * @Description: Polyline
*/ */
import { import {
@ -14,6 +14,7 @@ import {
Entity, Entity,
CallbackProperty, CallbackProperty,
PolylineDashMaterialProperty, PolylineDashMaterialProperty,
Cartesian2,
HeightReference, HeightReference,
} from 'cesium' } from 'cesium'
import { import {
@ -27,17 +28,8 @@ import EditGeometry from '@/utils/map/draw/editGeometry'
import { getDistance } from '@/utils/map/geocomputation.ts' import { getDistance } from '@/utils/map/geocomputation.ts'
import { Angle } from '@/utils/map/angle.ts' import { Angle } from '@/utils/map/angle.ts'
import { TextLabel } from '@/utils/map/geometry/textLabel.ts' import { TextLabel } from '@/utils/map/geometry/textLabel.ts'
type EntityOptions = { import { EntityOptions } from '@/types/entityoptions.ts'
id?: string
name?: string
show?: boolean
pixelSize?: number
color?: Color
fillColor?: Color
fill?: boolean
width?: number
outlineWidth?: number
}
export default class CreatePolyline { export default class CreatePolyline {
viewer: Viewer viewer: Viewer
handler: ScreenSpaceEventHandler handler: ScreenSpaceEventHandler
@ -57,6 +49,9 @@ export default class CreatePolyline {
clickTimeout: any clickTimeout: any
altitudeOffset: number = 20 //相对高度 altitudeOffset: number = 20 //相对高度
vDashLinePosition: Cartesian3[][] = [] //垂直辅助线坐标数组 vDashLinePosition: Cartesian3[][] = [] //垂直辅助线坐标数组
// 存储第一次点击的信息
firstClickPosition: Cartesian2 | null = null
firstClickTime: number | null = null
// layer: CustomDataSource // layer: CustomDataSource
defaultStyle: EntityOptions = { defaultStyle: EntityOptions = {
// id: 'Polyline' + String(PolylineEntity.id), // id: 'Polyline' + String(PolylineEntity.id),
@ -111,17 +106,9 @@ export default class CreatePolyline {
private leftClickCallBack = ( private leftClickCallBack = (
event: ScreenSpaceEventHandler.PositionedEvent, event: ScreenSpaceEventHandler.PositionedEvent,
) => { ) => {
const pickedObject = this.viewer.scene.pick(event.position) //根据屏幕坐标判断是否为同一点
if (pickedObject) { if (this.isSamplePosition(event.position.clone())) return
//点击同一位置,返回
if (
pickedObject.id.id ===
this.polyline?.controlPointsID[this.positions.length - 1]
) {
console.log('********click the same point')
return
}
}
let cartesian3 = cartesian2ToCartesian3(this.viewer, event.position) let cartesian3 = cartesian2ToCartesian3(this.viewer, event.position)
if (cartesian3 != undefined) { if (cartesian3 != undefined) {
if (!this.polyline) { if (!this.polyline) {
@ -152,24 +139,24 @@ export default class CreatePolyline {
this.viewer.scene.requestRender() //刷新 this.viewer.scene.requestRender() //刷新
// 计算2点距离 // 计算2点距离
if (this.positions.length >= 2 && this.bMeasure) { if (this.positions.length >= 2 && this.bMeasure) {
let distance = getDistance( const distance = getDistance(
this.positions[this.positions.length - 1], this.positions[this.positions.length - 1],
this.positions[this.positions.length - 2], this.positions[this.positions.length - 2],
) )
this.totalDistance += distance this.totalDistance += distance
// 计算2点方位角 // 计算2点方位角
let azimuth = Angle.getAzimuth( const azimuth = Angle.getAzimuth(
this.positions[this.positions.length - 2], this.positions[this.positions.length - 2],
this.positions[this.positions.length - 1], this.positions[this.positions.length - 1],
) )
// 计算2点的中间点 // 计算2点的中间点
let midPoint = Cartesian3.midpoint( const midPoint = Cartesian3.midpoint(
this.positions[this.positions.length - 1], this.positions[this.positions.length - 1],
this.positions[this.positions.length - 2], this.positions[this.positions.length - 2],
new Cartesian3(), new Cartesian3(),
) )
// 添加label // 添加label
let labelText = `距离: ${distance.toFixed(2)}km, 方位角: ${azimuth}°` const labelText = `距离: ${distance.toFixed(2)}km, 方位角: ${azimuth}°`
new TextLabel(this.viewer, midPoint, labelText) 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 * @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-28 16:22:58 * @Date: 2024-03-28 16:22:58
* @LastEditors: cbwu * @LastEditors: cbwu
* @LastEditTime: 2024-04-13 10:45:37 * @LastEditTime: 2024-04-22 11:32:00
* @Description: * @Description:
*/ */
import { import {
@ -41,7 +41,11 @@ export abstract class BaseGeometry extends CustomDataSource {
* @param bAddControlPoint * @param bAddControlPoint
* @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) { if (index === -1) {
//插入尾部 //插入尾部
this.positions.push(pos) 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,13 +22,13 @@ import {Cartesian3, Entity, Color, Viewer, Cartesian2, HorizontalOrigin, Vertica
Zhaipeixiu Zhaipeixiu
*/ */
export type textLabelOptions = { export type textLabelOptions = {
showBackground?: boolean, //显示背景 showBackground?: boolean //显示背景
backgroundColor?: Color, //背景色 backgroundColor?: Color //背景色
backgroundPadding?: any, //padding值 backgroundPadding?: any //padding值
fillColor: Color, fillColor: Color
horizontalOrigin?: any, //水平对齐方式 horizontalOrigin?: any //水平对齐方式
verticalOrigin?: any, //竖直对齐方式 verticalOrigin?: any //竖直对齐方式
outlineColor?: any, outlineColor?: any
outlineWidth?: any outlineWidth?: any
} }
@ -31,7 +47,12 @@ export class TextLabel{
_cesiumLabel: Entity | undefined = undefined _cesiumLabel: Entity | undefined = undefined
// 构造函数新建label并显示 // 构造函数新建label并显示
constructor(viewer: Viewer, position: Cartesian3, text: string, options?: textLabelOptions) { constructor(
viewer: Viewer,
position: Cartesian3,
text: string,
options?: textLabelOptions,
) {
this._viewer = viewer this._viewer = viewer
this._defaultStyle = { ...this._defaultStyle, ...options } this._defaultStyle = { ...this._defaultStyle, ...options }
@ -40,7 +61,7 @@ export class TextLabel{
name: 'default', name: 'default',
label: { label: {
text: text, text: text,
scale: .6, scale: 0.6,
pixelOffset: new Cartesian2(20, -20), pixelOffset: new Cartesian2(20, -20),
showBackground: this._defaultStyle.showBackground, //显示背景 showBackground: this._defaultStyle.showBackground, //显示背景
backgroundColor: this._defaultStyle.backgroundColor, //背景色 backgroundColor: this._defaultStyle.backgroundColor, //背景色
@ -50,8 +71,8 @@ export class TextLabel{
outlineWidth: this._defaultStyle.outlineWidth, outlineWidth: this._defaultStyle.outlineWidth,
horizontalOrigin: this._defaultStyle.horizontalOrigin, //对齐方式 horizontalOrigin: this._defaultStyle.horizontalOrigin, //对齐方式
verticalOrigin: this._defaultStyle.verticalOrigin, verticalOrigin: this._defaultStyle.verticalOrigin,
heightReference: HeightReference.CLAMP_TO_GROUND heightReference: HeightReference.CLAMP_TO_GROUND,
} },
}) })
this._viewer.entities.add(this._cesiumLabel) this._viewer.entities.add(this._cesiumLabel)
} }

Loading…
Cancel
Save