5

I am trying to suppress the single click event that adds popups to the map. I have the following function:

function addFullScreenPopups(){
      map.on('singleclick', function(evt) {
        var popup = new ol.Overlay.Popup({insertFirst: false});
        map.addOverlay(popup);
        var prettyCoord = ol.coordinate.toStringHDMS(ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326'), 2);
        console.log("Pretty Coordinates : "+prettyCoord);
        popup.show(evt.coordinate, '<div><h2>Coordinates</h2><p>' + prettyCoord + '</p></div>');
    });
    }

I used to use unByKey to unlisten the single click event. However, the developers removed this method in OpenLayers 4. Therefore, I am bound to use map.un method. I am trying to use this method, but it is not doing anything. I am doing map.un("singleclick", addFullScreenPopups()); for unlistening this event, but it is not doing anything. Why?

Update: I also looked at the signature of .on method and it is given as on(type, listener, opt_this). So I changed my .on method to map.on('singleclick', function addFullScreenPopups(evt) {//statements}); and called map.un('singleclick', addFullScreenPopups()); Unfortunately, this didn't help me out.

8

Try to wrap your callback function in a separate function. Something like that:

//here is you callback function
function myCallback(evt){
        var popup = new ol.Overlay.Popup({insertFirst: false});
        map.addOverlay(popup);
        var prettyCoord = ol.coordinate.toStringHDMS(ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326'), 2);
        console.log("Pretty Coordinates : "+prettyCoord);
        popup.show(evt.coordinate, '<div><h2>Coordinates</h2><p>' + prettyCoord + '</p></div>');
    }
}
//To bind the event 
map.on('singleclick', myCallback);
//To unbind the event
map.un('singleclick', myCallback);
4

In case you would like to keep your registering function in its current form, the on method in OpenLayers returns the unique key assigned to the event listener function, which can be used with the ol.Observable.unByKey global function.

function addFullScreenPopups(){
  var evtKey = map.on('singleclick', function(evt) {
    var popup = new ol.Overlay.Popup({insertFirst: false});
    map.addOverlay(popup);
    var prettyCoord = ol.coordinate.toStringHDMS(ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326'), 2);
    console.log("Pretty Coordinates : "+prettyCoord);
    popup.show(evt.coordinate, '<div><h2>Coordinates</h2><p>' + prettyCoord + '</p></div>');
  });
  return evtKey;
}

var key = addFullScreenPopups(); //Register the listener and save the key.
ol.Observable.unByKey(key); //Remove the listener. You don't have to know about the map object and the click event, it is handled by OL internally.
1

You can use Javascript's removeEventListener function to remove the listener.

map.removeEventListener('singleclick');
  • 1
    Albeit I like your point, there are some misconceptions in your answer preventing me from upvoting it. 1) OL's removeEventListener is not a native JS function, OL successfully implemented the DOM 2 EventTarget interface. 2) if you don't specify the listener function, OL removes the last one associated to the event type on the appropriate object. See the source code. – Gabor Farkas May 25 '17 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.