javascript - OL3 删除拖动框交互

标签 javascript extjs openlayers-3

我想删除我在 js 类中添加的 map 的交互,但是当我尝试删除其他类中的该交互时,我不知道该怎么做,因为我无法访问交互对象在我需要的类(class)中......

我有这段代码(我添加了交互):

...
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();
...

删除交互的 js 类:

...
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);
}

如果它们不在同一个对象中,那么您需要确保可以从两个作用域访问拖放框变量

关于javascript - OL3 删除拖动框交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31194831/

相关文章:

javascript - Ajax 输入问题,文本区域模糊

javascript - 我怎样才能改变 extjs 中可 float 形式的位置

angularjs - OpenLayers v3.5.0 map ,使用 bbox 策略从 GeoJSON 加载特征

javascript - ExtJS 相当于 jQuery 的 $ (".className",父)

javascript - Openlayers 3 : how to select a feature programmatically using ol. 交互。选择?

javascript - 从打开的图层中删除图层不起作用

javascript - IIFE 如何保护命名空间?

javascript - 如何在html div标签上显示person对象的属性?

javascript - Angular 应用程序未将页面加载到 View 中并自行重新启动

Extjs 6 使用 Ext.define 定义一个链式商店