diff options
Diffstat (limited to 'platform/darwin/src/MGLMapSnapshotter.mm')
-rw-r--r-- | platform/darwin/src/MGLMapSnapshotter.mm | 87 |
1 files changed, 82 insertions, 5 deletions
diff --git a/platform/darwin/src/MGLMapSnapshotter.mm b/platform/darwin/src/MGLMapSnapshotter.mm index 835e1995f3..9d32ecf72c 100644 --- a/platform/darwin/src/MGLMapSnapshotter.mm +++ b/platform/darwin/src/MGLMapSnapshotter.mm @@ -26,7 +26,7 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; @implementation MGLMapSnapshotOptions -- (instancetype _Nonnull)initWithStyleURL:(nullable NSURL*)styleURL camera:(MGLMapCamera*)camera size:(CGSize) size; +- (instancetype _Nonnull)initWithStyleURL:(nullable NSURL *)styleURL camera:(MGLMapCamera *)camera size:(CGSize) size { self = [super init]; if (self) { @@ -60,7 +60,7 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; std::unique_ptr<mbgl::Actor<mbgl::MapSnapshotter::Callback>> _snapshotCallback; } -- (instancetype)initWithOptions:(MGLMapSnapshotOptions*)options; +- (instancetype)initWithOptions:(MGLMapSnapshotOptions *)options { self = [super init]; if (self) { @@ -102,12 +102,12 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; return self; } -- (void)startWithCompletionHandler:(MGLMapSnapshotCompletionHandler)completion; +- (void)startWithCompletionHandler:(MGLMapSnapshotCompletionHandler)completion { [self startWithQueue:dispatch_get_main_queue() completionHandler:completion]; } -- (void)startWithQueue:(dispatch_queue_t)queue completionHandler:(MGLMapSnapshotCompletionHandler)completion; +- (void)startWithQueue:(dispatch_queue_t)queue completionHandler:(MGLMapSnapshotCompletionHandler)completion { if ([self isLoading]) { [NSException raise:NSInternalInconsistencyException @@ -180,10 +180,87 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; }); } -- (void)cancel; +- (void)cancel { _snapshotCallback.reset(); _mbglMapSnapshotter.reset(); } +- (NSURL *)styleURL +{ + NSString *styleURLString = @(_mbglMapSnapshotter->getStyleURL().c_str()); + return styleURLString && styleURLString.length > 0 ? [NSURL URLWithString:styleURLString] : nil; +} + +- (void)setStyleURL:(NSURL *)url +{ + _mbglMapSnapshotter->setStyleURL(std::string([url.absoluteString UTF8String])); +} + +- (CGSize)size +{ + mbgl::Size size = _mbglMapSnapshotter->getSize(); + return CGSizeMake(size.width, size.height); +} + +- (void)setSize:(CGSize)size +{ + _mbglMapSnapshotter->setSize({ + static_cast<uint32_t>(MAX(size.width, MGLSnapshotterMinimumPixelSize)), + static_cast<uint32_t>(MAX(size.height, MGLSnapshotterMinimumPixelSize)) + }); +} + +- (MGLMapCamera *)camera +{ + mbgl::CameraOptions cameraOptions = _mbglMapSnapshotter->getCameraOptions(); + CGFloat pitch = *cameraOptions.pitch; + CLLocationDirection heading = mbgl::util::wrap(*cameraOptions.angle, 0., 360.); + CLLocationDistance distance = MGLAltitudeForZoomLevel(*cameraOptions.zoom, pitch, cameraOptions.center->latitude(), [self size]); + return [MGLMapCamera cameraLookingAtCenterCoordinate:MGLLocationCoordinate2DFromLatLng(*cameraOptions.center) + fromDistance:distance + pitch:pitch + heading:heading]; +} + +- (void)setCamera:(MGLMapCamera *)camera +{ + mbgl::CameraOptions cameraOptions; + CLLocationCoordinate2D center; + if (CLLocationCoordinate2DIsValid(camera.centerCoordinate)) { + cameraOptions.center = MGLLatLngFromLocationCoordinate2D(camera.centerCoordinate); + center = camera.centerCoordinate; + } else { + // Center is optional, but always set. + center = MGLLocationCoordinate2DFromLatLng(*_mbglMapSnapshotter->getCameraOptions().center); + } + + cameraOptions.angle = MAX(0, camera.heading) * mbgl::util::DEG2RAD; + cameraOptions.zoom = MAX(0, MGLZoomLevelForAltitude(camera.altitude, camera.pitch, center.latitude, [self size])); + cameraOptions.pitch = MAX(0, camera.pitch); +} + +- (double)zoomLevel +{ + mbgl::CameraOptions cameraOptions = _mbglMapSnapshotter->getCameraOptions(); + return MGLAltitudeForZoomLevel(*cameraOptions.zoom, *cameraOptions.pitch, cameraOptions.center->latitude(), [self size]); +} + +- (void)setZoomLevel:(double)zoomLevel +{ + mbgl::CameraOptions cameraOptions = _mbglMapSnapshotter->getCameraOptions(); + cameraOptions.zoom = zoomLevel; + _mbglMapSnapshotter->setCameraOptions(cameraOptions); +} + +- (MGLCoordinateBounds)coordinateBounds +{ + return MGLCoordinateBoundsFromLatLngBounds(_mbglMapSnapshotter->getRegion()); +} + +- (void)setCoordinateBounds:(MGLCoordinateBounds)coordinateBounds +{ + _mbglMapSnapshotter->setRegion(MGLLatLngBoundsFromCoordinateBounds(coordinateBounds)); +} + @end |