Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Added Map::{get,set}LatLngBounds
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Apr 4, 2017
1 parent 694a12f commit 372df88
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class Map : private util::noncopyable {
void resetZoom();

// Bounds
void setLatLngBounds(const LatLngBounds&);
LatLngBounds getLatLngBounds() const;
void setMinZoom(double);
double getMinZoom() const;
void setMaxZoom(double);
Expand Down
9 changes: 9 additions & 0 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,15 @@ void Map::resetZoom() {

#pragma mark - Bounds

LatLngBounds Map::getLatLngBounds() const {
return impl->transform.getState().getLatLngBounds();
}

void Map::setLatLngBounds(const LatLngBounds& bounds) {
impl->cameraMutated = true;
impl->transform.setLatLngBounds(bounds);
impl->onUpdate(Update::Repaint);
}

void Map::setMinZoom(const double minZoom) {
impl->transform.setMinZoom(minZoom);
Expand Down
6 changes: 6 additions & 0 deletions src/mbgl/map/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,12 @@ void Transform::setScale(double scale, optional<EdgeInsets> padding, const Anima

#pragma mark - Bounds

void Transform::setLatLngBounds(const LatLngBounds& bounds) {
if (bounds.valid()) {
state.setLatLngBounds(bounds);
}
}

void Transform::setMinZoom(const double minZoom) {
if (std::isnan(minZoom)) return;
state.setMinZoom(minZoom);
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/map/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Transform : private util::noncopyable {

// Bounds

void setLatLngBounds(const LatLngBounds&);
void setMinZoom(double);
void setMaxZoom(double);

Expand Down
15 changes: 13 additions & 2 deletions src/mbgl/map/transform_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ double TransformState::getScale() const {

#pragma mark - Bounds

void TransformState::setLatLngBounds(const LatLngBounds& bounds_) {
bounds = bounds_;
setLatLngZoom(getLatLng(LatLng::Unwrapped), getZoom());
}

LatLngBounds TransformState::getLatLngBounds() const {
return bounds;
}

void TransformState::setMinZoom(const double minZoom) {
if (minZoom <= getMaxZoom()) {
min_scale = zoomScale(util::clamp(minZoom, util::MIN_ZOOM, util::MAX_ZOOM));
Expand Down Expand Up @@ -321,16 +330,18 @@ void TransformState::moveLatLng(const LatLng& latLng, const ScreenCoordinate& an
}

void TransformState::setLatLngZoom(const LatLng &latLng, double zoom) {
const LatLng constrained = bounds.constrain(latLng);

double newScale = zoomScale(zoom);
const double newWorldSize = newScale * util::tileSize;
Bc = newWorldSize / util::DEGREES_MAX;
Cc = newWorldSize / util::M2PI;

const double m = 1 - 1e-15;
const double f = util::clamp(std::sin(util::DEG2RAD * latLng.latitude), -m, m);
const double f = util::clamp(std::sin(util::DEG2RAD * constrained.latitude), -m, m);

ScreenCoordinate point = {
-latLng.longitude * Bc,
-constrained.longitude * Bc,
0.5 * Cc * std::log((1 + f) / (1 - f)),
};
setScalePoint(newScale, point);
Expand Down
4 changes: 4 additions & 0 deletions src/mbgl/map/transform_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class TransformState {
double getZoomFraction() const;

// Bounds
void setLatLngBounds(const LatLngBounds&);
LatLngBounds getLatLngBounds() const;
void setMinZoom(double);
double getMinZoom() const;
void setMaxZoom(double);
Expand Down Expand Up @@ -80,6 +82,8 @@ class TransformState {
bool rotatedNorth() const;
void constrain(double& scale, double& x, double& y) const;

LatLngBounds bounds = LatLngBounds::world();

// Limit the amount of zooming possible on the map.
double min_scale = std::pow(2, 0);
double max_scale = std::pow(2, 20);
Expand Down
39 changes: 39 additions & 0 deletions test/map/transform.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,42 @@ TEST(Transform, DefaultTransform) {
ASSERT_FALSE(transform.resize({ max, max }));
testConversions(nullIsland, center);
}

TEST(Transform, LatLngBounds) {
const LatLng nullIsland {};
const LatLng sanFrancisco { 37.7749, -122.4194 };

Transform transform;
transform.resize({ 1000, 1000 });
transform.setLatLngZoom({ 0, 0 }, transform.getState().getMaxZoom());

// Default bounds.
ASSERT_EQ(transform.getState().getLatLngBounds(), LatLngBounds::world());
ASSERT_EQ(transform.getLatLng(), nullIsland);

// Invalid bounds.
transform.setLatLngBounds(LatLngBounds::empty());
ASSERT_EQ(transform.getState().getLatLngBounds(), LatLngBounds::world());

transform.setLatLng(sanFrancisco);
ASSERT_EQ(transform.getLatLng(), sanFrancisco);

// Single location.
transform.setLatLngBounds(LatLngBounds::singleton(sanFrancisco));
ASSERT_EQ(transform.getLatLng(), sanFrancisco);

transform.setLatLngBounds(LatLngBounds::hull({ -90.0, -180.0 }, { 0.0, 180.0 }));
transform.setLatLng(sanFrancisco);
ASSERT_EQ(transform.getLatLng().latitude, 0.0);
ASSERT_EQ(transform.getLatLng().longitude, sanFrancisco.longitude);

transform.setLatLngBounds(LatLngBounds::hull({ -90.0, 0.0 }, { 90.0, 180.0 }));
transform.setLatLng(sanFrancisco);
ASSERT_EQ(transform.getLatLng().latitude, sanFrancisco.latitude);
ASSERT_EQ(transform.getLatLng().longitude, 0.0);

transform.setLatLngBounds(LatLngBounds::hull({ -90.0, 0.0 }, { 0.0, 180.0 }));
transform.setLatLng(sanFrancisco);
ASSERT_EQ(transform.getLatLng().latitude, 0.0);
ASSERT_EQ(transform.getLatLng().longitude, 0.0);
}

0 comments on commit 372df88

Please sign in to comment.