From cb7ed47558888dac42b4a5af61e003be939cc00c Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Mon, 26 Jun 2017 20:38:14 +1000 Subject: [PATCH 1/2] add map.getMaxBounds method Credit to @lamuertepeluda which provided some of the basis to this commit at https://github.com/mapbox/mapbox-gl-js/issues/4029#issuecomment-294713995 --- src/ui/map.js | 17 +++++++++++++++++ test/unit/ui/map.test.js | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/ui/map.js b/src/ui/map.js index e2af2b826bb..98ef0e03aee 100755 --- a/src/ui/map.js +++ b/src/ui/map.js @@ -406,6 +406,22 @@ class Map extends Camera { return bounds; } + /** + * Gets the map's geographical bounds. + * + * Returns the LngLatBounds by which pan and zoom operations on the map are constrained. + * + * @returns {LngLatBounds | null} The maximum bounds the map is constrained to, or `null` if none set. + */ + getMaxBounds () { + if (!(!Array.isArray(this.transform.latRange) || this.transform.latRange.length === 0) && + (!Array.isArray(this.transform.lngRange) || this.transform.lngRange.length === 0)) { + return null; + } + return new LngLatBounds([this.transform.lngRange[0], this.transform.latRange[0]], + [this.transform.lngRange[1], this.transform.latRange[1]]); + } + /** * Sets or clears the map's geographical bounds. * @@ -434,6 +450,7 @@ class Map extends Camera { return this; } + /** * Sets or clears the map's minimum zoom level. * If the map's current zoom level is lower than the new minimum, diff --git a/test/unit/ui/map.test.js b/test/unit/ui/map.test.js index 50092deee7b..5d7dca187fc 100755 --- a/test/unit/ui/map.test.js +++ b/test/unit/ui/map.test.js @@ -645,6 +645,24 @@ test('Map', (t) => { t.end(); }); + t.test('#getMaxBounds', (t) => { + t.test('returns null when no bounds set', (t) => { + const map = createMap({zoom:0}); + t.equal(map.getMaxBounds(), null); + t.end(); + }); + + t.test('returns bounds', (t) => { + const map = createMap({zoom:0}); + const bounds = [[-130.4297, 50.0642], [-61.52344, 24.20688]]; + map.setMaxBounds(bounds); + t.deepEqual(map.getMaxBounds().toArray(), bounds); + t.end(); + }); + + t.end(); + }); + t.test('#setMinZoom', (t) => { const map = createMap({zoom:5}); map.setMinZoom(3.5); From 07cc2915fe56a1bced2b375c5fd78e95cf904e3a Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Tue, 27 Jun 2017 08:54:31 +1000 Subject: [PATCH 2/2] make getMaxBounds easier to read --- src/ui/map.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ui/map.js b/src/ui/map.js index 98ef0e03aee..21f82a6c221 100755 --- a/src/ui/map.js +++ b/src/ui/map.js @@ -414,12 +414,13 @@ class Map extends Camera { * @returns {LngLatBounds | null} The maximum bounds the map is constrained to, or `null` if none set. */ getMaxBounds () { - if (!(!Array.isArray(this.transform.latRange) || this.transform.latRange.length === 0) && - (!Array.isArray(this.transform.lngRange) || this.transform.lngRange.length === 0)) { + if (this.transform.latRange && this.transform.latRange.length === 2 && + this.transform.lngRange && this.transform.lngRange.length === 2) { + return new LngLatBounds([this.transform.lngRange[0], this.transform.latRange[0]], + [this.transform.lngRange[1], this.transform.latRange[1]]); + } else { return null; } - return new LngLatBounds([this.transform.lngRange[0], this.transform.latRange[0]], - [this.transform.lngRange[1], this.transform.latRange[1]]); } /**