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 Oct 2, 2018
1 parent c0fd6b1 commit fe5ad23
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
20 changes: 19 additions & 1 deletion platform/darwin/src/MGLMapCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,27 @@ MGL_EXPORT
*/
@property (nonatomic) CGFloat pitch;

/** Meters above ground level. */
/**
The altitude (measured in meters) above the map at which the camera is
situated.
The altitude is the distance from the viewpoint to the map, perpendicular to
the map plane. This property does not account for physical elevation.
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
acrossDistance: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

0 comments on commit fe5ad23

Please sign in to comment.