Open
Description
mapbox-gl-js version: 3.12
browser: not important
When providing center to map.fitBounds
it doesn't' work as expected - that is showing the bounds inside view but with camera at center
I need to workaround it like that:
if (center) {
let maxDistanceFromCenter = 0;
poisToFit.forEach(poi => {
const distance = getDistanceFrom(poi, { latitude: center[1], longitude: center[0] });
if (distance > maxDistanceFromCenter) {
maxDistanceFromCenter = distance;
}
});
const centerPoint = turf.point(center);
const directions = [0, 90, 180, 270]; // North, East, South, West
const bounds = new mapboxgl.LngLatBounds([center[0], center[1]], [center[0], center[1]]);
directions.forEach(bearing => {
const dest = turf.destination(centerPoint, maxDistanceFromCenter, bearing, { units: "meters" });
const [lng, lat] = dest.geometry.coordinates;
bounds.extend([lng, lat]);
});
const options: mapboxgl.EasingOptions = defined({
padding,
animate: true,
duration: 1000,
});
map.fitBounds(bounds, options);
}
While I would prefer to have something like this:
map.fitBounds(bounds, {
padding,
animate: true,
duration: 1000,
center: center ? { lng: center[0], lat: center[1] } : undefined,
});
I also spotted that this api doesn't work well when passing undefined so that's why use of defined
by me (removes undefined values from object)