diff options
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/MGLMapCamera.h | 44 | ||||
-rw-r--r-- | platform/darwin/src/MGLMapCamera.mm | 33 | ||||
-rw-r--r-- | platform/darwin/src/MGLMapSnapshotter.h | 2 | ||||
-rw-r--r-- | platform/darwin/test/MGLDocumentationExampleTests.swift | 2 | ||||
-rw-r--r-- | platform/darwin/test/MGLMapCameraTests.m | 49 |
5 files changed, 124 insertions, 6 deletions
diff --git a/platform/darwin/src/MGLMapCamera.h b/platform/darwin/src/MGLMapCamera.h index 7ce5927d1d..f02beb731d 100644 --- a/platform/darwin/src/MGLMapCamera.h +++ b/platform/darwin/src/MGLMapCamera.h @@ -49,7 +49,11 @@ MGL_EXPORT /** Returns a new camera with the given distance, pitch, and heading. - + + This method interprets the distance as a straight-line distance from the + viewpoint to the center coordinate. To specify the altitude of the viewpoint, + use the `-cameraLookingAtCenterCoordinate:altitude:pitch:heading:` method. + @param centerCoordinate The geographic coordinate on which the map should be centered. @param distance The straight-line distance from the viewpoint to the @@ -63,11 +67,47 @@ MGL_EXPORT The value `180` means the top of the map points due south, and so on. */ + (instancetype)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate - fromDistance:(CLLocationDistance)distance + acrossDistance:(CLLocationDistance)distance + pitch:(CGFloat)pitch + heading:(CLLocationDirection)heading; + +/** + Returns a new camera with the given altitude, pitch, and heading. + + @param centerCoordinate The geographic coordinate on which the map should be + centered. + @param altitude The altitude (measured in meters) above the map at which the + camera should be situated. The altitude may be less than the distance from + the camera’s viewpoint to the camera’s focus point. + @param pitch The viewing angle of the camera, measured in degrees. A value of + `0` results in a camera pointed straight down at the map. Angles greater + than `0` result in a camera angled toward the horizon. + @param heading The camera’s heading, measured in degrees clockwise from true + north. A value of `0` means that the top edge of the map view corresponds to + true north. The value `90` means the top of the map is pointing due east. + The value `180` means the top of the map points due south, and so on. + */ ++ (instancetype)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate + altitude:(CLLocationDistance)altitude pitch:(CGFloat)pitch heading:(CLLocationDirection)heading; /** + @note This initializer incorrectly interprets the `distance` parameter. To + specify the straight-line distance from the viewpoint to `centerCoordinate`, + use the `-cameraLookingAtCenterCoordinate:acrossDistance:pitch:heading:` + method. To specify the altitude of the viewpoint, use the + `-cameraLookingAtCenterCoordinate:altitude:pitch:heading:` method, which has + the same behavior as this initializer. + */ ++ (instancetype)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate + fromDistance:(CLLocationDistance)distance + pitch:(CGFloat)pitch + heading:(CLLocationDirection)heading +__attribute__((deprecated("Use -cameraLookingAtCenterCoordinate:acrossDistance:pitch:heading: " + "or -cameraLookingAtCenterCoordinate:altitude:pitch:heading:."))); + +/** Returns a Boolean value indicating whether the given camera is functionally equivalent to the receiver. diff --git a/platform/darwin/src/MGLMapCamera.mm b/platform/darwin/src/MGLMapCamera.mm index 1611dbf4a3..5ab0bffaa6 100644 --- a/platform/darwin/src/MGLMapCamera.mm +++ b/platform/darwin/src/MGLMapCamera.mm @@ -47,16 +47,45 @@ BOOL MGLEqualFloatWithAccuracy(CGFloat left, CGFloat right, CGFloat accuracy) } + (instancetype)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate - fromDistance:(CLLocationDistance)distance + acrossDistance:(CLLocationDistance)distance + pitch:(CGFloat)pitch + heading:(CLLocationDirection)heading +{ + // Angle at the viewpoint formed by the straight lines running perpendicular + // to the ground and toward the center coordinate. + CLLocationDirection eyeAngle = 90 - pitch; + CLLocationDistance altitude = distance * sin(MGLRadiansFromDegrees(eyeAngle)); + + return [[self alloc] initWithCenterCoordinate:centerCoordinate + altitude:altitude + pitch:pitch + heading:heading]; +} + ++ (instancetype)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate + altitude:(CLLocationDistance)altitude pitch:(CGFloat)pitch heading:(CLLocationDirection)heading { return [[self alloc] initWithCenterCoordinate:centerCoordinate - altitude:distance + altitude:altitude pitch:pitch heading:heading]; } ++ (instancetype)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate + fromDistance:(CLLocationDistance)distance + pitch:(CGFloat)pitch + heading:(CLLocationDirection)heading +{ + // TODO: Remove this compatibility shim in favor of `-cameraLookingAtCenterCoordinate:acrossDistance:pitch:heading:. + // https://github.com/mapbox/mapbox-gl-native/issues/12943 + return [MGLMapCamera cameraLookingAtCenterCoordinate:centerCoordinate + altitude:distance + pitch:pitch + heading:heading]; +} + - (instancetype)initWithCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate altitude:(CLLocationDistance)altitude pitch:(CGFloat)pitch diff --git a/platform/darwin/src/MGLMapSnapshotter.h b/platform/darwin/src/MGLMapSnapshotter.h index a5ddcac0ac..8281e2a084 100644 --- a/platform/darwin/src/MGLMapSnapshotter.h +++ b/platform/darwin/src/MGLMapSnapshotter.h @@ -149,7 +149,7 @@ typedef void (^MGLMapSnapshotCompletionHandler)(MGLMapSnapshot* _Nullable snapsh ### Example ```swift - let camera = MGLMapCamera(lookingAtCenter: CLLocationCoordinate2D(latitude: 37.7184, longitude: -122.4365), fromDistance: 100, pitch: 20, heading: 0) + let camera = MGLMapCamera(lookingAtCenter: CLLocationCoordinate2D(latitude: 37.7184, longitude: -122.4365), altitude: 100, pitch: 20, heading: 0) let options = MGLMapSnapshotOptions(styleURL: MGLStyle.satelliteStreetsStyleURL, camera: camera, size: CGSize(width: 320, height: 480)) options.zoomLevel = 10 diff --git a/platform/darwin/test/MGLDocumentationExampleTests.swift b/platform/darwin/test/MGLDocumentationExampleTests.swift index dd34b25291..a8f92dd528 100644 --- a/platform/darwin/test/MGLDocumentationExampleTests.swift +++ b/platform/darwin/test/MGLDocumentationExampleTests.swift @@ -376,7 +376,7 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate { } //#-example-code - let camera = MGLMapCamera(lookingAtCenter: CLLocationCoordinate2D(latitude: 37.7184, longitude: -122.4365), fromDistance: 100, pitch: 20, heading: 0) + let camera = MGLMapCamera(lookingAtCenter: CLLocationCoordinate2D(latitude: 37.7184, longitude: -122.4365), altitude: 100, pitch: 20, heading: 0) let options = MGLMapSnapshotOptions(styleURL: MGLStyle.satelliteStreetsStyleURL, camera: camera, size: CGSize(width: 320, height: 480)) options.zoomLevel = 10 diff --git a/platform/darwin/test/MGLMapCameraTests.m b/platform/darwin/test/MGLMapCameraTests.m new file mode 100644 index 0000000000..d40a336922 --- /dev/null +++ b/platform/darwin/test/MGLMapCameraTests.m @@ -0,0 +1,49 @@ +#import <XCTest/XCTest.h> +#import <CoreLocation/CoreLocation.h> +#import <Mapbox/Mapbox.h> +#import <MapKit/MapKit.h> + +@interface MGLMapCameraTests : XCTestCase + +@end + +@implementation MGLMapCameraTests + +- (void)testViewingDistance { + CLLocationCoordinate2D fountainSquare = CLLocationCoordinate2DMake(39.10152215, -84.5124439696089); + MGLMapCamera *camera = [MGLMapCamera cameraLookingAtCenterCoordinate:fountainSquare + acrossDistance:10000 + pitch:0 + heading:0]; + MKMapCamera *mkCamera = [MKMapCamera cameraLookingAtCenterCoordinate:fountainSquare + fromDistance:10000 + pitch:0 + heading:0]; + XCTAssertEqualWithAccuracy(camera.altitude, 10000, 0.01, @"Untilted camera should use distance verbatim."); + XCTAssertEqualWithAccuracy(camera.altitude, mkCamera.altitude, 0.01, @"Untilted camera altitude should match MapKit."); + + camera = [MGLMapCamera cameraLookingAtCenterCoordinate:fountainSquare + altitude:10000 + pitch:0 + heading:0]; + XCTAssertEqual(camera.altitude, 10000, @"Untilted camera should use altitude verbatim."); + + camera = [MGLMapCamera cameraLookingAtCenterCoordinate:fountainSquare + acrossDistance:10000 + pitch:60 + heading:0]; + mkCamera = [MKMapCamera cameraLookingAtCenterCoordinate:fountainSquare + fromDistance:10000 + pitch:60 + heading:0]; + XCTAssertEqualWithAccuracy(camera.altitude, 5000, 0.01, @"Tilted camera altitude should account for pitch."); + XCTAssertEqualWithAccuracy(camera.altitude, mkCamera.altitude, 0.01, @"Tilted camera altitude should match MapKit."); + + camera = [MGLMapCamera cameraLookingAtCenterCoordinate:fountainSquare + altitude:10000 + pitch:60 + heading:0]; + XCTAssertEqual(camera.altitude, 10000, @"Tilted camera should use altitude verbatim."); +} + +@end |