无法注销openlayers中的点击事件 [英] can't unregister click event in openlayers

查看:1167
本文介绍了无法注销openlayers中的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有OpenLayers的问题,并注销添加到图层的点击事件。基本上,我需要做的是这样的:

I am having issues with OpenLayers and unregistering the click events that are added to a layer. Basically, what I need to do is this:

当用户点击标记时,他们会得到一个具有编辑链接的泡沫。用户点击该图标,并在地图上创建一个新图层,然后在地图上注册一个点击事件,等待用户点击地图。当他们点击地图上的某个地方时,它会将标记移动到他们点击的位置。这一切都很完美。

When a user clicks on a marker, they get a bubble that has an "edit" link in it. The user clicks that and it creates a new layer on the map and then registers a click event to the map waiting for the user to click on the map. When they click somewhere on the map, it then moves the marker to where they clicked. This all works perfectly.

但是,问题是当用户点击编辑标记,然后点击地图上的按钮取消动作,不移动标记,点击事件的注销不起作用。他们仍然可以点击地图并移动标记。

However, the issue is when the user clicks to edit the marker and then clicks on a button OUTSIDE OF THE MAP to cancel the action and NOT move the marker, the unregister of the click event doesn't work. They can still click on the map and it moves the marker.

以下是代码示例:

function move_marker(marker) {
    lmLayer = new OpenLayers.Layer.Markers("Landmark Creation",{displayInLayerSwitcher: false});
    map.addLayer(lmLayer);
    map.events.register("click", lmLayer, function(evt){
        var pixel = new OpenLayers.Pixel(evt.clientX,evt.clientY);
        position = map.getLonLatFromPixel(pixel);
        marker.lonlat = pixel;
        marker.moveTo(pixel);
        marker.draw();
        lmLayer.redraw();
        OpenLayers.Event.stop(evt);
    });
}
function cancel_move() {  // this function is triggered by a button outside of the map element
    lmLayer = map.getLayersByName('Landmark Creation');
    lmLayer[0].events.unregister("click");
    map.events.unregister("click");
    map.removeLayer(lmLayer[0]);
}

正如你在cancel函数中可以看到的那样,层名称,根据console.log,它在索引0中找到。我添加了注册到lmLayer,希望这将有所帮助,但到目前为止,没有运气。然后在地图元素上添加注销呼叫,然后最后删除那个新的层,因为我们不想干扰它。

As you can see in the cancel function, I am getting the layer by the layer name, which according to the console.log it is finding at index 0. I added the unregister to the lmLayer in hopes that would help, but so far, no luck. Then on the map element I add the unregister call and then finally I remove that new layer because we don't want it interfering.

。我失去了我的想法。

I'd appreciate some feedback on this. I'm losing my mind.

谢谢!

推荐答案

认为您需要告知您要取消注册的 事件的OpenLayers

I think you need to tell OpenLayers which click event you want it to unregister:

var method = function() {
    // Do stuff...
}

map.events.register('click', map, method);
map.events.unregister('click', map, method);

根据OpenLayers.Events来源,它检查范围方法存在于侦听器堆栈中:

According to the OpenLayers.Events source it checks whether the scope and the method is present in the listener stack:

unregister: function (type, obj, func) {
    if (obj == null)  {
        obj = this.object;
    }
    var listeners = this.listeners[type];
    if (listeners != null) {
        for (var i=0, len=listeners.length; i<len; i++) {
   HERE --> if (listeners[i].obj == obj && listeners[i].func == func) { <-- HERE
                listeners.splice(i, 1);
                break;
            }
        }
    }
},

我希望对你有用:)

这篇关于无法注销openlayers中的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆