hn-map 1.1.16 → 1.1.17

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/src/map.ts CHANGED
@@ -24,7 +24,6 @@ export default (hnMap: any) => {
24
24
  sj_route_net: true,
25
25
  // Cesium配置
26
26
  cesium_accessToken: "",
27
- cesium_baseUrl: "",
28
27
  cesium_terrainProvider: null,
29
28
  cesium_imageryProvider: null,
30
29
  cesium_options: {},
@@ -473,14 +472,33 @@ export default (hnMap: any) => {
473
472
  event: any = {};
474
473
  level: any = null;
475
474
  // 数据源集合
476
- dataSources: any = new Cesium.CustomDataSource("hnMap_dataSources");
475
+ // dataSources: any = new Cesium.CustomDataSource("hnMap_dataSources");
476
+ // // 数据源集合 - 改为初始化为数组
477
+ // dataSources: any[] = [];
478
+
477
479
  private constructor(id: any, option: any) {
478
480
  this.layerList = [];
479
- this.level = 10;
481
+ this.level = 6;
480
482
  deepMerge(this.option, option);
481
483
  this.config = this.formatConfig(this.option);
484
+
485
+ // 创建Viewer前确保Cesium已加载
486
+ if (typeof Cesium === "undefined") {
487
+ throw new Error("Cesium未加载,请检查资源加载顺序");
488
+ }
489
+
482
490
  this.map = new Cesium.Viewer(id, this.config);
483
491
 
492
+ // 确保 dataSources 存在且是正确的类型
493
+ if (!this.map.dataSources) {
494
+ this.map.dataSources = new Cesium.DataSourceCollection();
495
+ }
496
+
497
+ // 隐藏版权信息
498
+ if (this.map._cesiumWidget && this.map._cesiumWidget._creditContainer) {
499
+ this.map._cesiumWidget._creditContainer.style.display = "none";
500
+ }
501
+
484
502
  // 监听相机移动事件
485
503
  this.map.camera.moveEnd.addEventListener(() => {
486
504
  const camera = this.map.camera;
@@ -493,8 +511,6 @@ export default (hnMap: any) => {
493
511
  this.event.cameraMoveEnd();
494
512
  }
495
513
  });
496
- // 添加自定义数据源
497
- this.map.dataSources.add(this.dataSources);
498
514
 
499
515
  // 设置初始视图
500
516
  const { lat, lng, level, heading, pitch, roll } = this.option;
@@ -516,10 +532,10 @@ export default (hnMap: any) => {
516
532
  static async create(id: string, option: any) {
517
533
  const instance = new cesium_map(id, option);
518
534
 
519
- // 返回一个 Promise,等待地图的 'ready' 事件
520
- await new Promise<void>((resolve) => {
521
- resolve();
522
- });
535
+ // 等待场景初始化完成
536
+ if (instance.map.scene.initializePromise) {
537
+ await instance.map.scene.initializePromise;
538
+ }
523
539
 
524
540
  return instance;
525
541
  }
@@ -527,103 +543,32 @@ export default (hnMap: any) => {
527
543
  formatConfig(option: any) {
528
544
  const config: any = {
529
545
  ...option.cesium_options,
530
- // 设置不显示商标
531
- imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
532
- url: "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
533
- enablePickFeatures: false,
534
- }),
546
+ animation: false, // 关闭动画控件
547
+ timeline: false, // 关闭时间轴
548
+ navigationHelpButton: false, // 关闭导航帮助按钮
549
+
550
+ selectionIndicator: false, // 关闭选择指示器
551
+ homeButton: false, // 关闭主页按钮
552
+
553
+ shouldAnimate: true, // 允许动画
535
554
  };
536
555
 
537
556
  // 设置地形 - 兼容性处理
538
557
  if (option.cesium_terrainProvider) {
539
558
  config.terrainProvider = option.cesium_terrainProvider;
540
- } else {
541
- config.terrainProvider = this.createTerrainProvider();
542
559
  }
543
-
544
560
  // 设置影像 - 兼容性处理
545
561
  if (option.cesium_imageryProvider) {
546
562
  config.imageryProvider = option.cesium_imageryProvider;
547
- } else {
548
- config.imageryProvider = this.createImageryProvider();
549
563
  }
550
564
 
551
565
  return config;
552
566
  }
553
567
 
554
- // 创建地形提供者(兼容各版本)
555
- createTerrainProvider() {
556
- // 检查是否支持createWorldTerrain
557
- if (typeof Cesium.createWorldTerrain === "function") {
558
- // 1.107及以上版本
559
- return Cesium.createWorldTerrain({
560
- requestWaterMask: true,
561
- requestVertexNormals: true,
562
- });
563
- } else if (Cesium.CesiumTerrainProvider) {
564
- // 1.106及以下版本
565
- const terrainUrl = this.getTerrainUrl();
566
- return new Cesium.CesiumTerrainProvider({
567
- url: terrainUrl,
568
- requestWaterMask: true,
569
- requestVertexNormals: true,
570
- });
571
- } else {
572
- // 没有地形
573
- console.warn("Cesium地形不可用,使用无地形模式");
574
- return new Cesium.EllipsoidTerrainProvider();
575
- }
576
- }
577
-
578
- // 获取地形URL(兼容处理)
579
- getTerrainUrl() {
580
- // 根据不同版本的API获取地形URL
581
- if (Cesium.IonResource && Cesium.IonResource.fromAssetId) {
582
- return Cesium.IonResource.fromAssetId(1);
583
- } else if (Cesium.Ion && Cesium.Ion.defaultServer) {
584
- return `${Cesium.Ion.defaultServer.url}/assets/1/quantized-mesh`;
585
- } else {
586
- // 使用Cesium官方的地形服务
587
- return "https://assets.cesium.com/1/";
588
- }
589
- }
590
-
591
- // 创建影像提供者(兼容各版本)
592
- createImageryProvider() {
593
- // 默认使用Bing地图或ArcGIS
594
- try {
595
- // 尝试创建Bing地图
596
- if (Cesium.BingMapsImageryProvider) {
597
- return new Cesium.BingMapsImageryProvider({
598
- url: "https://dev.virtualearth.net",
599
- key: this.option.cesium_bing_key || "", // 需要Bing Maps Key
600
- mapStyle: Cesium.BingMapsStyle.AERIAL,
601
- });
602
- }
603
- } catch (e) {
604
- console.warn("Bing地图不可用,尝试其他影像源", e);
605
- }
606
-
607
- // 使用ArcGIS作为备选
608
- if (Cesium.ArcGisMapServerImageryProvider) {
609
- return new Cesium.ArcGisMapServerImageryProvider({
610
- url: "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
611
- });
612
- }
613
-
614
- // 使用OpenStreetMap作为最后备选
615
- if (Cesium.OpenStreetMapImageryProvider) {
616
- return new Cesium.OpenStreetMapImageryProvider({
617
- url: "https://a.tile.openstreetmap.org/",
618
- });
619
- }
620
-
621
- // 没有可用影像提供者
622
- console.warn("没有可用的影像提供者");
623
- return undefined;
624
- }
568
+ // 添加图层
625
569
  // 添加图层
626
570
  addLayer(layer: any) {
571
+ console.log("Adding layer:", layer);
627
572
  if (this.layerList.find((v: any) => v.id === layer.id)) {
628
573
  console.error("已存在同名图层" + layer.id);
629
574
  return null;
@@ -631,33 +576,98 @@ export default (hnMap: any) => {
631
576
 
632
577
  this.layerList.push(layer);
633
578
 
634
- // 如果是数据源图层,添加到数据源集合
635
- if (layer.layerEntity && layer.layerEntity.entities) {
636
- this.map.dataSources.add(layer.layerEntity);
637
- }
638
-
639
- // 如果是Primitive图层,添加到Primitive集合
640
- if (
641
- layer.layerEntity &&
642
- layer.layerEntity instanceof Cesium.PrimitiveCollection
643
- ) {
644
- this.map.scene.primitives.add(layer.layerEntity);
579
+ const entity = layer.layerEntity;
580
+ console.log("Layer entity:", entity);
581
+ if (entity && this.map) {
582
+ try {
583
+ // 根据实体类型进行添加
584
+ if (entity instanceof Cesium.Entity) {
585
+ console.log("Adding Entity to entities");
586
+ this.map.entities.add(entity);
587
+ } else if (
588
+ entity instanceof Cesium.Primitive ||
589
+ entity instanceof Cesium.PrimitiveCollection
590
+ ) {
591
+ console.log("Adding Primitive to scene");
592
+ this.map.scene.primitives.add(entity);
593
+ } else if (entity instanceof Cesium.DataSource) {
594
+ // 确保 dataSources 是正确的类型后再调用 add 方法
595
+ console.log("Adding DataSource");
596
+ if (
597
+ this.map.dataSources &&
598
+ typeof this.map.dataSources.add === "function"
599
+ ) {
600
+ this.map.dataSources.add(entity);
601
+ } else {
602
+ console.warn(
603
+ "dataSources 不可用,尝试创建新的 DataSourceCollection"
604
+ );
605
+ if (!this.map.dataSources) {
606
+ this.map.dataSources = new Cesium.DataSourceCollection();
607
+ }
608
+ this.map.dataSources.add(entity);
609
+ }
610
+ } else if (Array.isArray(entity)) {
611
+ // 如果是实体数组
612
+ console.log("Adding array of entities");
613
+ entity.forEach((item) => {
614
+ if (item instanceof Cesium.Entity) {
615
+ this.map.entities.add(item);
616
+ }
617
+ });
618
+ } else {
619
+ // 默认尝试添加到 entities
620
+ console.log("Adding to entities (default)");
621
+ this.map.entities.add(entity);
622
+ }
623
+ } catch (error: any) {
624
+ console.error("添加图层失败:", error);
625
+ console.error("错误详情:", {
626
+ entity: entity,
627
+ entityType: typeof entity,
628
+ entityConstructor: entity?.constructor?.name,
629
+ });
630
+ }
645
631
  }
646
632
 
647
633
  return layer;
648
634
  }
649
-
650
635
  // 获取图层
651
636
  getLayer(layerId: any) {
652
637
  return this.layerList.find((v: any) => v.id === layerId);
653
638
  }
654
639
 
640
+ // 删除图层
655
641
  // 删除图层
656
642
  removeLayer(layerId: any) {
657
643
  const layer = this.getLayer(layerId);
658
644
  if (layer) {
645
+ const entity = layer.layerEntity;
646
+ if (entity && this.map) {
647
+ if (entity instanceof Cesium.DataSource) {
648
+ if (
649
+ this.map.dataSources &&
650
+ typeof this.map.dataSources.remove === "function"
651
+ ) {
652
+ this.map.dataSources.remove(entity);
653
+ }
654
+ } else if (
655
+ entity instanceof Cesium.PrimitiveCollection ||
656
+ entity instanceof Cesium.Primitive
657
+ ) {
658
+ this.map.scene.primitives.remove(entity);
659
+ } else if (entity instanceof Cesium.Entity) {
660
+ this.map.entities.remove(entity);
661
+ } else if (Array.isArray(entity)) {
662
+ entity.forEach((item) => {
663
+ if (item instanceof Cesium.Entity) {
664
+ this.map.entities.remove(item);
665
+ }
666
+ });
667
+ }
668
+ }
659
669
  this.layerList = this.layerList.filter((v: any) => v.id !== layerId);
660
- layer.destroy();
670
+ layer.destroy?.();
661
671
  }
662
672
  }
663
673