[GIS] Stop OpenLayers 3 map firing click event after dragging

javascriptopenlayers

I have an OpenLayers 3 map with a source of ol.source.MapQuest({ layer: 'osm' }). I haven't added a DragPan interaction, but the map is draggable anyway – which I want it to be.

I also have a click event for the map:

$(MapUtility.map.getViewport()).on('click', function(e) {
    //Stuff
});

which fires every time I finish dragging the map.

I used to have a similar problem with a DragBox interaction and solved it by calling e.preventDefault() at the start of my boxend event. However, that was an interaction I added and wrote the event for. The DragPan is in ol.js and I'd rather not change that if possible.

Is there something I can do in my click event to check whether the click was caused by DragPan? Alternatively, is there a way to amend the behaviour of this instance of the DragPan interaction without changing it globally?

Best Answer

I had the same issue, with a click event firing when dragging a map, when using:

map.getViewport().addEventListener('click', function);

I have changed this to use the ol3 event 'singleclick', the singleclick has a small delay to ensure it is not triggered by a drag or doubleclick:

map.on('singleclick', function(evt){
  var coordinate = evt.coordinate;
  var pixel = evt.pixel;
}

This way also provides more useful parameters on the event.

Source: ol3-dev discussion