From 106054140b18e94d8a19bcba59a1a734c74a95ce Mon Sep 17 00:00:00 2001 From: mourner Date: Fri, 23 Sep 2011 16:55:47 +0300 Subject: [PATCH] A better attempt at the copy world problem, hopefully closed #273, fixed #293, fixed #294, closed #288 --- src/dom/Draggable.js | 1 + src/handler/MapDrag.js | 29 +++++++++++++++++++++++------ src/map/Map.js | 3 ++- src/map/ext/Map.PanAnimation.js | 4 +++- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/dom/Draggable.js b/src/dom/Draggable.js index 66f48d30..4e1a2b28 100644 --- a/src/dom/Draggable.js +++ b/src/dom/Draggable.js @@ -76,6 +76,7 @@ L.Draggable = L.Class.extend({ }, _updatePosition: function() { + this.fire('predrag'); L.DomUtil.setPosition(this._element, this._newPos); this.fire('drag'); }, diff --git a/src/handler/MapDrag.js b/src/handler/MapDrag.js index b5620b88..ed016ac6 100644 --- a/src/handler/MapDrag.js +++ b/src/handler/MapDrag.js @@ -12,6 +12,11 @@ L.Handler.MapDrag = L.Handler.extend({ this._draggable.on('dragstart', this._onDragStart, this); this._draggable.on('drag', this._onDrag, this); this._draggable.on('dragend', this._onDragEnd, this); + + if (this._map.options.worldCopyJump) { + this._draggable.on('predrag', this._onPreDrag, this); + this._map.on('viewreset', this._onViewReset, this); + } } this._draggable.enable(); this._enabled = true; @@ -37,15 +42,27 @@ L.Handler.MapDrag = L.Handler.extend({ this._map.fire('drag'); }, - _onDragEnd: function() { + _onViewReset: function() { + var pxCenter = this._map.getSize().divideBy(2), + pxWorldCenter = this._map.latLngToLayerPoint(new L.LatLng(0,0)); + + this._initialWorldOffset = pxWorldCenter.subtract(pxCenter); + }, + + _onPreDrag: function() { var map = this._map, - pane = map._panes.objectsPane, - left = map._getTopLeftPoint().x, - worldWidth = map.options.scale(this._map.getZoom()), - rest = ((left % worldWidth) + worldWidth) % worldWidth; + worldWidth = map.options.scale(map.getZoom()), + halfWidth = Math.round(worldWidth / 2), + dx = this._initialWorldOffset.x, + x = this._draggable._newPos.x, + newX1 = (x - halfWidth + dx) % worldWidth + halfWidth - dx, + newX2 = (x + halfWidth + dx) % worldWidth - halfWidth - dx, + newX = Math.abs(newX1 + dx) < Math.abs(newX2 + dx) ? newX1 : newX2; - pane.style.left = (left - rest) + 'px'; + this._draggable._newPos.x = newX; + }, + _onDragEnd: function() { map.fire('moveend'); map.fire('dragend'); } diff --git a/src/map/Map.js b/src/map/Map.js index 0d6b9063..21ebcfc9 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -32,7 +32,8 @@ L.Map = L.Class.extend({ // misc trackResize: true, - closePopupOnClick: true + closePopupOnClick: true, + worldCopyJump: true }, diff --git a/src/map/ext/Map.PanAnimation.js b/src/map/ext/Map.PanAnimation.js index 02ccfd15..fbdb9a4c 100644 --- a/src/map/ext/Map.PanAnimation.js +++ b/src/map/ext/Map.PanAnimation.js @@ -5,7 +5,9 @@ L.Map.include(!(L.Transition && L.Transition.implemented()) ? {} : { if (this._loaded && !forceReset && this._layers) { // difference between the new and current centers in pixels - var offset = this._getNewTopLeftPoint(center).subtract(this._getTopLeftPoint()); + var offset = this._getNewTopLeftPoint(center).subtract(this._getTopLeftPoint()); + + center = new L.LatLng(center.lat, center.lng); var done = (zoomChanged ? !!this._zoomToIfCenterInView && this._zoomToIfCenterInView(center, zoom, offset) :