qinbo
2021-11-02 4a1aaa1eb44bac3dc66c34187ca312c19d2ddf85
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use script"; //开发环境建议开启严格模式
(function (window, mars3d) {
  //创建widget类,需要继承BaseWidget
  class MyWidget extends mars3d.widget.BaseWidget {
    //弹窗配置
    get view() {
      return {
        type: "window",
        url: "view.html",
        windowOptions: {
          width: 350,
          position: {
            top: 45,
            bottom: 30,
            right: 5,
          },
        },
      };
    }
 
    //初始化[仅执行1次]
    create() {
      this.drawLayer = new mars3d.layer.GraphicLayer({
        hasEdit: true,
      });
      this.drawLayer.on(mars3d.EventType.drawCreated, (e) => {
        this.updateBufferForEdit(e.graphic);
      });
      this.drawLayer.on(mars3d.EventType.editMovePoint, (e) => {
        this.updateBufferForEdit(e.graphic);
      });
 
      this.bufferLayer = new mars3d.layer.GraphicLayer();
    }
    //每个窗口创建完成后调用
    winCreateOK(opt, result) {
      this.viewWindow = result;
    }
    //打开激活
    activate() {
      this.map.addLayer(this.bufferLayer);
      this.map.addLayer(this.drawLayer);
    }
    //关闭释放
    disable() {
      this.clearDraw();
      this.clearShowFeature();
 
      this.map.removeLayer(this.drawLayer);
      this.map.removeLayer(this.bufferLayer);
      this.viewWindow = null;
    }
    clearDraw() {
      this.clearBuffer();
      this.geojsonEntity = null;
      this.drawLayer.clear();
    }
    clearBuffer() {
      this.lastgeojson = null;
      this.bufferLayer.clear();
    }
    updateBufferForRadius() {
      if (!this.geojsonEntity) {
        return;
      }
 
      this.updateBuffer(this.geojsonEntity);
    }
    updateBufferForEdit(graphic) {
      this.clearBuffer();
 
      if (!this.viewWindow.hasBuffer()) {
        return;
      }
 
      this.geojsonEntity = graphic.toGeoJSON();
      this.updateBuffer(this.geojsonEntity);
    }
    updateBuffer(geojson) {
      this.clearBuffer();
 
      var buffere;
      try {
        var bufferRadius = this.viewWindow.getBufferRadius();
        buffere = mars3d.PolyUtil.buffer(geojson, bufferRadius);
      } catch (e) {
        console.log(e);
      }
      if (!buffere) {
        return;
      }
 
      this.lastgeojson = buffere;
 
      let graphicsOptions = mars3d.Util.geoJsonToGraphics(buffere, {
        type: "polygon",
        style: {
          color: "rgba(255,0,0,0.4)",
          clampToGround: true,
        },
      });
      this.bufferLayer.addGraphic(graphicsOptions);
    }
 
    hasDraw() {
      return this.drawLayer.length > 0;
    }
    //查询后全部显示处理
    clearShowFeature() {
      if (!this.viewWindow) {
        return;
      }
      for (var i = this.viewWindow.layersCfg.length - 1; i >= 0; i--) {
        var item = this.viewWindow.layersCfg[i];
        if (item.queryArcServer) {
          item.queryArcServer.clear();
        }
        item.result = [];
      }
    }
    getExtentByType(extenttype) {
      if (extenttype == "1") {
        //当前视域内
        return this.map.getExtent();
      } else if (extenttype == "2") {
        //绘制的区域
        this.drawLayer.stopDraw();
        return this.drawLayer.getGraphics()[0];
      } else if (extenttype == "3") {
        //缓冲区内
        this.drawLayer.stopDraw();
        return this.lastgeojson;
      }
      return null;
    }
    query(param) {
      this.clearShowFeature();
 
      let layerCfg = param.layer;
 
      if (!layerCfg.queryArcServer) {
        layerCfg.queryArcServer = new mars3d.query.QueryArcServer({
          url: layerCfg.url,
          symbol: layerCfg.symbol,
          buildings: layerCfg.buildings,
          popup: layerCfg.columns || "all",
          name: this.config.name,
          pid: 99, //图层管理 中使用,父节点id
        });
        this.map.addLayer(layerCfg.queryArcServer.layer);
      }
 
      console.log(param.graphic);
 
      haoutil.loading.show(); //loading
 
      layerCfg.queryArcServer.query({
        where: param.where,
        graphic: param.graphic,
        page: false, //不分页
        success: (result) => {
          haoutil.loading.hide();
          if (result.count == 0) {
            console.log("未查询到 " + layerCfg.name + " 相关记录!");
            this.viewWindow.updateAllCount(0, layerCfg);
          } else {
            for (var i = 0; i < result.list.length; i++) {
              const item = result.list[i];
 
              item["长度"] = mars3d.Util.formatNum(item.graphic.distance, 2);
              item["面积"] = mars3d.Util.formatNum(item.graphic.area, 2);
 
              item.graphic.attr["长度"] = item["长度"];
              item.graphic.attr["面积"] = item["面积"];
            }
            layerCfg.result = result.list;
            this.viewWindow.updateAllCount(result.list.length, layerCfg);
          }
        },
        error: (error, msg) => {
          haoutil.loading.hide();
          console.log("服务访问错误", error);
          haoutil.alert(msg, "服务访问错误");
        },
      });
    }
 
    flyTo(item) {
      var graphic = item.graphic;
      if (!graphic) {
        return;
      }
 
      //参数解释:线面数据:scale控制边界的放大比例,点数据:radius控制视距距离
      graphic.flyTo({ radius: 1000 });
      // this.highlight(graphic);
    }
  }
 
  //注册到widget管理器中。
  mars3d.widget.bindClass(MyWidget);
 
  //每个widet之间都是直接引入到index.html中,会存在彼此命名冲突,所以闭包处理下。
})(window, mars3d);