diff --git a/src/map/Map.js b/src/map/Map.js index 309230049f8..bcd7f8a6938 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -119,34 +119,40 @@ L.Map = L.Class.extend({ } } + this.on('moveend', this._panInsideMaxBounds, this); + return this; }, panInsideBounds: function (bounds) { bounds = L.latLngBounds(bounds); - var viewBounds = this.getBounds(), - viewSw = this.project(viewBounds.getSouthWest()), - viewNe = this.project(viewBounds.getNorthEast()), + var viewBounds = this.getPixelBounds(), + viewSw = viewBounds.getBottomLeft(), + viewNe = viewBounds.getTopRight(), sw = this.project(bounds.getSouthWest()), ne = this.project(bounds.getNorthEast()), dx = 0, dy = 0; if (viewNe.y < ne.y) { // north - dy = ne.y - viewNe.y; + dy = Math.ceil(ne.y - viewNe.y); } if (viewNe.x > ne.x) { // east - dx = ne.x - viewNe.x; + dx = Math.floor(ne.x - viewNe.x); } if (viewSw.y > sw.y) { // south - dy = sw.y - viewSw.y; + dy = Math.floor(sw.y - viewSw.y); } if (viewSw.x < sw.x) { // west - dx = sw.x - viewSw.x; + dx = Math.ceil(sw.x - viewSw.x); + } + + if (dx || dy) { + return this.panBy(new L.Point(dx, dy)); } - return this.panBy(new L.Point(dx, dy, true)); + return this; }, addLayer: function (layer) { @@ -609,6 +615,10 @@ L.Map = L.Class.extend({ } }, + _panInsideMaxBounds: function () { + this.panInsideBounds(this.options.maxBounds); + }, + _checkIfLoaded: function () { if (!this._loaded) { throw new Error('Set map center and zoom first.'); diff --git a/src/map/handler/Map.Drag.js b/src/map/handler/Map.Drag.js index 4f09d31c7dd..8a82ca6da69 100644 --- a/src/map/handler/Map.Drag.js +++ b/src/map/handler/Map.Drag.js @@ -140,15 +140,6 @@ L.Map.Drag = L.Handler.extend({ }); } } - - if (options.maxBounds) { - // TODO predrag validation instead of animation - L.Util.requestAnimFrame(this._panInsideMaxBounds, map, true, map._container); - } - }, - - _panInsideMaxBounds: function () { - this.panInsideBounds(this.options.maxBounds); } });