huweili-cesium 1.2.30 → 1.2.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/js/basis.js CHANGED
@@ -126,8 +126,17 @@ export function basicConfig() {
126
126
  e.preventDefault();
127
127
  });
128
128
 
129
+ // 获取现有的事件处理器(如果有)
130
+ const existingLeftClickHandler = map.screenSpaceEventHandler.getInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
131
+ const existingRightClickHandler = map.screenSpaceEventHandler.getInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
132
+
129
133
  // 添加右键点击事件监听
130
134
  map.screenSpaceEventHandler.setInputAction((click) => {
135
+ // 先调用已有的处理器(如果有)
136
+ if (existingRightClickHandler) {
137
+ existingRightClickHandler(click);
138
+ }
139
+
131
140
  // 获取点击位置的笛卡尔坐标
132
141
  const cartesian = map.camera.pickEllipsoid(click.position, map.scene.globe.ellipsoid);
133
142
  if (cartesian) {
@@ -145,7 +154,7 @@ export function basicConfig() {
145
154
  cartesian,
146
155
  };
147
156
 
148
- console.log('右键点击事件触发,点击位置:', position);
157
+ console.log('右键点击位置:', position);
149
158
  callbacks.onRightClick?.(position);
150
159
  } else {
151
160
  callbacks.onRightClick?.({
@@ -157,6 +166,11 @@ export function basicConfig() {
157
166
 
158
167
  // 添加左键点击事件,用于隐藏弹窗
159
168
  map.screenSpaceEventHandler.setInputAction((click) => {
169
+ // 先调用已有的处理器(如果有)
170
+ if (existingLeftClickHandler) {
171
+ existingLeftClickHandler(click);
172
+ }
173
+
160
174
  // 获取点击位置的笛卡尔坐标
161
175
  const cartesian = map.camera.pickEllipsoid(click.position, map.scene.globe.ellipsoid);
162
176
  if (cartesian) {
@@ -166,7 +180,7 @@ export function basicConfig() {
166
180
  const lat = Cesium.Math.toDegrees(cartographic.latitude);
167
181
  const height = cartographic.height;
168
182
 
169
- console.log('左键点击事件触发,点击位置:', {
183
+ console.log('左键点击位置:', {
170
184
  lng: Number(lng),
171
185
  lat: Number(lat),
172
186
  height: Number(height)
@@ -218,17 +218,40 @@ export class ClickHandler {
218
218
  */
219
219
  ensureMapHandler(map, mapId) {
220
220
  if (!this.handlers.has(mapId) && map) {
221
- const handler = new Cesium.ScreenSpaceEventHandler(map.scene.canvas)
222
-
223
- handler.setInputAction((click) => {
224
- this.handleClick(map, mapId, click, 'left')
225
- }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
226
-
227
- handler.setInputAction((click) => {
228
- this.handleClick(map, mapId, click, 'right')
229
- }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
230
-
231
- this.handlers.set(mapId, handler)
221
+ // 使用地图自带的 screenSpaceEventHandler 而不是创建新的
222
+ // 这样可以避免与 basis.js 中的 mouseController 冲突
223
+ const handler = map.screenSpaceEventHandler
224
+
225
+ // 获取已有的事件处理器(如果有)
226
+ const existingLeftClickHandler = handler.getInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
227
+ const existingRightClickHandler = handler.getInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
228
+
229
+ // 存储需要清理的事件回调引用
230
+ const cleanupCallbacks = {
231
+ leftClick: null,
232
+ rightClick: null
233
+ }
234
+
235
+ cleanupCallbacks.leftClick = (click) => {
236
+ // 先调用已有的处理器(如果有)
237
+ if (existingLeftClickHandler) {
238
+ existingLeftClickHandler(click);
239
+ }
240
+ this.handleClick(map, mapId, click, 'left');
241
+ }
242
+
243
+ cleanupCallbacks.rightClick = (click) => {
244
+ // 先调用已有的处理器(如果有)
245
+ if (existingRightClickHandler) {
246
+ existingRightClickHandler(click);
247
+ }
248
+ this.handleClick(map, mapId, click, 'right');
249
+ }
250
+
251
+ handler.setInputAction(cleanupCallbacks.leftClick, Cesium.ScreenSpaceEventType.LEFT_CLICK);
252
+ handler.setInputAction(cleanupCallbacks.rightClick, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
253
+
254
+ this.handlers.set(mapId, { handler, cleanupCallbacks });
232
255
  }
233
256
  }
234
257
 
@@ -280,9 +303,16 @@ export class ClickHandler {
280
303
  }
281
304
  })
282
305
 
283
- const handler = this.handlers.get(mapId)
284
- if (handler) {
285
- handler.destroy()
306
+ const handlerData = this.handlers.get(mapId)
307
+ if (handlerData) {
308
+ // 只移除我们添加的事件监听器,不要销毁 map.screenSpaceEventHandler
309
+ const { handler, cleanupCallbacks } = handlerData
310
+ if (cleanupCallbacks.leftClick) {
311
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
312
+ }
313
+ if (cleanupCallbacks.rightClick) {
314
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK)
315
+ }
286
316
  this.handlers.delete(mapId)
287
317
  }
288
318
  }
package/js/setPoint.js CHANGED
@@ -352,9 +352,183 @@ export function setPoint(baseUrl) {
352
352
  return modelEntity
353
353
  }
354
354
 
355
+ /**
356
+ * 拾取地图经纬度并放置图片点位
357
+ *
358
+ * @param {Object} options 配置选项
359
+ * @param {string} options.id 点位唯一标识
360
+ * @param {string} options.image 点位图片地址,支持本地 public 路径或网络地址
361
+ * @param {string} [options.mapId] 地图ID
362
+ * @param {number} [options.width=32] 图片宽度
363
+ * @param {number} [options.height=32] 图片高度
364
+ * @param {number} [options.pointHeight=0] 点位高度
365
+ * @param {Function} [options.onPick] 拾取成功回调
366
+ * @returns {Function|null} 取消拾取监听的函数
367
+ */
368
+ const pickMapPointByClick = (options = {}) => {
369
+ const { id, image, mapId, width = 32, height = 32, pointHeight = 0, onPick } = options
370
+ const map = mapStore.getMap(mapId)
371
+
372
+ if (!map) {
373
+ console.error('地图实例不存在')
374
+ return null
375
+ }
376
+
377
+ if (!id) {
378
+ console.error('点位id不能为空')
379
+ return null
380
+ }
381
+
382
+ if (!image) {
383
+ console.error('点位图片不能为空')
384
+ return null
385
+ }
386
+
387
+ const handler = new Cesium.ScreenSpaceEventHandler(map.scene.canvas)
388
+
389
+ handler.setInputAction((click) => {
390
+ const cartesian = map.camera.pickEllipsoid(click.position, map.scene.globe.ellipsoid)
391
+ if (!cartesian) return
392
+
393
+ const cartographic = Cesium.Cartographic.fromCartesian(cartesian)
394
+ const lng = Cesium.Math.toDegrees(cartographic.longitude)
395
+ const lat = Cesium.Math.toDegrees(cartographic.latitude)
396
+
397
+ const point = createPickedImagePoint({
398
+ id,
399
+ image,
400
+ lng,
401
+ lat,
402
+ height: pointHeight,
403
+ width,
404
+ heightPixel: height,
405
+ mapId
406
+ })
407
+
408
+ if (typeof onPick === 'function') {
409
+ onPick({
410
+ id,
411
+ lng,
412
+ lat,
413
+ height: pointHeight,
414
+ cartesian,
415
+ point,
416
+ mapId
417
+ })
418
+ }
419
+
420
+ handler.destroy()
421
+ }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
422
+
423
+ return () => {
424
+ if (!handler.isDestroyed()) {
425
+ handler.destroy()
426
+ }
427
+ }
428
+ }
429
+
430
+ /**
431
+ * 根据经纬度创建图片点位
432
+ * @param {Object} options 配置选项
433
+ * @param {string} options.id 点位唯一标识
434
+ * @param {string} options.image 点位图片地址
435
+ * @param {number} options.lng 经度
436
+ * @param {number} options.lat 纬度
437
+ * @param {number} [options.height=0] 点位高度
438
+ * @param {number} [options.width=32] 图片宽度
439
+ * @param {number} [options.heightPixel=32] 图片高度
440
+ * @param {string} [options.mapId] 地图ID
441
+ * @returns {Cesium.Entity|null} 创建的点位实体
442
+ */
443
+ const createPickedImagePoint = (options = {}) => {
444
+ const { id, image, lng, lat, height = 0, width = 32, heightPixel = 32, mapId } = options
445
+ const map = mapStore.getMap(mapId)
446
+
447
+ if (!map) {
448
+ console.error('地图实例不存在')
449
+ return null
450
+ }
451
+
452
+ if (!id) {
453
+ console.error('点位id不能为空')
454
+ return null
455
+ }
456
+
457
+ if (!image) {
458
+ console.error('点位图片不能为空')
459
+ return null
460
+ }
461
+
462
+ if (mapStore.hasGraphicMap(id, mapId)) {
463
+ console.warn(`图片点位已存在,ID: ${id}`)
464
+ return mapStore.getGraphicMap(id, mapId)
465
+ }
466
+
467
+ const entity = map.entities.add({
468
+ id,
469
+ position: Cesium.Cartesian3.fromDegrees(Number(lng), Number(lat), Number(height)),
470
+ billboard: {
471
+ image,
472
+ width,
473
+ height: heightPixel,
474
+ verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
475
+ horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
476
+ disableDepthTestDistance: Number.POSITIVE_INFINITY
477
+ }
478
+ })
479
+
480
+ entity.destroy = () => {
481
+ map.entities.remove(entity)
482
+ mapStore.removeGraphicMap(id, mapId)
483
+ }
484
+
485
+ mapStore.setGraphicMap(id, entity, mapId)
486
+ return entity
487
+ }
488
+
489
+ /**
490
+ * 删除/销毁拾取创建的图片点位
491
+ * @param {string|Object} pointId 点位id,或包含 id/mapId 的对象
492
+ * @param {string} [mapId] 地图ID
493
+ * @returns {boolean} 是否删除成功
494
+ */
495
+ const destroyPickedImagePoint = (pointId, mapId) => {
496
+ const id = typeof pointId === 'object' ? pointId?.id : pointId
497
+ const targetMapId = typeof pointId === 'object' ? pointId?.mapId : mapId
498
+ const map = mapStore.getMap(targetMapId)
499
+
500
+ if (!map) {
501
+ console.error('地图实例不存在')
502
+ return false
503
+ }
504
+
505
+ if (!id) {
506
+ console.error('点位id不能为空')
507
+ return false
508
+ }
509
+
510
+ const point = mapStore.getGraphicMap(id, targetMapId)
511
+ if (!point) {
512
+ console.warn(`图片点位不存在,ID: ${id}`)
513
+ return false
514
+ }
515
+
516
+ if (typeof point.destroy === 'function') {
517
+ point.destroy()
518
+ } else {
519
+ map.entities.remove(point)
520
+ mapStore.removeGraphicMap(id, targetMapId)
521
+ }
522
+
523
+ return true
524
+ }
525
+
355
526
  return {
356
527
  setPointByDiv,
357
528
  setPointByGlb,
358
- setDronePointByGlb
529
+ setDronePointByGlb,
530
+ pickMapPointByClick,
531
+ createPickedImagePoint,
532
+ destroyPickedImagePoint
359
533
  }
360
534
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huweili-cesium",
3
- "version": "1.2.30",
3
+ "version": "1.2.32",
4
4
  "description": "基于 Cesium 的地图工具库(无人机态势、轨迹、围栏、工具栏等)",
5
5
  "type": "module",
6
6
  "main": "./index.js",