Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get `mousedown` event on OpenLayers.Map?

I'm using OpenLayers 2.13. I want to detect mousedown, mousemove, mouseup events while mouse is over OpenLayers.Map, so I wrote the following code.

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 mousedowns.

It says here that mousemove and mouseup is supported but mousedown is not. Is there any hacks I can apply to detect mousedown events without modifying OpenLayers script?

like image 824
Izumi Kawashima Avatar asked Jan 13 '23 01:01

Izumi Kawashima


1 Answers

Add the 4th argument as 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. One of them will eat the event when [map drag start] is detected, so the mousedown event will never reach the last listener.

Without the 4th argument, events.register() will add the listener to the end of the event-listening chain. With the 4th argument, it will add it to the first.

like image 139
Izumi Kawashima Avatar answered Jan 16 '23 02:01

Izumi Kawashima