You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow custom bearing/rotation while using fitBounds() #1338
The bearing option in fitBounds() is currently hard-coded to 0, but it would be nice to support eased rotation in fitBounds(), too. Removing that bearing = 0 line will rotate the map, but without easing and with uncorrected bounds.
Here's an test-case around Wyoming:
// Wyoming, slightly offset because of simplificationmap.fitBounds([[45,-111],[41,-104]],{bearing: 45});
Not sure what happened with this, but cannot find it in the actual mapbox-gl-js latest release.
I ended up implementing my own version of fitBounds, called fitBoundsRotated, which has a small part of code that transforms the box and then uses that to calculate the ending bounds, plus using the map bearing or given bearing options.
// Copied code from MapBoxGL's Camera#fitBounds method
// Adding in rotation logic
fitBoundsRotated( bounds, options, eventData ) {
options = assign({
padding: {
top: 0,
bottom: 0,
right: 0,
left: 0
},
offset: [0, 0],
maxZoom: this.transform.maxZoom
}, options);
if (typeof options.padding === 'number') {
const p = options.padding;
options.padding = {
top: p,
bottom: p,
right: p,
left: p
};
}
bounds = mapboxgl.LngLatBounds.convert(bounds);
// we separate the passed padding option into two parts, the part that does not affect the map's center
// (lateral and vertical padding), and the part that does (paddingOffset). We add the padding offset
// to the options `offset` object where it can alter the map's center in the subsequent calls to
// `easeTo` and `flyTo`.
const paddingOffset = [options.padding.left - options.padding.right, options.padding.top - options.padding.bottom],
lateralPadding = Math.min(options.padding.right, options.padding.left),
verticalPadding = Math.min(options.padding.top, options.padding.bottom);
options.offset = [options.offset[0] + paddingOffset[0], options.offset[1] + paddingOffset[1]];
options.bearing = options.bearing || this.getBearing();
const offset = mapboxgl.Point.convert(options.offset),
tr = this.transform,
nw = tr.project(bounds.getNorthWest()),
se = tr.project(bounds.getSouthEast()),
size = se.sub(nw);
/** START CUSTOM ROTATION HACK **/
// Now using a "cropped rectangle" rotation method
// https://stackoverflow.com/questions/33866535/how-to-scale-a-rotated-rectangle-to-always-fit-another-rectangle
// W = w·|cos φ| + h·|sin φ|
// H = w·|sin φ| + h·|cos φ|
const theta = options.bearing * (Math.PI/180),
W = size.x * Math.abs( Math.cos(theta) ) + size.y * Math.abs( Math.sin(theta) ),
H = size.x * Math.abs( Math.sin(theta) ) + size.y * Math.abs( Math.cos(theta) ),
rotatedSize = {x: W, y: H},
/** END CUSTOM ROTATION HACK **/
scaleX = (tr.width - lateralPadding * 2 - Math.abs(offset.x) * 2) / rotatedSize.x,
scaleY = (tr.height - verticalPadding * 2 - Math.abs(offset.y) * 2) / rotatedSize.y;
if (scaleY < 0 || scaleX < 0) {
if (typeof console !== "undefined") console.warn('Map cannot fit within canvas with the given bounds, padding, and/or offset.');
return this;
}
options.center = tr.unproject(nw.add(se).div(2));
options.zoom = Math.min(tr.scaleZoom(tr.scale * Math.min(scaleX, scaleY)), options.maxZoom);
return options.linear ?
this.easeTo(options, eventData) :
this.flyTo(options, eventData);
}
Not sure if this could get reviewed and considered implemented. Pinging @lucaswoj just to let someone know.
Reacted by edoroshenko, phonebooks, matt wujek, Victor Gerard Temprano, vulkd, Marco Q, Arik Yavilevich, Doug McCune, Yarin Kessler and Petr Meissner
@phonebooks in my code, I have my own class Map that extends mapbox-gl/map, and than this function is included there.
Alternatively, I believe you could just have this function outside of your map, but when calling it you must bind the function to the map object so that this references will behave relative to the map itself
Activity
1ec5 commentedon Jun 26, 2015
Dupe of #1225.
kelvinabrokwa commentedon Jun 26, 2015
should be addresses in #1340
1ec5 commentedon Jul 1, 2015
Reopening because #1340 has been pared down to just keep the bearing but not account for it.
lucaswoj commentedon Jul 28, 2016
Merged with #2112
dagjomar commentedon Sep 20, 2017
Not sure what happened with this, but cannot find it in the actual mapbox-gl-js latest release.
I ended up implementing my own version of
fitBounds, calledfitBoundsRotated, which has a small part of code that transforms the box and then uses that to calculate the ending bounds, plus using the map bearing or given bearing options.Not sure if this could get reviewed and considered implemented. Pinging @lucaswoj just to let someone know.
phonebooks commentedon Apr 5, 2018
Dagjomar - how do I get this function to work in my JS program ??
this is exactly what I need by the way !! Thanks
dagjomar commentedon Jun 8, 2018
@phonebooks in my code, I have my own class Map that extends mapbox-gl/map, and than this function is included there.
Alternatively, I believe you could just have this function outside of your map, but when calling it you must bind the function to the map object so that
thisreferences will behave relative to the map itselfsleep-drifter commentedon Jul 3, 2018
Would love to see this implemented. Thanks for the work around @dagjomar
waissbluth commentedon Apr 28, 2020
Was a combination of uneven padding and bearing considered in the implementation? I opened an issue about that: #9641