qinbo
2021-12-23 d359d32ab88a2aace4f9b0e8e2b2dc5e692cd8a9
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(function (window, mars3d) {
  //创建widget类,需要继承BaseWidget
  class MyWidget extends mars3d.widget.BaseWidget {
    //外部资源配置
    get resources() {
      return ["view.css"];
    }
 
    //弹窗配置
    get view() {
      return {
        type: "append",
        url: "view.html",
        parent: "body",
      };
    }
    getTileLayers() {
      if (!this._alllayers) {
        let tilelayers = this.map.getTileLayers();
 
        this._alllayers = [];
        for (let i = 0; i < tilelayers.length; i++) {
          let layer = tilelayers[i];
          if (layer.noLayerManage || layer.parent || layer.options.mapSplit === false) {
            continue;
          }
 
          let options = layer.toJSON();
          options.noLayerManage = true;
 
          delete options.id;
          delete options.uuid;
          delete options.pid;
          delete options.show;
 
          let splitTileLayer = mars3d.LayerUtil.create(options, this.map.options.templateValues);
          splitTileLayer._oldLayer = layer;
          this._alllayers.push(splitTileLayer);
        }
 
        console.log("卷帘图层=>", this._alllayers);
      }
      return this._alllayers;
    }
    //每个窗口创建完成后调用
    winCreateOK(html) {
      var that = this;
 
      var inhtmlBaseLayer = "";
      var inhtmlSwipelayer = "";
      var arrLayers = this.getTileLayers();
 
      for (var i = 0; i < arrLayers.length; i++) {
        var layer = arrLayers[i];
        if (i == 1) {
          inhtmlBaseLayer += ' <option value="' + i + '" selected>' + layer.name + "</option>"; //selected的默认值
        } else {
          inhtmlBaseLayer += ' <option value="' + i + '">' + layer.name + "</option>";
        }
 
        inhtmlSwipelayer += ' <option value="' + i + '">' + layer.name + "</a></option>";
      }
      $("#ddl_basemap").html(inhtmlBaseLayer);
      $("#ddl_swipelayer").html(inhtmlSwipelayer);
 
      $("#btn_mapSwipe_close").click(function () {
        that.disableBase();
      });
    }
    //激活插件
    activate() {
      $(".toolBarRight").css({ top: "13px" });
 
      this._lastBasemap = this.map.basemap.options;
      this.map.basemap = null;
      this._lastShowArr = [];
 
      this.mapSplit = new mars3d.control.MapSplit({});
      this.map.addControl(this.mapSplit);
 
      this.changeLeftLayer(1);
      this.changeRightLayer(0);
    }
    //释放插件
    disable() {
      $(".toolBarRight").css({ top: "10px" });
      $("#slider").remove();
 
      this.mapSplit.leftLayer = null;
      this.mapSplit.rightLayer = null;
 
      for (let i = 0; i < this._lastShowArr.length; i++) {
        let layer = this._lastShowArr[i];
        layer.show = true;
      }
      this._lastShowArr = [];
 
      this.map.removeControl(this.mapSplit, true);
      this.mapSplit = null;
 
      this.map.basemap = this._lastBasemap;
      this._lastBasemap = null;
      this._alllayers = null;
    }
 
    //view界面控制
    changeLeftLayer(id, index) {
      if (this._last_swipeLayer_id == id) {
        toastr.warning("图层对比不能为同一图层!");
        // 使selected的值不变
        if (this.baseOldIndex) {
          $("#ddl_basemap").get(0).selectedIndex = this.baseOldIndex;
        } else {
          $("#ddl_basemap").get(0).selectedIndex = 1;
        }
        return;
      }
      this.baseOldIndex = index;
      this._last_baselayer_id = id;
 
      var arrLayers = this.getTileLayers();
      var thisLayer = arrLayers[id];
 
      let oldLayer = thisLayer._oldLayer;
      if (oldLayer.isAdded && oldLayer.show) {
        oldLayer.show = false;
        this._lastShowArr.push(oldLayer);
      }
 
      this.mapSplit.leftLayer = thisLayer;
    }
    changeRightLayer(id, index) {
      if (this._last_baselayer_id == id) {
        toastr.warning("图层对比不能为同一图层!");
        if (this.swipeOldIndex) {
          $("#ddl_swipelayer").get(0).selectedIndex = this.swipeOldIndex;
        } else {
          $("#ddl_swipelayer").get(0).selectedIndex = 0;
        }
        return;
      }
      this.swipeOldIndex = index;
      this._last_swipeLayer_id = id;
 
      var arrLayers = this.getTileLayers();
      var thisLayer = arrLayers[id];
 
      let oldLayer = thisLayer._oldLayer;
      if (oldLayer.isAdded && oldLayer.show) {
        oldLayer.show = false;
        this._lastShowArr.push(oldLayer);
      }
 
      this.mapSplit.rightLayer = thisLayer;
    }
  }
 
  //注册到widget管理器中。
  window.mapSwipeWidget = mars3d.widget.bindClass(MyWidget);
 
  //每个widet之间都是直接引入到index.html中,会存在彼此命名冲突,所以闭包处理下。
})(window, mars3d);