Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a movement threshold for drag panning #1832

Closed
jlc467 opened this issue Dec 12, 2015 · 11 comments · Fixed by #6809
Closed

Add a movement threshold for drag panning #1832

jlc467 opened this issue Dec 12, 2015 · 11 comments · Fixed by #6809

Comments

@jlc467
Copy link

jlc467 commented Dec 12, 2015

As a user, very easy to trigger drag event when you really meant for a click event. Can listen to dragstart+end and fire click if drag was only a pixel or two, but wondering if there is an option to set drag threshold directly.. if not, should there be?

@bhousel
Copy link
Contributor

bhousel commented Dec 12, 2015

I don't know if we need to make it an option, but I do agree that we should add a tolerance into the click events so that drag/rotate do not start until a user moves more than a few pixels, and clicks do fire even if start/end positions are within the threshold.

@musicformellons
Copy link

Maybe related. My experience is that in a mobile screen it happens quite often that I unintentionally apparently swipe the map where I meant just to drag it a little with huge leap to 'unknown territory'. I would also like to have a more controlled/ modest dragging somehow. Would it be possible to have a couple of 'drag tweak settings'?

@lucaswoj lucaswoj changed the title Preventing accidental map drags Add a tolerance to click events Jul 29, 2016
@yatiac
Copy link

yatiac commented Jan 4, 2017

This is an issue for us as well.
We are coming from leaflet and with leaflet we had a parameter on the drag event called 'distance' and with that you were able to define a threshold for that.

On MapboxGL you will need to calculate this by hand by doing something like this:

self.map.on('dragend', function(e) {
            var startPos = e.target.dragPan._startPos;
            var endPos = e.target.dragPan._pos;
            var distance = self._getDistance(startPos,endPos);
             if (distance < 20) { return; } // this is a threshold to avoid firing events with small (accidental) moves 
             // any other code you need
         });

Map.prototype._getDistance = function(p1,p2){
    return Math.sqrt(Math.pow(Math.abs(p2.y-p1.y),2)+Math.pow(Math.abs(p2.x-p1.x),2));
};

It would be good to have a distance as a property (as seen here).

@mollymerp
Copy link
Contributor

hi @yatiac! thanks for submitting your suggestion. while this is not a high priority for our development team right now, we would happily consider a pull request implementing this option.

@yatiac
Copy link

yatiac commented Jan 5, 2017

@mollymerp thank you for your response, my team will work on the PR.

For now we found another solution that seems simpler:

self.map.on('dragend', function(e) {          
            var distance = e.target.dragPan._startPos.dist(e.target.dragPan._pos)
             if (distance < 20) { return; } // this is a threshold to avoid firing events with small (accidental) moves 
             // any other code you need
      });

This can be easily implemented in MapboxGL but we don't have the time right now, if someone else is available, you can take a look at this and implement it in here.

@jlc467
Copy link
Author

jlc467 commented Jan 5, 2017

@yatiac Thanks! Tested your snippet above in a jsbin and it resolved my original issue.

@jlc467 jlc467 closed this as completed Jan 5, 2017
@lucaswoj
Copy link
Contributor

lucaswoj commented Jan 5, 2017

Re-opening to track the task of integrating this enhancement into GL JS core 😄

@lucaswoj lucaswoj reopened this Jan 5, 2017
@jfirebaugh
Copy link
Contributor

I think we should leave this as something that is implemented downstream of Mapbox GL JS. Most operating system's have built in tolerance for mouse events, and I don't think Mapbox GL JS should override that by default.

@hyperknot
Copy link

hyperknot commented May 15, 2018

@jfirebaugh this is a different kind of tolerance than what you are referring to, this issue is not filtered by operating systems.

The problem is that when the user clicks on interactive features usually it's not a pixel precise click but a <4px drag, especially when using a mouse. Leaflet had this implemented in core and I agree that this would be nice to have in a polished/tested form in Mapbox GL JS core.

@jfirebaugh jfirebaugh reopened this May 25, 2018
@jfirebaugh jfirebaugh changed the title Add a tolerance to click events Add a movement threshold for drag panning May 25, 2018
@msbarry
Copy link
Contributor

msbarry commented Jun 13, 2018

I'm using mapbox-gl for a very click-heavy application and this issue was starting to bother me. Submitted a pull request that attempts to fix this. Let me know you think.

@hyperknot
Copy link

@msbarry this is so nice, thanks for making it into a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants