Skip to content

Commit

Permalink
add initial bounds as map constructor option mapbox#1970
Browse files Browse the repository at this point in the history
  • Loading branch information
stepankuzmin committed Oct 25, 2017
1 parent c5554ff commit 325f00a
Show file tree
Hide file tree
Showing 2 changed files with 27 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 @@ -186,6 +186,7 @@ const defaultOptions = {
* @param {number} [options.zoom=0] The initial zoom level of the map. If `zoom` is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to `0`.
* @param {number} [options.bearing=0] The initial bearing (rotation) of the map, measured in degrees counter-clockwise from north. If `bearing` is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to `0`.
* @param {number} [options.pitch=0] The initial pitch (tilt) of the map, measured in degrees away from the plane of the screen (0-60). If `pitch` is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to `0`.
* @param {LngLatBoundsLike} [options.bounds] The initial bounds of the map. If `bounds` is specified, it overrides `center` and `zoom` constructor options.
* @param {boolean} [options.renderWorldCopies=true] If `true`, multiple copies of the world will be rendered, when zoomed out.
* @param {number} [options.maxTileCacheSize=null] The maxiumum number of tiles stored in the tile cache for a given source. If omitted, the cache will be dynamically sized based on the current viewport.
* @param {string} [options.localIdeographFontFamily=null] If specified, defines a CSS font-family
Expand Down Expand Up @@ -324,6 +325,22 @@ class Map extends Camera {
this._hash = options.hash && (new Hash()).addTo(this);
// don't set position from options if set through hash
if (!this._hash || !this._hash._onHashChange()) {
if (options.bounds) {
const bounds = LngLatBounds.convert(options.bounds),
tr = this.transform,
nw = tr.project(bounds.getNorthWest()),
se = tr.project(bounds.getSouthEast()),
size = se.sub(nw),
dimensions = this._containerDimensions(),
width = dimensions[0],
height = dimensions[1],
scaleX = width / size.x,
scaleY = height / size.y;

options.center = tr.unproject(nw.add(se).div(2));
options.zoom = Math.min(tr.scaleZoom(tr.scale * Math.min(scaleX, scaleY)), options.maxZoom);
}

this.jumpTo({
center: options.center,
zoom: options.zoom,
Expand Down
10 changes: 10 additions & 0 deletions test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ test('Map', (t) => {
t.end();
});

t.test('initial bounds in constructor options', (t) => {
const bounds = [[-133, 16], [-68, 50]];
const map = createMap({bounds});

t.deepEqual(fixedLngLat(map.getCenter(), 4), { lng: -100.5, lat: 34.7171 });
t.equal(fixedNum(map.getZoom(), 3), 2.469);

t.end();
});

t.test('disables handlers', (t) => {
t.test('disables all handlers', (t) => {
const map = createMap({interactive: false});
Expand Down

0 comments on commit 325f00a

Please sign in to comment.