wangzhibo
2021-09-06 2efb5d2a02ce47a9fd6f1cec138496aba72f0815
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
(function (window, mars3d) {
  //创建widget类,需要继承BaseWidget
  class MyWidget extends mars3d.widget.BaseWidget {
    //外部资源配置
    get resources() {
      return ["map.css"];
    }
 
    //弹窗配置
    get view() {
      return {
        type: "window",
        url: "view.html",
        windowOptions: {
          width: 300,
          height: 400,
        },
      };
    }
    //初始化[仅执行1次]
    create() {
      this.storageName = "mars3d_addmarker";
      this.editable = true;
 
      this.graphicLayer = new mars3d.layer.GraphicLayer({
        name: this.config.name,
        pid: 99, //图层管理 中使用,父节点id
        hasEdit: false,
      });
      this.map.addLayer(this.graphicLayer);
 
      this.graphicLayer.bindPopup((event) => {
        let graphic = event.graphic;
        if (!graphic) {
          return;
        }
 
        var html = mars3d.Util.getTemplateHtml({
          title: "我的标记",
          template: [
            { field: "name", name: "名称" },
            { field: "remark", name: "备注", type: "textarea" },
            this.editable ? { field: "id", name: "确定", type: "button" } : null,
          ],
          attr: {
            id: graphic.id,
            ...graphic.attr,
          },
          edit: this.editable,
          width: 190,
        });
        return html;
      });
 
      this.graphicLayer.on(mars3d.EventType.popupOpen, (event) => {
        let container = event.container;
        let btnOk = container.querySelector(".mars3d-popup-btn");
        if (btnOk) {
          let graphic = event.graphic;
          btnOk.addEventListener("click", (e) => {
            $(".mars3d-popup-edititem").each(function () {
              var val = $(this).val();
              var key = $(this).attr("data-type");
              graphic.attr[key] = val;
            });
            graphic.label.text = graphic.attr.name;
            this.saveEditFeature(graphic);
          });
        }
      });
      //事件监听
      this.graphicLayer.on(mars3d.EventType.drawCreated, (e) => {
        var graphic = e.graphic;
        if (!graphic || !graphic.positionShow) {
          return;
        }
        graphic.attr.name = "我的标记";
 
        this.saveEntity(graphic, () => {
          this.graphicLayer.openPopup(graphic);
        });
      });
 
      this.graphicLayer.on(mars3d.EventType.editMovePoint, (e) => {
        this.saveEntity(e.graphic); //编辑修改了点
      });
      this.graphicLayer.on(mars3d.EventType.removeGraphic, (e) => {
        this.deleteItem(e.graphic.id); //保存数据
      });
 
      this.getList();
    }
    //每个窗口创建完成后调用
    winCreateOK(opt, result) {
      this.viewWindow = result;
    }
    //激活插件
    activate() {
      this.hasEdit(true);
    }
    //释放插件
    disable() {
      this.viewWindow = null;
      this.stopDraw();
      this.hasEdit(false);
    }
    stopDraw() {
      this.graphicLayer.stopDraw();
    }
    //配置的样式
    getStyle() {
      return {
        scale: 1,
        image: this.path + "img/mark1.png",
        horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        scaleByDistance: new Cesium.NearFarScalar(1.5e2, 1, 8.0e6, 0.2),
        highlight: {
          type: mars3d.EventType.click,
          image: this.path + "img/mark2.png",
        },
        label: {
          text: "{name}",
          font_size: 20,
          outline: true,
          outlineColor: Cesium.Color.BLACK,
          outlineWidth: 3,
          horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          pixelOffset: new Cesium.Cartesian2(0, -50),
          distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000),
        },
      };
    }
    drawPoint() {
      this.graphicLayer.startDraw({
        type: "billboard",
        style: this.getStyle(),
      });
    }
    hasEdit(val) {
      this.editable = val;
      this.graphicLayer.hasEdit = val;
    }
    //========================
    saveEditFeature(graphic) {
      this.graphicLayer.closePopup();
 
      this.saveEntity(graphic);
    }
    deleteItemById(id) {
      let graphic = this.graphicLayer.getGraphicById(id);
      if (graphic) {
        graphic.remove();
      }
      this.deleteItem(id);
    }
    getGraphicAttrList() {
      var arrAttr = [];
      var arrGraphic = this.graphicLayer.getGraphics();
      for (var i = 0, len = arrGraphic.length; i < len; i++) {
        let graphic = arrGraphic[i];
        let attr = graphic.attr;
        if (!attr.name) {
          attr.name = "我的标记";
        }
        arrAttr.push({
          id: graphic.id,
          ...attr,
        });
      }
      return arrAttr;
    }
    flyTo(id) {
      let graphic = this.graphicLayer.getGraphicById(id);
      if (graphic) {
        //参数解释:点数据:radius控制视距距离
        this.map.flyToGraphic(graphic, { radius: 2500 });
      }
    }
    getJsonData() {
      var arr = [];
      var graphics = this.graphicLayer.getGraphics();
      for (var i = 0, len = graphics.length; i < len; i++) {
        var graphic = graphics[i];
        var attr = graphic.attr;
        var point = graphic.point.format();
 
        arr.push({
          id: graphic.id,
          name: attr.name,
          remark: attr.remark,
          lat: point.lat,
          lng: point.lng,
          alt: point.alt,
        });
      }
      return arr;
    }
 
    loadJson(arr, isclear) {
      if (arr == null || arr.length == 0) {
        return;
      }
      if (isclear) {
        this.graphicLayer.clear();
      }
 
      var arrGraphic = [];
      for (var i = 0; i < arr.length; i++) {
        var item = arr[i];
        if (!item.lat || !item.lng) {
          continue;
        }
 
        if (!isclear) {
          //叠加时,清除已有同id数据
          let graphic = this.graphicLayer.getGraphicById(item.id);
          this.graphicLayer.removeGraphic(graphic);
        }
 
        var graphic = new mars3d.graphic.BillboardEntity({
          id: item.id,
          position: [item.lng, item.lat, item.alt],
          style: this.getStyle(),
          attr: {
            name: item.name || "我的标记",
            remark: item.remark || "",
          },
        });
        this.graphicLayer.addGraphic(graphic);
        arrGraphic.push(graphic);
      }
 
      this.map.flyToGraphic(arrGraphic, { duration: 2.0, scale: 2 });
 
      if (this.viewWindow) {
        this.viewWindow.refMarkerList();
      }
 
      var storagedata = this.getJsonData();
      localforage.setItem(this.storageName, storagedata);
    }
    getList() {
      if (window.hasServer) {
        //读取服务端存储
        // window.sendAjax({
        //   url: 'map/mark/list',
        //   type: 'get',
        //   success: (arr) => {
        //     this.loadJson(arr, true)
        //   },
        // })
      } else {
        //读取本地存储
        localforage.getItem(this.storageName).then((laststorage) => {
          if (!laststorage) {
            return;
          }
          this.loadJson(laststorage, true);
        });
      }
    }
    saveEntity(graphic, endfun) {
      if (window.hasServer) {
        //服务端存储
        // var attr = graphic.attr
        // var point = graphic.point
        // window.sendAjax({
        //   url: 'map/mark/update',
        //   data: JSON.stringify({
        //     id: graphic.id,
        //     name: attr.name,
        //     remark: attr.remark,
        //     lng: point.lng,
        //     lat: point.lat,
        //     alt: point.alt,
        //   }),
        //   type: 'post',
        //   contentType: 'application/json',
        //   success: (data) => {
        //     if (endfun) {
        //       endfun()
        //     }
        //     this.viewWindow.refMarkerList()
        //   },
        // })
      } else {
        //浏览器本地存储
        var storagedata = this.getJsonData();
        localforage.setItem(this.storageName, storagedata);
 
        if (endfun) {
          endfun();
        }
        this.viewWindow.refMarkerList();
      }
    }
 
    deleteItem(id) {
      this.graphicLayer.closePopup();
      this.viewWindow.refMarkerList();
 
      //服务端存储
      if (window.hasServer) {
        // window.sendAjax({
        //   url: 'map/mark/' + id,
        //   type: 'delete',
        //   success: function (data) {},
        // })
      } else {
        //浏览器本地存储
        var storagedata = this.getJsonData();
        localforage.setItem(this.storageName, storagedata);
      }
    }
    deleteAll() {
      this.graphicLayer.clear();
 
      if (this.viewWindow) {
        this.viewWindow.refMarkerList();
      }
 
      if (window.hasServer && window.userId) {
        //服务端存储
        // window.sendAjax({
        //   url: 'map/mark/del/' + window.userId,
        //   type: 'delete',
        //   dataType: 'json',
        //   success: function (data) {},
        // })
      } else {
        //浏览器本地存储
        localforage.removeItem(this.storageName);
      }
    }
  }
 
  //注册到widget管理器中。
  mars3d.widget.bindClass(MyWidget);
 
  //每个widet之间都是直接引入到index.html中,会存在彼此命名冲突,所以闭包处理下。
})(window, mars3d);