-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
alternative maxBounds implementations #6969
Comments
I'm inclined to keep it simple. Do any other mapping libraries use this alternative definition? |
I'll see if I can experiment with the options to play with some alternatives to see if they are handy or not, even if it doesn't land that's fine.
I'm not sure actually. |
If I understand this right, then #4268 was implemented, you could achieve this by setting half the width and height as the "global padding" options. The current solution is definitely too restrictive. I think the padding option is better than this "alternative definition" option, because it allows for alternative options between the two extremes:
|
When I used For example, if I supply a bounding box for the country Italy ( I prefer the behavior for The "global padding" option @stevage mentioned above is not intuitive to me. I'm relatively new to mapbox and digital map building in general and it's not obvious how this is a viable solution. So I use Perhaps, I'm missing something that's just not intuitive yet. I'm open to better solutions or idiomatic ways to make |
I found a workaround that allows me to calculate a import bbox from '@turf/bbox'
import { bounds as viewportBounds } from '@mapbox/geo-viewport'
const countryBoundingBox = bbox(countryGeoJson)
const { center, zoom } = map.cameraForBounds(countryBoundingBox)
const dimensions = [
Math.floor(map.getCanvas().offsetWidth),
Math.floor(map.getCanvas().offsetHeight),
]
// Mapbox vector tiles are 512x512, as opposed to the library's assumed default of 256x256, hence the final `512` argument
const bounds = viewportBounds(center.toArray(), zoom, dimensions, 512) I dug into the code for I also tried using |
@tmcgann you may want to wait for the
|
maxBounds are very restricting on the zoom level. There is no way to zoom out to see the full bounds on most map display ratio's. Something like this works much better for me but it's not perfect as dragging stops while your drag operation is out of bounds: map.on('move', function(){
let center = map.getCenter()
if(!boundsobj.contains(center)){
if(center.lng > boundsobj.getEast()){
center.lng = boundsobj.getEast();
}else if(center.lng < boundsobj.getWest()){
center.lng = boundsobj.getWest();
}
if(center.lat > boundsobj.getNorth()){
center.lat = boundsobj.getNorth();
}else if(center.lat < boundsobj.getSouth()){
center.lat = boundsobj.getSouth();
}
map.stop();
map.setCenter(center);
}
}) |
I have adjusted @nondescriptpointer's solution a bit so that it works a bit more smoothly. I've used the I'm using Vue's map.on("move", (e) => {
if (e.target._easeOptions.easeId === 'bouncing') {
return;
}
let center = map.getCenter();
if (!maxBounds.contains(center)) {
if (center.lng > maxBounds.getEast()) {
center.lng = maxBounds.getEast();
} else if (center.lng < maxBounds.getWest()) {
center.lng = maxBounds.getWest();
}
if (center.lat > maxBounds.getNorth()) {
center.lat = maxBounds.getNorth();
} else if (center.lat < maxBounds.getSouth()) {
center.lat = maxBounds.getSouth();
}
map.stop();
nextTick(() => {
map.easeTo({
center: center,
duration: 500,
easeId: 'bouncing'
});
})
}
}); |
Map.maxBounds
(red box) is implemented as never show the area outside the maxBounds (blue box, must be strictly within red box) . While useful in some contexts it has limitations on zooming out, which is sometimes not desirable.An alternative approach to
maxBounds
is to ensure that at least some part of these bounds is shown in the map, potentially with a padding (green box must have some part in the red box).Another alternative is to stick with the current approach but add a padding in pixels to the maxBounds.
The text was updated successfully, but these errors were encountered: