Javascript OL3删除dragbox交互
我想删除我在js类中添加的映射的一个交互,但是当我尝试在其他类中删除该交互时,我不知道如何做,因为我无法访问我需要的类中的交互对象 我有以下代码(我添加了交互): 要删除交互的js类的名称:Javascript OL3删除dragbox交互,javascript,extjs,openlayers-3,Javascript,Extjs,Openlayers 3,我想删除我在js类中添加的映射的一个交互,但是当我尝试在其他类中删除该交互时,我不知道如何做,因为我无法访问我需要的类中的交互对象 我有以下代码(我添加了交互): 要删除交互的js类的名称: ... var dragBox = new ol.interaction.DragBox({ condition: ol.events.condition.always, style: new ol.style.Style({ stroke: new
...
var dragBox = new ol.interaction.DragBox({
condition: ol.events.condition.always,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: [0, 0, 255, 1]
})
})
});
mapa.addInteraction(dragBox);
dragBox.on('boxend', function(e) {
if (capaActiva != null) {
var info = [];
var extent = dragBox.getGeometry().getExtent();
var ext = extent.toString().split(',');
var ext1 = ext[0];
var ext2 = ext[1];
var ext3 = ext[2];
var ext4 = ext[3];
var tfn = ol.proj.getTransform('EPSG:3857', 'EPSG:4326');
var textent = ol.extent.applyTransform([ext1, ext2, ext3, ext4], tfn).toString();
...
...
cerrar: function(){
mapa.removeInteraction(dragBox);
}
...
在第二类中,我得到了错误:
dragBox is not defined
如何访问和删除第二个类中的交互?需要在两个作用域中都可以访问dragBox 如果看不到更多的代码,很难说,但你可以做到
this.dragBox = new ol.interaction.DragBox({
condition: ol.events.condition.always,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: [0, 0, 255, 1]
})
})
});
mapa.addInteraction(this.dragBox);
this.dragBox.on('boxend', function(e) { ......
.......................
cerrar: function(){
mapa.removeInteraction(dragBox);
}
如果它们不在同一个对象中,则需要确保可以从两个作用域访问dragbox变量是的,谢谢,但我想在我关闭应用程序中的窗口时删除交互,因此我无法将变量放入此类,我需要window类中的变量,我该怎么做?你可能想考虑你的应用程序是如何构造的,因为一般的全局是非常糟糕的。但如果您使用window.dragBox=。。。。这将使其在全球范围内可用