-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLeaflet.DoubleTapDragZoom.js
106 lines (81 loc) · 3.31 KB
/
Leaflet.DoubleTapDragZoom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
L.Map.mergeOptions({
doubleTapDragZoom: L.Browser.touch && !L.Browser.android23,
doubleTapDragZoomOptions: {
reverse: false,
},
});
var DoubleTapDragZoom = L.Handler.extend({
addHooks: function () {
this._map.on('doubletapdragstart', this._onDoubleTapDragStart, this);
this._map.on('doubletapdrag', this._onDoubleTapDrag, this);
this._map.on('doubletapdragend', this._onDoubleTapDragEnd, this);
L.DomEvent.on(this._map._container, 'touchmove', this._onDragging, this);
},
removeHooks: function () {
this._map.off('doubletapdragstart', this._onDoubleTapDragStart, this);
this._map.off('doubletapdrag', this._onDoubleTapDrag, this);
this._map.off('doubletapdragend', this._onDoubleTapDragEnd, this);
},
_onDoubleTapDragStart: function (e) {
var map = this._map;
if (!e.touches || e.touches.length !== 1 || map._animatingZoom) { return; }
var p = map.mouseEventToContainerPoint(e.touches[0]);
this._startPointY = p.y;
this._startPoint = p;
this._centerPoint = map.getSize()._divideBy(2);
if (map.options.doubleTapDragZoom === 'center') {
this._startLatLng = map.containerPointToLatLng(this._centerPoint);
} else {
this._startLatLng = map.containerPointToLatLng(p);
}
this._startZoom = map.getZoom();
map._stop();
map._moveStart(true, false);
this._doubleTapDragging = true;
},
_onDoubleTapDrag: function (e) {
if (!e.touches || e.touches.length !== 1) { return; }
var map = this._map;
var reverse = this._map.options.doubleTapDragZoomOptions.reverse;
var p = map.mouseEventToContainerPoint(e.touches[0]);
if (p.y <= 0) {
return;
}
var distance = reverse ? p.y - this._startPointY : this._startPointY - p.y;
var scale = Math.pow(Math.E, distance / 200);
if (scale === 1) { return; }
this._zoom = map.getScaleZoom(scale, this._startZoom);
if (map.options.doubleTapDragZoom === 'center') {
this._center = this._startLatLng;
} else {
var delta =
L.point(this._startPoint.x, p.y)
._add(this._startPoint)
.divideBy(2)
._subtract(this._centerPoint);
this._center = map.unproject(map.project(this._startLatLng, this._zoom).subtract(delta), this._zoom);
}
L.Util.cancelAnimFrame(this._animRequest);
var moveFn = L.Util.bind(map._move, map, this._center, this._zoom, {pinch: true, round: false});
this._animRequest = L.Util.requestAnimFrame(moveFn, this, true);
},
_onDoubleTapDragEnd: function (e) {
this._doubleTapDragging = false;
if (!this._center) { return; }
L.Util.cancelAnimFrame(this._animRequest);
// Pinch updates GridLayers' levels only when zoomSnap is off, so zoomSnap becomes noUpdate.
if (this._map.options.zoomAnimation) {
this._map._animateZoom(this._center, this._map._limitZoom(this._zoom), true, this._map.options.zoomSnap);
} else {
this._map._resetView(this._center, this._map._limitZoom(this._zoom));
}
this._center = null;
},
_onDragging: function (e) {
if (this._doubleTapDragging) {
L.DomEvent.preventDefault(e);
L.DomEvent.stopPropagation(e);
}
}
});
L.Map.addInitHook('addHandler', 'doubleTapDragZoom', DoubleTapDragZoom);