2

I am using openlayers 3.20.0. And below link will show you the small example.

click-here. (In the example see the console for logs)

I have a map object as

this.map = new ol.Map({
    layers : [ somelayer ],
    target : 'map',
    view : new ol.View({
        center : [ -11000000, 4600000 ],
        zoom : 4
    })
});

Here I have attached 'singleclick' and 'dblclick' on map.

And also I have draw interaction on map, as below

this.draw = new ol.interaction.Draw({
    features : features,
    type : "Point"
});

which is emits 'drawstart' and 'drawend' event.

But when we are drawing a point it will emit 'singleclick' event on map also. How can we prevent this from not emitting the 'singleclick' while drawing.

I know we can remove the 'singleclick' event listener while drawing, but I don't want to remove listener and add again.

Is there any other way of suppressing this event.

6
  • event.stopPropagation(); use this.
    – Jai
    Commented Mar 10, 2017 at 7:46
  • I have tried with that one also, but not working. Commented Mar 10, 2017 at 7:50
  • 1
    It's not clear what you're trying to suppress, since nothing changes if singleclick is or not active. But I think you're looking for event.preventDefault(). Commented Mar 10, 2017 at 11:07
  • When we are drawing I do not want the singleclick event to fire. I tried with the event.stopPropagation() also. But It will fire drawstart then drawend and finally singleclick events. Commented Mar 10, 2017 at 11:34
  • 1
    It would also be nice to know why you want to prevent the event from occuring. If that's because you have other tools active at the same time, you could instead deactivate them while you're drawing. Commented Mar 10, 2017 at 13:07

1 Answer 1

1

I was having the same problem. I solved this with unByKey method. Every map.on method returns a unique key. You can use map.unByKey(singleclick_key) to stop single click event. You can initialize it after completing your drawing on more time.

So, you are looking for something like this:

var singleclick_key= map.on('singleclick', function(evt){
//statements
}

and you can remove this event by using the following command:

map.unByKey(singleclick_key);

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