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

Commit

Permalink
[ios, macos] Added MGLMapCamera.viewingDistance property
Browse files Browse the repository at this point in the history
Co-authored-by: Dave Prukop <dave.prukop@mapbox.com>
  • Loading branch information
1ec5 and d-prukop committed Sep 26, 2018
1 parent a46b8ac commit 8577a32
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
17 changes: 16 additions & 1 deletion platform/darwin/src/MGLMapCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 14 additions & 0 deletions platform/darwin/src/MGLMapCamera.mm
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ - (id)copyWithZone:(nullable NSZone *)zone
heading:_heading];
}

+ (NSSet<NSString *> *)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°>",
Expand Down
20 changes: 19 additions & 1 deletion platform/darwin/test/MGLMapCameraTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions platform/macos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8577a32

Please sign in to comment.