Skip to content

Commit 327164e

Browse files
ansiskarimnaaji
authored andcommitted
fix mapTouchEvent.point for touchend events (#9536)
1 parent 516f44f commit 327164e

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/ui/events.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ export class MapTouchEvent extends Event {
157157
* @private
158158
*/
159159
constructor(type: string, map: Map, originalEvent: TouchEvent) {
160-
const points = DOM.touchPos(map.getCanvasContainer(), originalEvent.touches);
160+
const touches = type === "touchend" ? originalEvent.changedTouches : originalEvent.touches;
161+
const points = DOM.touchPos(map.getCanvasContainer(), touches);
161162
const lngLats = points.map((t) => map.unproject(t));
162163
const point = points.reduce((prev, curr, i, arr) => {
163164
return prev.add(curr.div(arr.length));
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {test} from '../../../util/test';
2+
import window from '../../../../src/util/window';
3+
import Map from '../../../../src/ui/map';
4+
import DOM from '../../../../src/util/dom';
5+
import simulate from '../../../util/simulate_interaction';
6+
7+
function createMap(t) {
8+
t.stub(Map.prototype, '_detectMissingCSS');
9+
return new Map({interactive: false, container: DOM.create('div', '', window.document.body)});
10+
}
11+
12+
test('MapEvent handler fires touch events with correct values', (t) => {
13+
const map = createMap(t);
14+
15+
const touchstart = t.spy();
16+
const touchmove = t.spy();
17+
const touchend = t.spy();
18+
19+
map.on('touchstart', touchstart);
20+
map.on('touchmove', touchmove);
21+
map.on('touchend', touchend);
22+
23+
const touchesStart = [{identifier: 1, clientX: 0, clientY: 50}];
24+
const touchesMove = [{identifier: 1, clientX: 0, clientY: 60}];
25+
const touchesEnd = [{identifier: 1, clientX: 0, clientY: 60}];
26+
27+
simulate.touchstart(map.getCanvas(), {touches: touchesStart, targetTouches: touchesStart});
28+
t.equal(touchstart.callCount, 1);
29+
t.deepEqual(touchstart.getCall(0).args[0].point, {x: 0, y: 50});
30+
t.equal(touchmove.callCount, 0);
31+
t.equal(touchend.callCount, 0);
32+
console.log(touchstart);
33+
34+
simulate.touchmove(map.getCanvas(), {touches: touchesMove, targetTouches: touchesMove});
35+
t.equal(touchstart.callCount, 1);
36+
t.equal(touchmove.callCount, 1);
37+
t.deepEqual(touchmove.getCall(0).args[0].point, {x: 0, y: 60});
38+
t.equal(touchend.callCount, 0);
39+
40+
simulate.touchend(map.getCanvas(), {touches: [], targetTouches: [], changedTouches: touchesEnd});
41+
t.equal(touchstart.callCount, 1);
42+
t.equal(touchmove.callCount, 1);
43+
t.equal(touchend.callCount, 1);
44+
t.deepEqual(touchend.getCall(0).args[0].point, {x: 0, y: 60});
45+
46+
map.remove();
47+
t.end();
48+
});

0 commit comments

Comments
 (0)