简体   繁体   English

如何在 OpenLayers.Map 上获取`mousedown` 事件?

[英]How to get `mousedown` event on OpenLayers.Map?

I'm using OpenLayers 2.13.我正在使用 OpenLayers 2.13。 I want to detect mousedown , mousemove , mouseup events while mouse is over OpenLayers.Map , so I wrote the following code.我想在鼠标悬停在OpenLayers.Map上时检测mousedownmousemovemouseup事件,所以我编写了以下代码。

var map = new OpenLayers.Map("map",{controls:[
    new OpenLayers.Control.Navigation(),
    new OpenLayers.Control.ArgParser(),
    new OpenLayers.Control.Attribution()
]});
var events = map.events;
events.register("mousedown",map,function(e){
    console.log("mousedown");
});
events.register("mousemove",map,function(e){
    console.log("mousemove");
});
events.register("mouseup",map,function(e){
    console.log("mouseup");
});

As a result, mousemove and mouseup is detected but no mousedown s.结果,检测到mousemovemouseup但没有检测到mousedown

It says here that mousemove and mouseup is supported but mousedown is not.在这里说支持mousemovemouseup但不支持mousedown Is there any hacks I can apply to detect mousedown events without modifying OpenLayers script?是否有任何技巧可以在不修改 OpenLayers 脚本的情况下应用来检测mousedown事件?

Add the 4th argument as true .将第四个参数添加为true

var events = map.events;
events.register("mousedown",map,function(e){
    console.log("mousedown");
    return true;
},true); // This argument is new

There are several event listeners already listening the mousedown event.有几个事件侦听器已经在侦听mousedown事件。 One of them will eat the event when [map drag start] is detected, so the mousedown event will never reach the last listener.其中一个会在检测到 [map drag start] 时吃掉该事件,因此mousedown事件永远不会到达最后一个侦听器。

Without the 4th argument, events.register() will add the listener to the end of the event-listening chain.如果没有第四个参数, events.register()会将监听器添加到事件监听链的末尾。 With the 4th argument, it will add it to the first.使用第四个参数,它将添加到第一个参数。

There is also a registerPriority() which adds to the front of the list:还有一个 registerPriority() 添加到列表的前面:

http://dev.openlayers.org/docs/files/OpenLayers/Events-js.html#OpenLayers.Events.registerPriority http://dev.openlayers.org/docs/files/OpenLayers/Events-js.html#OpenLayers.Events.registerPriority

if you using open layer API > 5 then us can use below code如果您使用开放层 API > 5 那么我们可以使用以下代码

 map.on('pointerdown', function(){
      console.log("mousedown");

    });

Simple way to make it:简单的制作方法:

map.events.listeners.mousedown.unshift({ 
    func: function(){
        // code
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM