"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);