huweili-cesium 1.2.31 → 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/setPoint.js +175 -1
- package/package.json +1 -1
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
|
}
|