Skip to content

Commit

Permalink
move bounds logic from Map to Transform
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Aug 1, 2018
1 parent 1a0a8d1 commit 7cdb197
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
40 changes: 38 additions & 2 deletions src/geo/transform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow

import LngLat from './lng_lat';

import LngLatBounds from './lng_lat_bounds';
import Point from '@mapbox/point-geometry';
import Coordinate from './coordinate';
import { wrap, clamp } from '../util/util';
Expand Down Expand Up @@ -52,7 +52,7 @@ class Transform {
this._minZoom = minZoom || 0;
this._maxZoom = maxZoom || 22;

this.latRange = [-85.05113, 85.05113];
this.setMaxBounds();

this.width = 0;
this.height = 0;
Expand Down Expand Up @@ -401,6 +401,42 @@ class Transform {
return new Point(p[0] / p[3], p[1] / p[3]);
}

/**
* Returns the map's geographical bounds. When the bearing or pitch is non-zero, the visible region is not
* an axis-aligned rectangle, and the result is the smallest bounds that encompasses the visible region.
*/
getBounds(): LngLatBounds {
return new LngLatBounds()
.extend(this.pointLocation(new Point(0, 0)))
.extend(this.pointLocation(new Point(this.width, 0)))
.extend(this.pointLocation(new Point(this.width, this.height)))
.extend(this.pointLocation(new Point(0, this.height)));
}

/**
* Returns the maximum geographical bounds the map is constrained to, or `null` if none set.
*/
getMaxBounds(): LngLatBounds | null {
if (!this.latRange || this.latRange.length !== 2 ||
!this.lngRange || this.lngRange.length !== 2) return null;

return new LngLatBounds([this.lngRange[0], this.latRange[0]], [this.lngRange[1], this.latRange[1]]);
}

/**
* Sets or clears the map's geographical constraints.
*/
setMaxBounds(bounds?: LngLatBounds) {
if (bounds) {
this.lngRange = [bounds.getWest(), bounds.getEast()];
this.latRange = [bounds.getSouth(), bounds.getNorth()];
this._constrain();
} else {
this.lngRange = null;
this.latRange = [-85.05113, 85.05113];
}
}

/**
* Calculate the posMatrix that, given a tile coordinate, would be used to display the tile on a map.
* @param {UnwrappedTileID} unwrappedTileID;
Expand Down
41 changes: 8 additions & 33 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,32 +477,16 @@ class Map extends Camera {
/**
* Returns the map's geographical bounds. When the bearing or pitch is non-zero, the visible region is not
* an axis-aligned rectangle, and the result is the smallest bounds that encompasses the visible region.
*
* @returns {LngLatBounds}
*/
getBounds() {
return new LngLatBounds()
.extend(this.transform.pointLocation(new Point(0, 0)))
.extend(this.transform.pointLocation(new Point(this.transform.width, 0)))
.extend(this.transform.pointLocation(new Point(this.transform.width, this.transform.height)))
.extend(this.transform.pointLocation(new Point(0, this.transform.height)));
getBounds(): LngLatBounds {
return this.transform.getBounds();
}

/**
* 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.
* Returns the maximum geographical bounds the map is constrained to, or `null` if none set.
*/
getMaxBounds () {
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;
}
getMaxBounds(): LngLatBounds | null {
return this.transform.getMaxBounds();
}

/**
Expand All @@ -515,21 +499,12 @@ class Map extends Camera {
* as close as possible to the operation's request while still
* remaining within the bounds.
*
* @param {LngLatBoundsLike | null | undefined} lnglatbounds The maximum bounds to set. If `null` or `undefined` is provided, the function removes the map's maximum bounds.
* @param {LngLatBoundsLike | null | undefined} bounds The maximum bounds to set. If `null` or `undefined` is provided, the function removes the map's maximum bounds.
* @returns {Map} `this`
*/
setMaxBounds(lnglatbounds: LngLatBoundsLike) {
if (lnglatbounds) {
const b = LngLatBounds.convert(lnglatbounds);
this.transform.lngRange = [b.getWest(), b.getEast()];
this.transform.latRange = [b.getSouth(), b.getNorth()];
this.transform._constrain();
} else if (lnglatbounds === null || lnglatbounds === undefined) {
this.transform.lngRange = null;
this.transform.latRange = null;
}
setMaxBounds(bounds: LngLatBoundsLike) {
this.transform.setMaxBounds(LngLatBounds.convert(bounds));
return this._update();

}

/**
Expand Down

0 comments on commit 7cdb197

Please sign in to comment.