Skip to content

Commit

Permalink
add map.getMaxBounds method
Browse files Browse the repository at this point in the history
Credit to @lamuertepeluda which provided some of the basis to this
commit at #4029 (comment)
  • Loading branch information
andrewharvey committed Jun 26, 2017
1 parent f0cd11d commit cb7ed47
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit cb7ed47

Please sign in to comment.