Add padding options for map.fitBounds/getBoundsZoom, close #859

Also make fitBounds accept ILayer, and fix bounds for fitWorld, close
#960
This commit is contained in:
Vladimir Agafonkin 2013-04-22 15:21:30 +03:00
parent d91f56009b
commit de0cd66f88

View File

@ -71,20 +71,29 @@ L.Map = L.Class.extend({
return this.setView(newCenter, zoom); return this.setView(newCenter, zoom);
}, },
fitBounds: function (bounds) { // (LatLngBounds) fitBounds: function (bounds, paddingTopLeft, paddingBottomRight) { // (LatLngBounds || ILayer[, Point, Point])
bounds = L.latLngBounds(bounds);
var zoom = this.getBoundsZoom(bounds), bounds = bounds.getBounds ? bounds.getBounds() : L.latLngBounds(bounds);
swPoint = this.project(bounds.getSouthWest()),
nePoint = this.project(bounds.getNorthEast()), paddingTopLeft = L.point(paddingTopLeft || [0, 0]);
center = this.unproject(swPoint.add(nePoint).divideBy(2)); paddingBottomRight = L.point(paddingBottomRight || paddingTopLeft);
var zoom = this.getBoundsZoom(bounds, false, paddingTopLeft.add(paddingBottomRight)),
paddingOffset = new L.Point(
paddingBottomRight.x - paddingTopLeft.x,
paddingBottomRight.y - paddingTopLeft.y).divideBy(2);
swPoint = this.project(bounds.getSouthWest(), zoom),
nePoint = this.project(bounds.getNorthEast(), zoom),
center = this.unproject(swPoint.add(nePoint).divideBy(2).add(paddingOffset), zoom);
return this.setView(center, zoom); return this.setView(center, zoom);
}, },
fitWorld: function () { fitWorld: function () {
var sw = new L.LatLng(-60, -170), var sw = new L.LatLng(-90, -180),
ne = new L.LatLng(85, 179); ne = new L.LatLng(90, 180);
return this.fitBounds(new L.LatLngBounds(sw, ne)); return this.fitBounds(new L.LatLngBounds(sw, ne));
}, },
@ -321,7 +330,7 @@ L.Map = L.Class.extend({
return Math.min(z1, z2); return Math.min(z1, z2);
}, },
getBoundsZoom: function (bounds, inside) { // (LatLngBounds, Boolean) -> Number getBoundsZoom: function (bounds, inside, padding) { // (LatLngBounds[, Boolean, Point]) -> Number
bounds = L.latLngBounds(bounds); bounds = L.latLngBounds(bounds);
var size = this.getSize(), var size = this.getSize(),
@ -329,10 +338,10 @@ L.Map = L.Class.extend({
maxZoom = this.getMaxZoom(), maxZoom = this.getMaxZoom(),
ne = bounds.getNorthEast(), ne = bounds.getNorthEast(),
sw = bounds.getSouthWest(), sw = bounds.getSouthWest(),
boundsSize, zoomNotFound = true,
nePoint, boundsSize, nePoint, swPoint;
swPoint,
zoomNotFound = true; padding = L.point(padding || [0, 0]);
if (inside) { if (inside) {
zoom--; zoom--;
@ -344,8 +353,8 @@ L.Map = L.Class.extend({
swPoint = this.project(sw, zoom); swPoint = this.project(sw, zoom);
boundsSize = new L.Point( boundsSize = new L.Point(
Math.abs(nePoint.x - swPoint.x), Math.abs(nePoint.x - swPoint.x) + padding.x,
Math.abs(swPoint.y - nePoint.y)); Math.abs(swPoint.y - nePoint.y) + padding.y);
if (!inside) { if (!inside) {
zoomNotFound = boundsSize.x <= size.x && boundsSize.y <= size.y; zoomNotFound = boundsSize.x <= size.x && boundsSize.y <= size.y;