From 3072990ce052cba101b20409fb5c2dc1559b2807 Mon Sep 17 00:00:00 2001 From: Stepan Kuzmin Date: Wed, 25 Oct 2017 13:04:39 +0300 Subject: [PATCH] add initial bounds as map constructor option #1970 --- src/ui/map.js | 18 ++++++++++++------ test/unit/ui/map.test.js | 10 ++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/ui/map.js b/src/ui/map.js index 4f59e7b01a1..3c96fded8cd 100755 --- a/src/ui/map.js +++ b/src/ui/map.js @@ -193,6 +193,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 maximum 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 @@ -368,12 +369,17 @@ 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()) { - this.jumpTo({ - center: options.center, - zoom: options.zoom, - bearing: options.bearing, - pitch: options.pitch - }); + if (options.bounds) { + this.resize(); + this.fitBounds(options.bounds, { duration: 0 }); + } else { + this.jumpTo({ + center: options.center, + zoom: options.zoom, + bearing: options.bearing, + pitch: options.pitch + }); + } } this.resize(); diff --git a/test/unit/ui/map.test.js b/test/unit/ui/map.test.js index 095899a147f..1d7b949519d 100755 --- a/test/unit/ui/map.test.js +++ b/test/unit/ui/map.test.js @@ -54,6 +54,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(t, {interactive: false});