炫技贴,提供Openlayers对交互工具和命令工具的统一管理思路,直接贴代码,剩下的代码,还请自己补充。

bindClickEvent() {
var __this = this;
for (let index in __this.interactions) {
let toolItem = __this.interactions[index];
this.addInteraction(toolItem);
}
}

addInteraction(toolItem: toolBarInteraction) {
var __this = this;
if (toolItem.type === "tool"){
if (toolItem.instance instanceof Interaction){
__this.map.addInteraction(toolItem.instance);
}
if(toolItem.domObj) {
toolItem.domObj.on("click", {tool: toolItem}, (e)=>{
let currentTool = e.data.tool;
__this.disableTools();
if (currentTool.instance instanceof Interaction){
currentTool.instance.setActive(true);
}
});
}
} else if (toolItem.type === "command" && toolItem.domObj){
toolItem.domObj.on("click", {tool: toolItem}, (e)=>{
let currentTool = e.data.tool;
if (currentTool.instance instanceof baseCommand){
currentTool.instance.execute();
}
});
}
}

使用方法:

const commandManagerObj = new commandManager({
map: map,
toolBarInteractions: [
new toolBarInteraction({ type: "tool", instance: new DragZoom({ out: false, condition: always }), domObj: $("#btn-zoomin") }),
new toolBarInteraction({ type: "tool", instance: new IdentifyTool(), domObj: $("#btn-identify") }),
]
});
commandManagerObj.bindClickEvent();