From ddc3a9ca61ab77a645caa0f47f7cca247ccbed2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20S=C3=A1nchez=20Ortega?= Date: Thu, 2 Feb 2017 16:21:10 +0100 Subject: [PATCH] Fix max/min calculation for getBoundsZoom (fixes #5136) (#5137) * Fix max/min calculation for getBoundsZoom (fixes #5136) * Added test for inside param of L.Map.getBoundsZoom() --- spec/suites/map/MapSpec.js | 9 +++++++++ src/map/Map.js | 6 ++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index 953cced9..650e4ad1 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -178,6 +178,7 @@ describe("Map", function () { describe("#getBoundsZoom", function () { var halfLength = 0.00025; var bounds = [[-halfLength, -halfLength], [halfLength, halfLength]]; + var wideBounds = [[-halfLength, -halfLength * 10], [halfLength, halfLength * 10]]; var padding = [100, 100]; var height = '400px'; @@ -219,6 +220,14 @@ describe("Map", function () { map.setZoom(16); expect(map.getBoundsZoom(bounds, false, padding)).to.eql(9); }); + + it("respects the 'inside' parameter", function () { + var container = map.getContainer(); + container.style.height = height; + document.body.appendChild(container); + expect(map.getBoundsZoom(wideBounds, false, padding)).to.be.equal(17); + expect(map.getBoundsZoom(wideBounds, true, padding)).to.be.equal(20); + }); }); describe('#setMaxBounds', function () { diff --git a/src/map/Map.js b/src/map/Map.js index 8b6ba24e..99c1214d 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -806,9 +806,11 @@ export var Map = Evented.extend({ se = bounds.getSouthEast(), size = this.getSize().subtract(padding), boundsSize = toBounds(this.project(se, zoom), this.project(nw, zoom)).getSize(), - snap = Browser.any3d ? this.options.zoomSnap : 1; + snap = Browser.any3d ? this.options.zoomSnap : 1, + scalex = size.x / boundsSize.x, + scaley = size.y / boundsSize.y, + scale = inside ? Math.max(scalex, scaley) : Math.min(scalex, scaley); - var scale = Math.min(size.x / boundsSize.x, size.y / boundsSize.y); zoom = this.getScaleZoom(scale, zoom); if (snap) {