Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support padding positions in fitBounds
Browse files Browse the repository at this point in the history
- ref #995
tristen committed Nov 5, 2015
1 parent acc4eb7 commit 3b4e558
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions js/ui/camera.js
Original file line number Diff line number Diff line change
@@ -337,7 +337,7 @@ util.extend(Camera.prototype, /** @lends Map.prototype */{
* @param {number} [options.speed=1.2] How fast animation occurs
* @param {number} [options.curve=1.42] How much zooming out occurs during animation
* @param {Function} options.easing
* @param {number} options.padding how much padding there is around the given bounds on each side in pixels
* @param {number|<Array<number>} options.padding how much padding there is around the given bounds on each side in pixels. Accepts a number for all padding edges or an array [n, e, s, w]
* @param {number} options.maxZoom
* @fires movestart
* @fires moveend
@@ -346,20 +346,25 @@ util.extend(Camera.prototype, /** @lends Map.prototype */{
fitBounds: function(bounds, options) {

options = util.extend({
padding: 0,
padding: [0, 0, 0, 0],
offset: [0, 0],
maxZoom: Infinity
}, options);

if (typeof options.padding === 'number') {
var p = options.padding;
options.padding = [p, p, p, p];
}

bounds = LngLatBounds.convert(bounds);

var offset = Point.convert(options.offset),
tr = this.transform,
nw = tr.project(bounds.getNorthWest()),
se = tr.project(bounds.getSouthEast()),
size = se.sub(nw),
scaleX = (tr.width - options.padding * 2 - Math.abs(offset.x) * 2) / size.x,
scaleY = (tr.height - options.padding * 2 - Math.abs(offset.y) * 2) / size.y;
scaleX = (tr.width - options.padding[1] + options.padding[3] * 2 - Math.abs(offset.x) * 2) / size.x,
scaleY = (tr.height - options.padding[0] + options.padding[2] * 2 - Math.abs(offset.y) * 2) / 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);

0 comments on commit 3b4e558

Please sign in to comment.