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

Prevent map from rotating when user just wants to zoom #48

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dist/leaflet-rotate-src.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/leaflet-rotate-src.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/leaflet-rotate.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/leaflet-rotate.js.map

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/map/handler/TouchGestures.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ L.Map.mergeOptions({
*/
bounceAtZoomLimits: true,

/**
* Set a minimum bearing value (rotate threshold) to
* prevent map from rotating when user just wants to
* zoom.
*
* Recommended value: 25
*
* @type { number | undefined }
*/
touchRotateIntertia: undefined

});

L.Map.TouchGestures = L.Handler.extend({
Expand Down Expand Up @@ -87,12 +98,21 @@ L.Map.TouchGestures = L.Handler.extend({
p2 = map.mouseEventToContainerPoint(e.touches[1]),
vector = p1.subtract(p2),
scale = p1.distanceTo(p2) / this._startDist,
inertia = map.options.touchRotateIntertia,
delta;

if (this._rotating) {
var theta = Math.atan(vector.x / vector.y);
var bearingDelta = (theta - this._startTheta) * L.DomUtil.RAD_TO_DEG;

if (vector.y < 0) { bearingDelta += 180; }

// prevent map rotation when bearingDelta < touchRotateIntertia
if (inertia) {
this._inertia = this._inertia || (Math.abs(this._startBearing - bearingDelta) >= inertia && bearingDelta);
bearingDelta = this._inertia ? bearingDelta - this._inertia : 0; // 0 = no rotation
}

if (bearingDelta) {
/**
* @TODO the pivot should be the last touch point,
Expand Down Expand Up @@ -151,6 +171,7 @@ L.Map.TouchGestures = L.Handler.extend({

this._zooming = false;
this._rotating = false;
this._inertia = false;
L.Util.cancelAnimFrame(this._animRequest);

L.DomEvent
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
// shiftKeyRotate: false,
// touchGestures: true,
touchRotate: true,
// touchRotateIntertia: 25,
// touchZoom: true
});

Expand Down