-
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
don't reset bearing in fitbounds #1340
Conversation
This maintains the current zoom but doesn’t account for rotation. For example, this call: map.fitBounds([[37, -109+2./60+48./(60*60)], [41, -102.05]], {bearing: 60}) shows most of Colorado but not all of it. (#1338 has another example.) In mapbox/mapbox-gl-native@bcbb56c for mapbox/mapbox-gl-native#1783, I corrected for this issue by fitting to a “visual bounding box” parallel to the viewport that encompasses the passed-in bounds. |
@1ec5 woops totally missed that. But doesn't box zoom always have a bunch of padding? [EDIT]: it doesn't |
It appears to have no padding: if you box-zoom on Colorado, for example, it fits perfectly when there’s no rotation but zooms in too far otherwise. |
@kelvinabrokwa Nice! Can you please add some tests for |
@jfirebaugh will do. It's not quite ready yet though. I jumped the gun on those googly eyes... |
@@ -67,7 +67,7 @@ BoxZoom.prototype = { | |||
this._finish(); | |||
|
|||
this._map | |||
.fitBounds(bounds, {linear: true}) | |||
.fitBounds(bounds, {linear: true, bearing: this._map.getBearing()}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These bounds are incorrect: if you rotate the map by 45° clockwise and box-zoom the District of Columbia, bounds.ne
is the northern corner of D.C. and bounds.sw
is the southern corner. But Camera.prototype.fitBounds()
is expecting an unrotated bounding box. So the camera ends up trying to fit to a bogus bounding box.
There are two solutions: the simpler solution is to extend bounds
by the other two corners of the zoom box. The center point would be correct, but the zoom level would be suboptimal in degenerate cases like D.C. The more complex but more correct solution is to port over the changes in mapbox/mapbox-gl-native#1795, then pass in the four corners of the zoom box rather than a LatLngBounds
.
y: Infinity | ||
}; | ||
|
||
for (var i in segment) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use forEach()
or a plain ol’ for loop in this case. In C++, the for…in syntax was cleaner than the alternative, but there isn’t much to be gained here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always iterate over arrays with a plain for
loop unless there's a strong reason not to.
ne = tr.project(bounds.getNorthEast()), | ||
sw = tr.project(bounds.getSouthWest()); | ||
|
||
var nePixelInf = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These variables only start out at ±∞; nePixel
and swPixel
are fine.
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; | ||
ne = tr.project(bounds.getNorthEast()), | ||
sw = tr.project(bounds.getSouthWest()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The calls to project()
need to take place inside the for loop below. As it is, you’re assuming that the northwest point remains the northwest point after rotation.
Something’s up with |
#1225