summaryrefslogtreecommitdiff
path: root/platform/darwin
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-12-05 22:22:46 -0800
committerMinh Nguyễn <mxn@1ec5.org>2015-12-13 17:26:53 -0800
commit0fb2b855a4539803f42a4048257da606f66d9f4e (patch)
tree7be98f423e47a40aa6d011c5e52751d0896125e0 /platform/darwin
parent3f2fae521cde0cc07796f65b33a227875466dc8d (diff)
downloadqtlocation-mapboxgl-0fb2b855a4539803f42a4048257da606f66d9f4e.tar.gz
[osx] Map camera
Shared MGLMapCamera between iOS and OS X. Unfortunately -camera and -setCamera: implementations need to be copy-pasted for now.
Diffstat (limited to 'platform/darwin')
-rw-r--r--platform/darwin/MGLMapCamera.mm101
1 files changed, 101 insertions, 0 deletions
diff --git a/platform/darwin/MGLMapCamera.mm b/platform/darwin/MGLMapCamera.mm
new file mode 100644
index 0000000000..d04e46fa90
--- /dev/null
+++ b/platform/darwin/MGLMapCamera.mm
@@ -0,0 +1,101 @@
+#import "MGLMapCamera.h"
+
+#include <mbgl/util/projection.hpp>
+
+@implementation MGLMapCamera
+
++ (BOOL)supportsSecureCoding
+{
+ return YES;
+}
+
++ (instancetype)camera
+{
+ return [[self alloc] init];
+}
+
++ (instancetype)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
+ fromEyeCoordinate:(CLLocationCoordinate2D)eyeCoordinate
+ eyeAltitude:(CLLocationDistance)eyeAltitude
+{
+ mbgl::LatLng centerLatLng = mbgl::LatLng(centerCoordinate.latitude, centerCoordinate.longitude);
+ mbgl::LatLng eyeLatLng = mbgl::LatLng(eyeCoordinate.latitude, eyeCoordinate.longitude);
+
+ mbgl::ProjectedMeters centerMeters = mbgl::Projection::projectedMetersForLatLng(centerLatLng);
+ mbgl::ProjectedMeters eyeMeters = mbgl::Projection::projectedMetersForLatLng(eyeLatLng);
+ CLLocationDirection heading = std::atan((centerMeters.northing - eyeMeters.northing) /
+ (centerMeters.easting - eyeMeters.easting));
+
+ double groundDistance = std::hypot(centerMeters.northing - eyeMeters.northing,
+ centerMeters.easting - eyeMeters.easting);
+ CGFloat pitch = std::atan(eyeAltitude / groundDistance);
+
+ return [[self alloc] initWithCenterCoordinate:centerCoordinate
+ altitude:eyeAltitude
+ pitch:pitch
+ heading:heading];
+}
+
++ (instancetype)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
+ fromDistance:(CLLocationDistance)distance
+ pitch:(CGFloat)pitch
+ heading:(CLLocationDirection)heading
+{
+ return [[self alloc] initWithCenterCoordinate:centerCoordinate
+ altitude:distance
+ pitch:(CGFloat)pitch
+ heading:heading];
+}
+
+- (instancetype)initWithCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
+ altitude:(CLLocationDistance)altitude
+ pitch:(CGFloat)pitch
+ heading:(CLLocationDirection)heading
+{
+ if (self = [super init])
+ {
+ _centerCoordinate = centerCoordinate;
+ _altitude = altitude;
+ _pitch = pitch;
+ _heading = heading;
+ }
+ return self;
+}
+
+- (nullable instancetype)initWithCoder:(NSCoder *)coder
+{
+ if (self = [super init])
+ {
+ _centerCoordinate = CLLocationCoordinate2DMake([coder decodeDoubleForKey:@"centerLatitude"],
+ [coder decodeDoubleForKey:@"centerLongitude"]);
+ _altitude = [coder decodeDoubleForKey:@"altitude"];
+ _pitch = [coder decodeDoubleForKey:@"pitch"];
+ _heading = [coder decodeDoubleForKey:@"heading"];
+ }
+ return self;
+}
+
+- (void)encodeWithCoder:(NSCoder *)coder
+{
+ [coder encodeDouble:_centerCoordinate.latitude forKey:@"centerLatitude"];
+ [coder encodeDouble:_centerCoordinate.longitude forKey:@"centerLongitude"];
+ [coder encodeDouble:_altitude forKey:@"altitude"];
+ [coder encodeDouble:_pitch forKey:@"pitch"];
+ [coder encodeDouble:_heading forKey:@"heading"];
+}
+
+- (id)copyWithZone:(nullable NSZone *)zone
+{
+ return [[[self class] allocWithZone:zone] initWithCenterCoordinate:_centerCoordinate
+ altitude:_altitude
+ pitch:_pitch
+ heading:_heading];
+}
+
+- (NSString *)description
+{
+ return [NSString stringWithFormat:@"<MKMapCamera %p centerCoordinate:%f, %f altitude:%.0fm heading:%.0f° pitch:%.0f°>",
+ self, _centerCoordinate.latitude, _centerCoordinate.longitude, _altitude, _heading, _pitch];
+}
+
+@end