Lin_Ye_Fang_Huo/public/widgets/manageLayers/widget.js

121 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-07-18 09:04:15 +08:00
"use script"; //开发环境建议开启严格模式
(function (window, mars3d) {
2023-07-08 15:37:34 +08:00
//创建widget类需要继承BaseWidget
class MyWidget extends mars3d.widget.BaseWidget {
//弹窗配置
get view() {
return {
2023-07-18 09:04:15 +08:00
type: "window",
url: "view.html",
2023-07-08 15:37:34 +08:00
windowOptions: {
width: 250,
height: 500,
},
2023-07-18 09:04:15 +08:00
};
2023-07-08 15:37:34 +08:00
}
//是否可以控制basemaps因为有底图控制了具体项目中可以按需改为false
get hasManagerBaseMaps() {
2023-07-18 09:04:15 +08:00
return true;
2023-07-08 15:37:34 +08:00
}
//初始化[仅执行1次]
create() {}
//每个窗口创建完成后调用
winCreateOK(opt, result) {
2023-07-18 09:04:15 +08:00
this.viewWindow = result;
2023-07-08 15:37:34 +08:00
}
//打开激活
activate() {
//监听事件,联动勾选状态
2023-07-18 09:04:15 +08:00
this.map.on(mars3d.EventType.addLayer, this._onAddLayerHandler, this);
this.map.on(mars3d.EventType.removeLayer, this._onRemoveLayerHandler, this);
2023-07-08 15:37:34 +08:00
}
//关闭释放
disable() {
2023-07-18 09:04:15 +08:00
this.viewWindow = null;
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
this.map.off(mars3d.EventType.addLayer, this._onAddLayerHandler, this);
this.map.off(mars3d.EventType.removeLayer, this._onRemoveLayerHandler, this);
2023-07-08 15:37:34 +08:00
}
_onAddLayerHandler(e) {
2023-07-18 09:04:15 +08:00
if (!this.isActivate || !this.viewWindow) {
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
console.log("添加了图层", e);
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
this.viewWindow.updateNode(e.layer);
2023-07-08 15:37:34 +08:00
}
_onRemoveLayerHandler(e) {
if (!this.isActivate || !this.viewWindow) {
2023-07-18 09:04:15 +08:00
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
console.log("移除了图层", e);
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
this.viewWindow.removeNode(e.layer);
2023-07-08 15:37:34 +08:00
}
getLayers() {
return this.map.getLayers({
basemaps: true, //是否取config.json中的basempas
layers: true, //是否取config.json中的layers
2023-07-18 09:04:15 +08:00
});
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
//对单击的图层做处理(单个)
checkClickLayer(layer, show) {
2023-07-08 15:37:34 +08:00
if (show) {
if (this.config.autoCenter && !layer.options.noCenter) {
//在对应config.json图层节点配置 noCenter:true 可以不定位
2023-07-18 09:04:15 +08:00
layer.flyTo();
2023-07-08 15:37:34 +08:00
}
//存在关联widget时
2023-07-18 09:04:15 +08:00
let item = layer.options;
2023-07-08 15:37:34 +08:00
if (item.onWidget) {
2023-07-18 09:04:15 +08:00
if (this._lastWidget) {
mars3d.widget.disable(this._lastWidget);
this._lastWidget = null;
}
2023-07-08 15:37:34 +08:00
mars3d.widget.activate({
uri: item.onWidget,
layerItem: item,
disableOther: false,
2023-07-18 09:04:15 +08:00
});
this._lastWidget = item.onWidget;
2023-07-08 15:37:34 +08:00
}
} else {
//存在关联widget时
2023-07-18 09:04:15 +08:00
let item = layer.options;
2023-07-08 15:37:34 +08:00
if (item.onWidget) {
2023-07-18 09:04:15 +08:00
mars3d.widget.disable(item.onWidget);
if (this._lastWidget == item.onWidget) {
this._lastWidget = null;
}
2023-07-08 15:37:34 +08:00
}
}
}
2023-07-18 09:04:15 +08:00
//更新图层:显示隐藏状态(勾选后的图层及其子级图层,多个)
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)
// }
}
}
2023-07-08 15:37:34 +08:00
}
//注册到widget管理器中。
2023-07-18 09:04:15 +08:00
mars3d.widget.bindClass(MyWidget);
2023-07-08 15:37:34 +08:00
//每个widet之间都是直接引入到index.html中会存在彼此命名冲突所以闭包处理下。
2023-07-18 09:04:15 +08:00
})(window, mars3d);