OpenLayers 3: remove event listener

In Openlayers 3, how to remove an event listener attached as follows:

var a = map.on("pointerdrag",function (e) { // event handler }); var b = map.on("pointerdrag",function (e) { // event handler }); 

How to remove only listner a and keep b active?

+8
javascript openlayers-3
source share
2 answers

And it's pretty simple! Its in the Docs API : unByKey , but a very inconsistent name for the off function.

So, to remove the event listener a :

 map.unByKey(a); 

Remove a listener, but turn on b .

Note. This will work through any object in Open Layers 3 that emits an event. like layers, interactions, etc.

+11
source share

I was just wondering the same thing, it seems that off would be the appropriate method to remove the event listener. You can also call this directly in the event callback:

 map.on("pointerdrag", function (e) { map.unByKey(e); }); 

This should remove only this particular event.

+1
source share

All Articles