A better attempt at the copy world problem, hopefully closed #273, fixed #293, fixed #294, closed #288

This commit is contained in:
mourner 2011-09-23 16:55:47 +03:00
parent 8212981daa
commit 106054140b
4 changed files with 29 additions and 8 deletions

View File

@ -76,6 +76,7 @@ L.Draggable = L.Class.extend({
},
_updatePosition: function() {
this.fire('predrag');
L.DomUtil.setPosition(this._element, this._newPos);
this.fire('drag');
},

View File

@ -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');
}

View File

@ -32,7 +32,8 @@ L.Map = L.Class.extend({
// misc
trackResize: true,
closePopupOnClick: true
closePopupOnClick: true,
worldCopyJump: true
},

View File

@ -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) :