zhaoxing
2022-10-14 366af23329c26d69d1f644e6a32fcb02e87910e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"use script"; //开发环境建议开启严格模式
(function (window, mars3d) {
  //创建widget类,需要继承BaseWidget
  class MyWidget extends mars3d.widget.BaseWidget {
    //弹窗配置
    get view() {
      return {
        type: "window",
        url: "view.html",
        windowOptions: {
          // noTitle: true,
          // closeBtn: 0,
          width: 250,
          height: 600,
        },
      };
    }
    //是否可以控制basemaps,因为有底图控制了,具体项目中可以按需改为false
    get hasManagerBaseMaps() {
      return true;
    }
 
    //初始化[仅执行1次]
    create() {}
    //每个窗口创建完成后调用
    winCreateOK(opt, result) {
      this.viewWindow = result;
    }
    //打开激活
    activate() {
      //监听事件,联动勾选状态
      this.map.on(mars3d.EventType.addLayer, this._onAddLayerHandler, this);
      // this.map.on(mars3d.EventType.removeLayer, this._onRemoveLayerHandler, this);
    }
    //关闭释放
    disable() {
      this.viewWindow = null;
 
      this.map.off(mars3d.EventType.addLayer, this._onAddLayerHandler, this);
      // this.map.off(mars3d.EventType.removeLayer, this._onRemoveLayerHandler, this);
    }
    _onAddLayerHandler(e) {
      if (!this.isActivate || !this.viewWindow) {
        return;
      }
      console.log("添加了图层", e);
 
      this.viewWindow.updateNode(e.layer);
    }
    _onRemoveLayerHandler(e) {
      if (!this.isActivate || !this.viewWindow) {
        return;
      }
      console.log("移除了图层", e);
 
      this.viewWindow.removeNode(e.layer);
    }
 
    getLayers() {
      return this.map.getLayers({
        basemaps: true, //是否取config.json中的basempas
        layers: true, //是否取config.json中的layers
      });
    }
    //对单击的图层做处理(单个)
    checkClickLayer(layer, show) {
      if (show) {
        if (this.config.autoCenter && !layer.options.noCenter) {
          //在对应config.json图层节点配置 noCenter:true 可以不定位
          layer.flyTo();
        }
 
        //存在关联widget时
        let item = layer.options;
        if (item.onWidget) {
          if (this._lastWidget) {
            mars3d.widget.disable(this._lastWidget);
            this._lastWidget = null;
          }
 
          mars3d.widget.activate({
            uri: item.onWidget,
            layerItem: item,
            disableOther: false,
            autoDisable:false
          });
          this._lastWidget = item.onWidget;
        }
      } else {
        //存在关联widget时
        let item = layer.options;
        if (item.onWidget) {
          mars3d.widget.disable(item.onWidget);
          if (this._lastWidget == item.onWidget) {
            this._lastWidget = null;
          }
        }
      }
    }
 
    //更新图层:显示隐藏状态(勾选后的图层及其子级图层,多个)
    updateLayerShow(layer, show) {
      layer.show = show;
 
      if (show) {
        if (!layer.isAdded) {
          this.map.off(mars3d.EventType.addLayer, this._onAddLayerHandler, this);
          this.map.addLayer(layer);
          this.map.on(mars3d.EventType.addLayer, this._onAddLayerHandler, this);
        }
      } else {
        // if (layer.options.type=="wms" && layer.isAdded) {
        //   this.map.removeLayer(layer)
        // }
      }
    }
  }
 
  //注册到widget管理器中。
  mars3d.widget.bindClass(MyWidget);
 
  //每个widet之间都是直接引入到index.html中,会存在彼此命名冲突,所以闭包处理下。
})(window, mars3d);