diff --git a/platform/darwin/src/MGLMapCamera.h b/platform/darwin/src/MGLMapCamera.h index 00a6c77df66..a931c1f5cca 100644 --- a/platform/darwin/src/MGLMapCamera.h +++ b/platform/darwin/src/MGLMapCamera.h @@ -25,9 +25,24 @@ MGL_EXPORT */ @property (nonatomic) CGFloat pitch; -/** Meters above ground level. */ +/** + The altitude (measured in meters) above the map at which the camera is + situated. + + This property’s value may be less than that of the `viewingDistance` property. + Setting this property automatically updates the `viewingDistance` property + based on the `pitch` property’s current value. + */ @property (nonatomic) CLLocationDistance altitude; +/** + The straight-line distance from the viewpoint to the `centerCoordinate`. + + Setting this property automatically updates the `altitude` property based on + the `pitch` property’s current value. + */ +@property (nonatomic) CLLocationDistance viewingDistance; + /** Returns a new camera with all properties set to 0. */ + (instancetype)camera; diff --git a/platform/darwin/src/MGLMapCamera.mm b/platform/darwin/src/MGLMapCamera.mm index e9a7629d45a..a52af507a66 100644 --- a/platform/darwin/src/MGLMapCamera.mm +++ b/platform/darwin/src/MGLMapCamera.mm @@ -131,6 +131,20 @@ - (id)copyWithZone:(nullable NSZone *)zone heading:_heading]; } ++ (NSSet *)keyPathsForValuesAffectingViewingDistance { + return [NSSet setWithObjects:@"altitude", @"pitch", nil]; +} + +- (CLLocationDistance)viewingDistance { + CLLocationDirection eyeAngle = 90 - self.pitch; + return self.altitude / sin(MGLRadiansFromDegrees(eyeAngle)); +} + +- (void)setViewingDistance:(CLLocationDistance)distance { + CLLocationDirection eyeAngle = 90 - self.pitch; + self.altitude = distance * sin(MGLRadiansFromDegrees(eyeAngle)); +} + - (NSString *)description { return [NSString stringWithFormat:@"<%@: %p; centerCoordinate = %f, %f; altitude = %.0fm; heading = %.0f°; pitch = %.0f°>", diff --git a/platform/darwin/test/MGLMapCameraTests.m b/platform/darwin/test/MGLMapCameraTests.m index 30ff9cced28..7c4a76c791f 100644 --- a/platform/darwin/test/MGLMapCameraTests.m +++ b/platform/darwin/test/MGLMapCameraTests.m @@ -9,7 +9,7 @@ @interface MGLMapCameraTests : XCTestCase @implementation MGLMapCameraTests -- (void)testViewingDistance { +- (void)testViewingDistanceInitialization { CLLocationCoordinate2D fountainSquare = CLLocationCoordinate2DMake(39.10152215, -84.5124439696089); MGLMapCamera *camera = [MGLMapCamera cameraLookingAtCenterCoordinate:fountainSquare correctlyFromDistance:10000 @@ -46,4 +46,22 @@ - (void)testViewingDistance { XCTAssertEqual(camera.altitude, 10000, @"Tilted camera should use altitude verbatim."); } +- (void)testViewingDistance { + MGLMapCamera *camera = [MGLMapCamera camera]; + camera.altitude = 10000; + XCTAssertEqual(camera.altitude, 10000); + XCTAssertEqual(camera.viewingDistance, 10000); + camera.viewingDistance = 10000; + XCTAssertEqual(camera.altitude, 10000); + XCTAssertEqual(camera.viewingDistance, 10000); + + camera.pitch = 60; + camera.altitude = 10000; + XCTAssertEqual(camera.altitude, 10000); + XCTAssertEqualWithAccuracy(camera.viewingDistance, 20000, 0.01); + camera.viewingDistance = 10000; + XCTAssertEqualWithAccuracy(camera.altitude, 5000, 0.01); + XCTAssertEqual(camera.viewingDistance, 10000); +} + @end diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index 0145f9da0d2..d4ae09e52ca 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -16,6 +16,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT ### Other changes * Deprecated the `+[MGLMapCamera cameraLookingAtCenterCoordinate:fromDistance:pitch:heading:]` method in favor of `+[MGLMapCamera cameraLookingAtCenterCoordinate:altitude:pitch:heading:]` and a temporarily named `+[MGLMapCamera cameraLookingAtCenterCoordinate:correctlyFromDistance:pitch:heading:]`. ([#12966](https://github.com/mapbox/mapbox-gl-native/pull/12966)) +* Added an `MGLMapCamera.viewingDistance` property based on the existing `MGLMapCamera.altitude` property. ([#12966](https://github.com/mapbox/mapbox-gl-native/pull/12966)) * Fixed an issue where `-[MGLMapSnapshotter startWithQueue:completionHandler:]` failed to call its completion handler in some cases. ([#12355](https://github.com/mapbox/mapbox-gl-native/pull/12355)) * Fixed bugs in coercion expression operators ("to-array" applied to empty arrays, "to-color" applied to colors, and "to-number" applied to null) [#12864](https://github.com/mapbox/mapbox-gl-native/pull/12864) diff --git a/platform/macos/CHANGELOG.md b/platform/macos/CHANGELOG.md index ff04b053ad4..ecec3b2310e 100644 --- a/platform/macos/CHANGELOG.md +++ b/platform/macos/CHANGELOG.md @@ -14,6 +14,7 @@ ### Other changes * Deprecated the `+[MGLMapCamera cameraLookingAtCenterCoordinate:fromDistance:pitch:heading:]` method in favor of `+[MGLMapCamera cameraLookingAtCenterCoordinate:altitude:pitch:heading:]` and a temporarily named `+[MGLMapCamera cameraLookingAtCenterCoordinate:correctlyFromDistance:pitch:heading:]`. ([#12966](https://github.com/mapbox/mapbox-gl-native/pull/12966)) +* Added an `MGLMapCamera.viewingDistance` property based on the existing `MGLMapCamera.altitude` property. ([#12966](https://github.com/mapbox/mapbox-gl-native/pull/12966)) * Fixed an issue where `-[MGLMapSnapshotter startWithQueue:completionHandler:]` failed to call its completion handler in some cases. ([#12355](https://github.com/mapbox/mapbox-gl-native/pull/12355)) * Fixed an issue where `MGLMapView` produced a designable error in Interface Builder storyboards in Xcode 10. ([#12883](https://github.com/mapbox/mapbox-gl-native/pull/12883)) * Fixed bugs in coercion expression operators ("to-array" applied to empty arrays, "to-color" applied to colors, and "to-number" applied to null) [#12864](https://github.com/mapbox/mapbox-gl-native/pull/12864)