From 92acc3e2d9d6fad32767432570b1ab0209abc1f1 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Thu, 17 Oct 2013 12:02:51 +0200 Subject: [PATCH] Make sure that the event position is correct for touch events This is a workaround for https://code.google.com/p/closure-library/issues/detail?id=588 --- src/ol/map.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/ol/map.js b/src/ol/map.js index 36526aba47..24c9d42a37 100644 --- a/src/ol/map.js +++ b/src/ol/map.js @@ -446,8 +446,22 @@ ol.Map.prototype.getEventCoordinate = function(event) { * @return {ol.Pixel} Pixel. */ ol.Map.prototype.getEventPixel = function(event) { - var eventPosition = goog.style.getRelativePosition(event, this.viewport_); - return [eventPosition.x, eventPosition.y]; + // goog.style.getRelativePosition is based on event.targetTouches, + // but touchend and touchcancel events have no targetTouches when + // the last finger is removed from the screen. + // So we ourselves compute the position of touch events. + // See https://code.google.com/p/closure-library/issues/detail?id=588 + if (goog.isDef(event.changedTouches)) { + var touch = event.changedTouches.item(0); + var viewportPosition = goog.style.getClientPosition(this.viewport_); + return [ + touch.clientX - viewportPosition.x, + touch.clientY - viewportPosition.y + ]; + } else { + var eventPosition = goog.style.getRelativePosition(event, this.viewport_); + return [eventPosition.x, eventPosition.y]; + } };