From 4286d5869c9af0513298b8b31d36744a3243bff4 Mon Sep 17 00:00:00 2001 From: Bobby Sudekum Date: Mon, 2 Jul 2018 16:30:18 -0700 Subject: [PATCH] Add pitch argument to cameraThatFits functions (#12213) --- include/mbgl/map/map.hpp | 6 +++--- platform/ios/CHANGELOG.md | 4 ++++ platform/ios/src/MGLMapView.h | 31 ++++++++++++++++++++++++++ platform/ios/src/MGLMapView.mm | 31 +++++++++++++++++++++++--- platform/macos/CHANGELOG.md | 4 ++++ platform/macos/src/MGLMapView.h | 31 ++++++++++++++++++++++++++ platform/macos/src/MGLMapView.mm | 26 ++++++++++++++++++++++ src/mbgl/map/map.cpp | 37 +++++++++++++++++++++----------- test/map/map.test.cpp | 32 +++++++++++++++++++++++++-- 9 files changed, 181 insertions(+), 21 deletions(-) diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 5ba23a76d..3aee93207 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -67,9 +67,9 @@ public: void jumpTo(const CameraOptions&); void easeTo(const CameraOptions&, const AnimationOptions&); void flyTo(const CameraOptions&, const AnimationOptions&); - CameraOptions cameraForLatLngBounds(const LatLngBounds&, const EdgeInsets&, optional bearing = {}) const; - CameraOptions cameraForLatLngs(const std::vector&, const EdgeInsets&, optional bearing = {}) const; - CameraOptions cameraForGeometry(const Geometry&, const EdgeInsets&, optional bearing = {}) const; + CameraOptions cameraForLatLngBounds(const LatLngBounds&, const EdgeInsets&, optional bearing = {}, optional pitch = {}) const; + CameraOptions cameraForLatLngs(const std::vector&, const EdgeInsets&, optional bearing = {}, optional pitch = {}) const; + CameraOptions cameraForGeometry(const Geometry&, const EdgeInsets&, optional bearing = {}, optional pitch = {}) const; LatLngBounds latLngBoundsForCamera(const CameraOptions&) const; // Position diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index c49dc4381..9b377913d 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -2,6 +2,10 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONTRIBUTING.md](../../CONTRIBUTING.md) to get started. +# master + +* Added `-[MGLMapView camera:fittingShape:edgePadding:]` and `-[MGLMapView camera:fittingCoordinateBounds:edgePadding:]` allowing you specify the pitch and direction for the calculated camera. ([#12213](https://github.com/mapbox/mapbox-gl-native/pull/12213)) + ## 4.2.0 ### Packaging diff --git a/platform/ios/src/MGLMapView.h b/platform/ios/src/MGLMapView.h index fd0ca19b2..04a7a0631 100644 --- a/platform/ios/src/MGLMapView.h +++ b/platform/ios/src/MGLMapView.h @@ -926,6 +926,37 @@ MGL_EXPORT IB_DESIGNABLE */ - (MGLMapCamera *)cameraThatFitsCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(UIEdgeInsets)insets; +/** + Returns the camera that best fits the given coordinate bounds, with the specified camera, + optionally with some additional padding on each side. + + @param camera The camera that the return camera should adhere to. All values + on this camera will be manipulated except for pitch and direction. + @param bounds The coordinate bounds to fit to the receiver’s viewport. + @param insets The minimum padding (in screen points) that would be visible + around the returned camera object if it were set as the receiver’s camera. + @return A camera object centered on the same location as the coordinate bounds + with zoom level as high (close to the ground) as possible while still + including the entire coordinate bounds. The initial camera's pitch and + direction will be honored. + */ +- (MGLMapCamera *)camera:(MGLMapCamera *)camera fittingCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(UIEdgeInsets)insets; + +/** + Returns the camera that best fits the given shape, with the specified camera, + optionally with some additional padding on each side. + + @param camera The camera that the return camera should adhere to. All values + on this camera will be manipulated except for pitch and direction. + @param shape The shape to fit to the receiver’s viewport. + @param insets The minimum padding (in screen points) that would be visible + around the returned camera object if it were set as the receiver’s camera. + @return A camera object centered on the shape's center with zoom level as high + (close to the ground) as possible while still including the entire shape. The + initial camera's pitch and direction will be honored. + */ +- (MGLMapCamera *)camera:(MGLMapCamera *)camera fittingShape:(MGLShape *)shape edgePadding:(UIEdgeInsets)insets; + /** Returns the camera that best fits the given shape, with the specified direction, optionally with some additional padding on each side. diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 7003f2e4b..1bbda9438 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -3348,14 +3348,39 @@ public: return [self cameraForCameraOptions:cameraOptions]; } +- (MGLMapCamera *)camera:(MGLMapCamera *)camera fittingCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(UIEdgeInsets)insets +{ + mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(insets); + padding += MGLEdgeInsetsFromNSEdgeInsets(self.contentInset); + + MGLMapCamera *currentCamera = self.camera; + CGFloat pitch = camera.pitch < 0 ? currentCamera.pitch : camera.pitch; + CLLocationDirection direction = camera.heading < 0 ? currentCamera.heading : camera.heading; + + mbgl::CameraOptions cameraOptions = _mbglMap->cameraForLatLngBounds(MGLLatLngBoundsFromCoordinateBounds(bounds), padding, direction, pitch); + return [self cameraForCameraOptions:cameraOptions]; +} + +- (MGLMapCamera *)camera:(MGLMapCamera *)camera fittingShape:(MGLShape *)shape edgePadding:(UIEdgeInsets)insets { + mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(insets); + padding += MGLEdgeInsetsFromNSEdgeInsets(self.contentInset); + + MGLMapCamera *currentCamera = self.camera; + CGFloat pitch = camera.pitch < 0 ? currentCamera.pitch : camera.pitch; + CLLocationDirection direction = camera.heading < 0 ? currentCamera.heading : camera.heading; + + mbgl::CameraOptions cameraOptions = _mbglMap->cameraForGeometry([shape geometryObject], padding, direction, pitch); + + return [self cameraForCameraOptions: cameraOptions]; +} + - (MGLMapCamera *)cameraThatFitsShape:(MGLShape *)shape direction:(CLLocationDirection)direction edgePadding:(UIEdgeInsets)insets { mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(insets); padding += MGLEdgeInsetsFromNSEdgeInsets(self.contentInset); - + mbgl::CameraOptions cameraOptions = _mbglMap->cameraForGeometry([shape geometryObject], padding, direction); - + return [self cameraForCameraOptions:cameraOptions]; - } - (MGLMapCamera *)cameraForCameraOptions:(const mbgl::CameraOptions &)cameraOptions diff --git a/platform/macos/CHANGELOG.md b/platform/macos/CHANGELOG.md index 7767185c4..f084552b6 100644 --- a/platform/macos/CHANGELOG.md +++ b/platform/macos/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog for Mapbox Maps SDK for macOS +# master + +* Added `-[MGLMapView camera:fittingShape:edgePadding:]` and `-[MGLMapView camera:fittingCoordinateBounds:edgePadding:]` allowing you specify the pitch and direction for the calculated camera. ([#12213](https://github.com/mapbox/mapbox-gl-native/pull/12213)) + ## 0.7.2 - June 22, 2018 * Fixed a crash in `-[MGLStyle localizeLabelsIntoLocale:]` on macOS 10.11. ([#12123](https://github.com/mapbox/mapbox-gl-native/pull/12123)) diff --git a/platform/macos/src/MGLMapView.h b/platform/macos/src/MGLMapView.h index 824df827a..305a1348e 100644 --- a/platform/macos/src/MGLMapView.h +++ b/platform/macos/src/MGLMapView.h @@ -485,6 +485,37 @@ MGL_EXPORT IB_DESIGNABLE */ - (MGLMapCamera *)cameraThatFitsCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(NSEdgeInsets)insets; +/** + Returns the camera that best fits the given coordinate bounds, with the specified camera, + optionally with some additional padding on each side. + + @param camera The camera that the return camera should adhere to. All values + on this camera will be manipulated except for pitch and direction. + @param bounds The coordinate bounds to fit to the receiver’s viewport. + @param insets The minimum padding (in screen points) that would be visible + around the returned camera object if it were set as the receiver’s camera. + @return A camera object centered on the same location as the coordinate bounds + with zoom level as high (close to the ground) as possible while still + including the entire coordinate bounds. The initial camera's pitch and + direction will be honored. + */ +- (MGLMapCamera *)camera:(MGLMapCamera *)camera fittingCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(NSEdgeInsets)insets; + +/** + Returns the camera that best fits the given shape, with the specified camera, + optionally with some additional padding on each side. + + @param camera The camera that the return camera should adhere to. All values + on this camera will be manipulated except for pitch and direction. + @param shape The shape to fit to the receiver’s viewport. + @param insets The minimum padding (in screen points) that would be visible + around the returned camera object if it were set as the receiver’s camera. + @return A camera object centered on the shape's center with zoom level as high + (close to the ground) as possible while still including the entire shape. The + initial camera's pitch and direction will be honored. + */ +- (MGLMapCamera *)camera:(MGLMapCamera *)camera fittingShape:(MGLShape *)shape edgePadding:(NSEdgeInsets)insets; + /** Returns the camera that best fits the given shape, with the specified direction, optionally with some additional padding on each side. diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index bc9ac1e64..154b71637 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -1275,6 +1275,32 @@ public: return [self cameraForCameraOptions:cameraOptions]; } +- (MGLMapCamera *)camera:(MGLMapCamera *)camera fittingCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(NSEdgeInsets)insets +{ + mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(insets); + padding += MGLEdgeInsetsFromNSEdgeInsets(self.contentInsets); + + MGLMapCamera *currentCamera = self.camera; + CGFloat pitch = camera.pitch < 0 ? currentCamera.pitch : camera.pitch; + CLLocationDirection direction = camera.heading < 0 ? currentCamera.heading : camera.heading; + + mbgl::CameraOptions cameraOptions = _mbglMap->cameraForLatLngBounds(MGLLatLngBoundsFromCoordinateBounds(bounds), padding, direction, pitch); + return [self cameraForCameraOptions:cameraOptions]; +} + +- (MGLMapCamera *)camera:(MGLMapCamera *)camera fittingShape:(MGLShape *)shape edgePadding:(NSEdgeInsets)insets { + mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(insets); + padding += MGLEdgeInsetsFromNSEdgeInsets(self.contentInsets); + + MGLMapCamera *currentCamera = self.camera; + CGFloat pitch = camera.pitch < 0 ? currentCamera.pitch : camera.pitch; + CLLocationDirection direction = camera.heading < 0 ? currentCamera.heading : camera.heading; + + mbgl::CameraOptions cameraOptions = _mbglMap->cameraForGeometry([shape geometryObject], padding, direction, pitch); + + return [self cameraForCameraOptions: cameraOptions]; +} + - (MGLMapCamera *)cameraThatFitsShape:(MGLShape *)shape direction:(CLLocationDirection)direction edgePadding:(NSEdgeInsets)insets { mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(insets); padding += MGLEdgeInsetsFromNSEdgeInsets(self.contentInsets); diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index ea35560e9..c17729948 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -364,13 +364,13 @@ void Map::setLatLngZoom(const LatLng& latLng, double zoom, const EdgeInsets& pad impl->onUpdate(); } -CameraOptions Map::cameraForLatLngBounds(const LatLngBounds& bounds, const EdgeInsets& padding, optional bearing) const { +CameraOptions Map::cameraForLatLngBounds(const LatLngBounds& bounds, const EdgeInsets& padding, optional bearing, optional pitch) const { return cameraForLatLngs({ bounds.northwest(), bounds.southwest(), bounds.southeast(), bounds.northeast(), - }, padding, bearing); + }, padding, bearing, pitch); } CameraOptions cameraForLatLngs(const std::vector& latLngs, const Transform& transform, const EdgeInsets& padding) { @@ -426,26 +426,37 @@ CameraOptions cameraForLatLngs(const std::vector& latLngs, const Transfo return options; } -CameraOptions Map::cameraForLatLngs(const std::vector& latLngs, const EdgeInsets& padding, optional bearing) const { - if(bearing) { - double angle = -*bearing * util::DEG2RAD; // Convert to radians - Transform transform(impl->transform.getState()); - transform.setAngle(angle); - CameraOptions options = mbgl::cameraForLatLngs(latLngs, transform, padding); - options.angle = angle; - return options; - } else { +CameraOptions Map::cameraForLatLngs(const std::vector& latLngs, const EdgeInsets& padding, optional bearing, optional pitch) const { + + if (!bearing && !pitch) { return mbgl::cameraForLatLngs(latLngs, impl->transform, padding); } + + Transform transform(impl->transform.getState()); + + if (bearing) { + double angle = -*bearing * util::DEG2RAD; // Convert to radians + transform.setAngle(angle); + } + if (pitch) { + double pitchAsRadian = *pitch * util::DEG2RAD; // Convert to radians + transform.setPitch(pitchAsRadian); + } + + CameraOptions options = mbgl::cameraForLatLngs(latLngs, transform, padding); + options.angle = transform.getAngle(); + options.pitch = transform.getPitch(); + + return options; } -CameraOptions Map::cameraForGeometry(const Geometry& geometry, const EdgeInsets& padding, optional bearing) const { +CameraOptions Map::cameraForGeometry(const Geometry& geometry, const EdgeInsets& padding, optional bearing, optional pitch) const { std::vector latLngs; forEachPoint(geometry, [&](const Point& pt) { latLngs.push_back({ pt.y, pt.x }); }); - return cameraForLatLngs(latLngs, padding, bearing); + return cameraForLatLngs(latLngs, padding, bearing, pitch); } LatLngBounds Map::latLngBoundsForCamera(const CameraOptions& camera) const { diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index f95e26fd8..4f7bd2df7 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -84,7 +84,21 @@ TEST(Map, LatLngBoundsToCameraWithAngle) { CameraOptions virtualCamera = test.map.cameraForLatLngBounds(bounds, {}, 35); ASSERT_TRUE(bounds.contains(*virtualCamera.center)); EXPECT_NEAR(*virtualCamera.zoom, 1.21385, 1e-5); - EXPECT_DOUBLE_EQ(virtualCamera.angle.value_or(0), -35 * util::DEG2RAD); + EXPECT_NEAR(virtualCamera.angle.value_or(0), -35 * util::DEG2RAD, 1e-5); +} + +TEST(Map, LatLngBoundsToCameraWithAngleAndPitch) { + MapTest<> test; + + test.map.setLatLngZoom({ 40.712730, -74.005953 }, 16.0); + + LatLngBounds bounds = LatLngBounds::hull({15.68169,73.499857}, {53.560711, 134.77281}); + + CameraOptions virtualCamera = test.map.cameraForLatLngBounds(bounds, {}, 35, 20); + ASSERT_TRUE(bounds.contains(*virtualCamera.center)); + EXPECT_NEAR(*virtualCamera.zoom, 13.66272, 1e-5); + ASSERT_DOUBLE_EQ(*virtualCamera.pitch, 20 * util::DEG2RAD); + EXPECT_NEAR(virtualCamera.angle.value_or(0), -35 * util::DEG2RAD, 1e-5); } TEST(Map, LatLngsToCamera) { @@ -93,12 +107,26 @@ TEST(Map, LatLngsToCamera) { std::vector 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.angle.value_or(0), -23 * util::DEG2RAD, 1e-5); 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, LatLngsToCameraWithAngleAndPitch) { + MapTest<> test; + + std::vector latLngs{{ 40.712730, 74.005953 }, {15.68169,73.499857}, {30.82678, 83.4082}}; + + CameraOptions virtualCamera = test.map.cameraForLatLngs(latLngs, {}, 23, 20); + EXPECT_NEAR(virtualCamera.angle.value_or(0), -23 * util::DEG2RAD, 1e-5); + EXPECT_NEAR(virtualCamera.zoom.value_or(0), 3.04378, 1e-5); + EXPECT_NEAR(virtualCamera.center->latitude(), 28.53718, 1e-5); + EXPECT_NEAR(virtualCamera.center->longitude(), 74.31746, 1e-5); + ASSERT_DOUBLE_EQ(*virtualCamera.pitch, 20 * util::DEG2RAD); +} + + TEST(Map, CameraToLatLngBounds) { MapTest<> test;