-
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
Changes from 9 commits
2bfe756
ffe1090
f90fc9c
40f0ecd
90da31c
ca6a439
10f2149
7d70066
47fbf44
bc3d085
b4c496d
4d94596
c6222f9
f6b0745
f34ed88
fdb5085
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -343,7 +343,6 @@ util.extend(Camera.prototype, /** @lends Map.prototype */{ | |
* @returns {Map} `this` | ||
*/ | ||
fitBounds: function(bounds, options) { | ||
|
||
options = util.extend({ | ||
padding: 0, | ||
offset: [0, 0], | ||
|
@@ -352,21 +351,67 @@ util.extend(Camera.prototype, /** @lends Map.prototype */{ | |
|
||
bounds = LatLngBounds.convert(bounds); | ||
|
||
var offset = Point.convert(options.offset), | ||
tr = this.transform, | ||
nw = tr.project(bounds.getNorthWest()), | ||
var offset = Point.convert(options.offset); | ||
|
||
var tr = this.transform; | ||
|
||
var 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; | ||
ne = tr.project(bounds.getNorthEast()), | ||
sw = tr.project(bounds.getSouthWest()); | ||
|
||
var segment = [ nw, sw, se, ne ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The “segment” terminology in gl-native was unfortunate; I’m going to rename it to “points” or somesuch. |
||
|
||
var nePixel = { | ||
x: -Infinity, | ||
y: -Infinity | ||
}; | ||
var swPixel = { | ||
x: Infinity, | ||
y: Infinity | ||
}; | ||
|
||
for (var i in segment) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always iterate over arrays with a plain |
||
var pixel = segment[i]; | ||
sw.x = Math.min(swPixel.x, pixel.x); | ||
ne.x = Math.max(nePixel.x, pixel.x); | ||
sw.y = Math.min(swPixel.y, pixel.y); | ||
ne.y = Math.max(nePixel.y, pixel.y); | ||
} | ||
|
||
var size = [ | ||
ne.x - sw.x, | ||
ne.y - sw.y | ||
]; | ||
|
||
// zoom | ||
var scaleX = (tr.width - options.padding * 2 - Math.abs(offset.x) * 2) / size.x; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You create |
||
var scaleY = (tr.height - options.padding * 2 - Math.abs(offset.y) * 2) / size.y; | ||
var minScale = Math.min(scaleX, scaleY); | ||
var zoom = Math.min(tr.scaleZoom(tr.scale * minScale), options.maxZoom); | ||
|
||
// center | ||
var paddedNEPixel = { | ||
x: ne.x + options.padding / minScale, | ||
y: ne.y + options.padding / minScale | ||
}; | ||
|
||
var paddedSWPixel = { | ||
x: sw.x - options.padding / minScale, | ||
y: sw.y - options.padding / minScale | ||
}; | ||
|
||
var centerPixel = { | ||
x: (paddedNEPixel.x + paddedSWPixel.x) / 2, | ||
y: (paddedNEPixel.y + paddedSWPixel.y) / 2 | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use var centerPixel = paddedNEPixel.add(paddedSWPixel).div(2); |
||
var center = tr.unproject(centerPixel); | ||
|
||
options.zoom = zoom; | ||
options.center = center; | ||
|
||
options.center = tr.unproject(nw.add(se).div(2)); | ||
options.zoom = Math.min(tr.scaleZoom(tr.scale * Math.min(scaleX, scaleY)), options.maxZoom); | ||
options.bearing = 0; | ||
return options.linear ? this.easeTo(options) : this.flyTo(options); | ||
|
||
return options.linear ? | ||
this.easeTo(options) : | ||
this.flyTo(options); | ||
}, | ||
|
||
/** | ||
|
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.