huweili-cesium 1.2.31 → 1.2.33

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/index.vue CHANGED
@@ -18,7 +18,7 @@ import { processMapConfigColors } from './js/tileProviders.js'
18
18
  import { applyImageryLayers, resolveOnlineBasemap } from './js/utils/mapImagery.js'
19
19
  import { createCustomToolbarButtons } from './js/customToolbarButtons.js'
20
20
  const { merge } = objectUtils()
21
- const { mouseController, setInitialCameraView, syncCameraInteractionMode } = basicConfig()
21
+ const { mouseController, setInitialCameraView, syncCameraInteractionMode, waitForMapReady } = basicConfig()
22
22
  const {
23
23
  addToolbarButton,
24
24
  addToolbarButtons,
@@ -239,10 +239,13 @@ const initCesium = async () => {
239
239
  })
240
240
  }
241
241
 
242
- mouseController(map, props.mapId, {
243
- onRightClick: (position) => emit('rightClick', position),
244
- onLeftClick: (event) => emit('leftClick', event),
245
- }) // 初始化鼠标控制器
242
+ const readyMap = await waitForMapReady(props.mapId)
243
+ if (readyMap) {
244
+ mouseController(readyMap, props.mapId, {
245
+ onRightClick: (position) => emit('rightClick', position),
246
+ onLeftClick: (event) => emit('leftClick', event),
247
+ }) // 初始化鼠标控制器
248
+ }
246
249
 
247
250
  mapStore.setMapInfo(
248
251
  'center',
package/js/basis.js CHANGED
@@ -21,6 +21,24 @@ export function basicConfig() {
21
21
  // 获取地图store实例
22
22
  const mapStore = useMapStore()
23
23
 
24
+ /**
25
+ * 等待地图实例初始化完成
26
+ * @param {string} mapInstanceId 地图实例ID
27
+ * @param {number} maxRetry 最大重试次数
28
+ * @param {number} interval 每次重试间隔,单位毫秒
29
+ * @returns {Promise<Cesium.Viewer|null>} 地图实例或 null
30
+ */
31
+ const waitForMapReady = async (mapInstanceId, maxRetry = 20, interval = 50) => {
32
+ for (let i = 0; i < maxRetry; i += 1) {
33
+ const map = mapStore.getMap(mapInstanceId)
34
+ if (map?.scene && map?.camera) {
35
+ return map
36
+ }
37
+ await new Promise((resolve) => setTimeout(resolve, interval))
38
+ }
39
+ return null
40
+ }
41
+
24
42
  /**
25
43
  * 检查坐标是否在中国境内
26
44
  * @param {*} longitude - 经度
@@ -741,6 +759,7 @@ export function basicConfig() {
741
759
  }
742
760
 
743
761
  return {
762
+ waitForMapReady,
744
763
  isInChina,
745
764
  mouseController,
746
765
  setCesiumCenter,
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.31",
3
+ "version": "1.2.33",
4
4
  "description": "基于 Cesium 的地图工具库(无人机态势、轨迹、围栏、工具栏等)",
5
5
  "type": "module",
6
6
  "main": "./index.js",