From 873d4cab31722d67c60c08dbd878aa6e4734203e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20S=C3=A1nchez=20Ortega?= Date: Thu, 11 May 2017 22:33:46 +0200 Subject: [PATCH] Handle edge case of empty bounds on _getBoundsCenterZoom (#5157) * Handle edge case of empty bounds on _getBoundsCenterZoom * Unit test for map._getBoundsCenterZoom --- spec/suites/map/MapSpec.js | 11 +++++++++++ src/map/Map.js | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index 650e4ad1..00841665 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -842,6 +842,17 @@ describe("Map", function () { }); }); + describe('#_getBoundsCenterZoom', function () { + var center = L.latLng(50.5, 30.51); + + it('Returns valid center on empty bounds in unitialized map', function () { + // Edge case from #5153 + var centerAndZoom = map._getBoundsCenterZoom([center, center]); + expect(centerAndZoom.center).to.eql(center); + expect(centerAndZoom.zoom).to.eql(Infinity); + }); + }); + describe('#fitBounds', function () { var center = L.latLng(50.5, 30.51), bounds = L.latLngBounds(L.latLng(1, 102), L.latLng(11, 122)), diff --git a/src/map/Map.js b/src/map/Map.js index 8da16f3b..9ea70db0 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -255,6 +255,13 @@ export var Map = Evented.extend({ zoom = (typeof options.maxZoom === 'number') ? Math.min(options.maxZoom, zoom) : zoom; + if (zoom === Infinity) { + return { + center: bounds.getCenter(), + zoom: zoom + }; + } + var paddingOffset = paddingBR.subtract(paddingTL).divideBy(2), swPoint = this.project(bounds.getSouthWest(), zoom),