diff options
author | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2017-10-02 13:42:26 -0700 |
---|---|---|
committer | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2017-10-02 15:10:13 -0700 |
commit | 83367f6537d718954d0d0f7b20d7c06377a2cd29 (patch) | |
tree | 2c5ca0dfc320d94ecf6c178faef0cb90595a3685 | |
parent | 076af14b905a99f207db459fa9c7bbf826773082 (diff) | |
download | qtlocation-mapboxgl-83367f6537d718954d0d0f7b20d7c06377a2cd29.tar.gz |
[core] Add `cameraForLatLngs()` method with modified bearing
-rw-r--r-- | include/mbgl/map/map.hpp | 8 | ||||
-rw-r--r-- | src/mbgl/map/map.cpp | 31 | ||||
-rw-r--r-- | test/map/map.test.cpp | 12 |
3 files changed, 39 insertions, 12 deletions
diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 4108725776..3e1f090665 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -66,6 +66,11 @@ public: void jumpTo(const CameraOptions&); void easeTo(const CameraOptions&, const AnimationOptions&); void flyTo(const CameraOptions&, const AnimationOptions&); + CameraOptions cameraForLatLngBounds(const LatLngBounds&, const EdgeInsets&) const; + CameraOptions cameraForLatLngs(const std::vector<LatLng>&, const EdgeInsets&) const; + CameraOptions cameraForLatLngs(const std::vector<LatLng>&, double heading, const EdgeInsets&) const; + + LatLngBounds latLngBoundsForCamera(const CameraOptions&) const; // Position void moveBy(const ScreenCoordinate&, const AnimationOptions& = {}); @@ -82,9 +87,6 @@ public: double getZoom() const; void setLatLngZoom(const LatLng&, double zoom, const AnimationOptions& = {}); void setLatLngZoom(const LatLng&, double zoom, const EdgeInsets&, const AnimationOptions& = {}); - CameraOptions cameraForLatLngBounds(const LatLngBounds&, const EdgeInsets&) const; - CameraOptions cameraForLatLngs(const std::vector<LatLng>&, const EdgeInsets&) const; - LatLngBounds latLngBoundsForCamera(const CameraOptions&) const; void resetZoom(); // Bounds diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 7534fe67ad..76ef4bc69f 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -373,18 +373,18 @@ CameraOptions Map::cameraForLatLngBounds(const LatLngBounds& bounds, const EdgeI }, padding); } -CameraOptions Map::cameraForLatLngs(const std::vector<LatLng>& latLngs, const EdgeInsets& padding) const { +CameraOptions cameraForLatLngs(const std::vector<LatLng>& latLngs, const Transform& transform, const EdgeInsets& padding) { CameraOptions options; if (latLngs.empty()) { return options; } - + Size size = transform.getState().getSize(); // Calculate the bounds of the possibly rotated shape with respect to the viewport. ScreenCoordinate nePixel = {-INFINITY, -INFINITY}; ScreenCoordinate swPixel = {INFINITY, INFINITY}; - double viewportHeight = getSize().height; + double viewportHeight = size.height; for (LatLng latLng : latLngs) { - ScreenCoordinate pixel = impl->transform.latLngToScreenCoordinate(latLng); + ScreenCoordinate pixel = transform.latLngToScreenCoordinate(latLng); swPixel.x = std::min(swPixel.x, pixel.x); nePixel.x = std::max(nePixel.x, pixel.x); swPixel.y = std::min(swPixel.y, viewportHeight - pixel.y); @@ -396,14 +396,14 @@ CameraOptions Map::cameraForLatLngs(const std::vector<LatLng>& latLngs, const Ed // Calculate the zoom level. double minScale = INFINITY; if (width > 0 || height > 0) { - double scaleX = double(getSize().width) / width; - double scaleY = double(getSize().height) / height; + double scaleX = double(size.width) / width; + double scaleY = double(size.height) / height; scaleX -= (padding.left() + padding.right()) / width; scaleY -= (padding.top() + padding.bottom()) / height; minScale = util::min(scaleX, scaleY); } - double zoom = getZoom() + util::log2(minScale); - zoom = util::clamp(zoom, getMinZoom(), getMaxZoom()); + double zoom = transform.getZoom() + util::log2(minScale); + zoom = util::clamp(zoom, transform.getState().getMinZoom(), transform.getState().getMaxZoom()); // Calculate the center point of a virtual bounds that is extended in all directions by padding. ScreenCoordinate centerPixel = nePixel + swPixel; @@ -421,11 +421,24 @@ CameraOptions Map::cameraForLatLngs(const std::vector<LatLng>& latLngs, const Ed // CameraOptions origin is at the top-left corner. centerPixel.y = viewportHeight - centerPixel.y; - options.center = latLngForPixel(centerPixel); + options.center = transform.screenCoordinateToLatLng(centerPixel); options.zoom = zoom; return options; } +CameraOptions Map::cameraForLatLngs(const std::vector<LatLng>& latLngs, const EdgeInsets& padding) const { + return mbgl::cameraForLatLngs(latLngs, impl->transform, padding); +} + +CameraOptions Map::cameraForLatLngs(const std::vector<LatLng>& latLngs, double heading, const EdgeInsets& padding) const { + double angle = -heading * util::DEG2RAD; + Transform transform(impl->transform.getState()); + transform.setAngle(angle); + CameraOptions options = mbgl::cameraForLatLngs(latLngs, transform, padding); + options.angle = angle; + return options; +} + LatLngBounds Map::latLngBoundsForCamera(const CameraOptions& camera) const { Transform shallow { impl->transform.getState() }; Size size = shallow.getState().getSize(); diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index 50d5e50abb..a86711925a 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -111,6 +111,18 @@ TEST(Map, LatLngBoundsToCamera) { ASSERT_TRUE(bounds.contains(*virtualCamera.center)); } +TEST(Map, LatLngsToCamera) { + MapTest<> test; + + std::vector<LatLng> latLngs{{ 40.712730, 74.005953 }, {15.68169,73.499857}, {30.82678, 83.4082}}; + + CameraOptions virtualCamera = test.map.cameraForLatLngs(latLngs, 23, {}); + EXPECT_DOUBLE_EQ(virtualCamera.angle.value_or(0), -23 * util::DEG2RAD); + EXPECT_NEAR(virtualCamera.zoom.value_or(0), 2.75434, 1e-5); + EXPECT_NEAR(virtualCamera.center->latitude(), 28.49288, 1e-5); + EXPECT_NEAR(virtualCamera.center->longitude(), 74.97437, 1e-5); +} + TEST(Map, CameraToLatLngBounds) { MapTest<> test; |