wangrong
2025-09-23 40634ebaf5863a599bba8ca2c0575ca079fb8e15
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
124
125
126
127
128
129
130
131
132
133
"use script" //开发环境建议开启严格模式
;(function (window, mars3d) {
  //创建widget类,需要继承BaseWidget
  class MyWidget extends es5widget.BaseWidget {
    //弹窗配置
    get view() {
      return {
        type: "window",
        url: "view.html",
        windowOptions: {
          // noTitle: true,
          // closeBtn: 0,
          width: 250,
          height: 500
        }
      }
    }
 
    //初始化[仅执行1次]
    create() {
      //演示,监听事件
      // es5widget.on("checkLayer", (event) => {
      //   if (!this.isActivate || !this.viewWindow) {
      //     return;
      //   }
      //   var layer = event.layer;
      //   this.viewWindow.updateNode(layer);
      // });
    }
    //每个窗口创建完成后调用
    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  ,因为有底图控制了,具体项目中可以按需改为false
        layers: true //是否取config.json中的layers
      })
    }
    //对单击的图层做处理(单个)
    checkClickLayer(layer, show) {
      if (show) {
        if (this.config.autoCenter && !layer.options.noCenter) {
          //在对应config.json图层节点配置 noCenter:true 可以不定位
          layer.readyPromise.then(function (layer) {
            layer.flyTo()
          })
        }
 
        //存在关联widget时
        let item = layer.options
        if (item.onWidget) {
          if (this._lastWidget) {
            es5widget.disable(this._lastWidget)
            this._lastWidget = null
          }
 
          es5widget.activate({
            uri: item.onWidget,
            layerItem: item,
            disableOther: false
          })
          this._lastWidget = item.onWidget
        }
      } else {
        if (this.config.autoCenter && !layer.options.noCenter) {
          this.map.cancelFlight()
        }
 
        //存在关联widget时
        let item = layer.options
        if (item.onWidget) {
          es5widget.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.isAdded) {
        //   this.map.removeLayer(layer)
        // }
      }
    }
  }
 
  //注册到widget管理器中。
  es5widget.bindClass(MyWidget)
 
  //每个widet之间都是直接引入到index.html中,会存在彼此命名冲突,所以闭包处理下。
})(window, mars3d)