Reimplemented fit to bounds

The new implementation is now public and takes advantage of MGLCoordinateBounds. It is re-ported from `Camera.prototype.fitBounds()` in mapbox/mapbox-gl-js to ensure correct behavior. A new function, MGLCoordinateBoundsMake(), makes it easier to create an MGLCoordinateBounds for use with this method.
This commit is contained in:
Minh Nguyễn
2015-06-23 15:20:50 -07:00
parent f5c1e2630e
commit c57799f26c
5 changed files with 62 additions and 13 deletions

View File

@@ -146,6 +146,27 @@ void Map::setLatLngZoom(LatLng latLng, double zoom, Duration duration) {
update(Update::Zoom);
}
void Map::fitBounds(LatLngBounds bounds, Duration duration) {
// Zoom level calculation below assumes no rotation.
setBearing(0);
// Calculate the center point, respecting the projection.
vec2<double> nePixel = pixelForLatLng(bounds.ne);
vec2<double> swPixel = pixelForLatLng(bounds.sw);
vec2<double> centerPixel = (nePixel + swPixel) * 0.5;
LatLng centerLatLng = latLngForPixel(centerPixel);
// Calculate the zoom level.
double scaleX = getWidth() / (nePixel.x - swPixel.x);
double scaleY = getHeight() / (nePixel.y - swPixel.y);
double minZoom = getMinZoom();
double maxZoom = getMaxZoom();
double zoom = std::log2(getScale() * std::fmin(scaleX, scaleY));
zoom = std::fmax(std::fmin(zoom, maxZoom), minZoom);
setLatLngZoom(centerLatLng, zoom, duration);
}
void Map::resetZoom() {
setZoom(0);
}