2

Is there a way to listen for the completion of a drag/pan event in OpenLayers v6.4.3? The 'moveend' event listener fires whenever the map move ends but is not specific to the map being moved by a drag/pan. If this event listener is used it fires when other actions cause the map to move, e.g., changing the center point of the map programmatically. The 'pointerdrag' event fires with every movement of the pointer drag not just when the drag/pan ends.

What we need is to specifically listen for the end of a drag/pan by the user. Is there an OpenLayers event listener for this purpose?

2 Answers 2

2

If a pointerdrag fires between movestart and moveend the move will be the result of the drag

let drag;
map.on('pointerdrag', function(){ drag = true; });
map.on('movestart', function(){ drag = false; });
map.on('moveend', function(){ console.log(drag); });
0

You could grab the zoom value of the view port. If the zoom level at the end of the event is the same as in the beginning of the event, it means that you just dragged the map. This applies with on.('moveend') on.('movestart') triggers

Something like the following:

let initialZoom = map.getView().getZoom();
map.on('moveend', e => {
      let finalZoom = map.getView().getZoom();
      if(initialZoom !== finalZoom ){
         //this event has to do with a zoom in - zoom out
         initialZoom = finalZoom;
      }else{
          //here you just dragged the map
      }
  });

5
  • This answer is not valid for two reasons. Zoom value does not change when map is panned, only on zoom change. The second reason is that question requires move to be detected only when map is dragged by mouse. Above solution does not take that into account.
    – TomazicM
    Commented Nov 11, 2020 at 9:00
  • When you zoom the map the on.('moveend') will trigger though, it is kind of a workaround to capture both zoom and pan in a single event. I have used this kind of workaround myself and it works. Commented Nov 11, 2020 at 9:19
  • Yes, but question is about drag/pan, not about zooming.
    – TomazicM
    Commented Nov 11, 2020 at 10:05
  • Yes actually they want to capture an event during moveend, something that pointerdrag cannot do. The moveend trigger fires not only in pan but in zoom too. This is why you can compare the two zoom values and if they are unchanged, it means that the map was just panned and so you can create a "custom" pointerdrag event but only when the dragging stops. Anyway, I just think that this answers exactly what op wanted to achieve. Commented Nov 11, 2020 at 12:56
  • Your are right about this one, my mistake.
    – TomazicM
    Commented Nov 11, 2020 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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