diff options
Diffstat (limited to 'platform/darwin/src/MGLMapSnapshotter.mm')
-rw-r--r-- | platform/darwin/src/MGLMapSnapshotter.mm | 138 |
1 files changed, 33 insertions, 105 deletions
diff --git a/platform/darwin/src/MGLMapSnapshotter.mm b/platform/darwin/src/MGLMapSnapshotter.mm index 8b54488814..15a8f6c8ca 100644 --- a/platform/darwin/src/MGLMapSnapshotter.mm +++ b/platform/darwin/src/MGLMapSnapshotter.mm @@ -81,7 +81,7 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; @end @interface MGLMapSnapshotter() -@property (nonatomic) MGLMapSnapshotOptions *options; + @end @implementation MGLMapSnapshotter { @@ -96,40 +96,9 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; { self = [super init]; if (self) { - _options = options; + [self setOptions:options]; _loading = false; - - mbgl::DefaultFileSource *mbglFileSource = [MGLOfflineStorage sharedOfflineStorage].mbglFileSource; - _mbglThreadPool = mbgl::sharedThreadPool(); - - std::string styleURL = std::string([options.styleURL.absoluteString UTF8String]); - - // Size; taking into account the minimum texture size for OpenGL ES - // For non retina screens the ratio is 1:1 MGLSnapshotterMinimumPixelSize - mbgl::Size size = { - static_cast<uint32_t>(MAX(options.size.width, MGLSnapshotterMinimumPixelSize)), - static_cast<uint32_t>(MAX(options.size.height, MGLSnapshotterMinimumPixelSize)) - }; - - float pixelRatio = MAX(options.scale, 1); - - // Camera options - mbgl::CameraOptions cameraOptions; - if (CLLocationCoordinate2DIsValid(options.camera.centerCoordinate)) { - cameraOptions.center = MGLLatLngFromLocationCoordinate2D(options.camera.centerCoordinate); - } - cameraOptions.angle = MAX(0, options.camera.heading) * mbgl::util::DEG2RAD; - cameraOptions.zoom = MAX(0, options.zoomLevel); - cameraOptions.pitch = MAX(0, options.camera.pitch); - - // Region - mbgl::optional<mbgl::LatLngBounds> coordinateBounds; - if (!MGLCoordinateBoundsIsEmpty(options.coordinateBounds)) { - coordinateBounds = MGLLatLngBoundsFromCoordinateBounds(options.coordinateBounds); - } - - // Create the snapshotter - _mbglMapSnapshotter = std::make_unique<mbgl::MapSnapshotter>(*mbglFileSource, *_mbglThreadPool, styleURL, size, pixelRatio, cameraOptions, coordinateBounds); + } return self; } @@ -437,81 +406,40 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; _mbglMapSnapshotter.reset(); } -- (NSURL *)styleURL -{ - NSString *styleURLString = @(_mbglMapSnapshotter->getStyleURL().c_str()); - return styleURLString.length ? [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 +- (void)setOptions:(MGLMapSnapshotOptions *)options { + _options = options; + mbgl::DefaultFileSource *mbglFileSource = [MGLOfflineStorage sharedOfflineStorage].mbglFileSource; + _mbglThreadPool = mbgl::sharedThreadPool(); + + std::string styleURL = std::string([options.styleURL.absoluteString UTF8String]); + + // Size; taking into account the minimum texture size for OpenGL ES + // For non retina screens the ratio is 1:1 MGLSnapshotterMinimumPixelSize + mbgl::Size size = { + static_cast<uint32_t>(MAX(options.size.width, MGLSnapshotterMinimumPixelSize)), + static_cast<uint32_t>(MAX(options.size.height, MGLSnapshotterMinimumPixelSize)) + }; + + float pixelRatio = MAX(options.scale, 1); + + // Camera options 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); + if (CLLocationCoordinate2DIsValid(options.camera.centerCoordinate)) { + cameraOptions.center = MGLLatLngFromLocationCoordinate2D(options.camera.centerCoordinate); } + cameraOptions.angle = MAX(0, options.camera.heading) * mbgl::util::DEG2RAD; + cameraOptions.zoom = MAX(0, options.zoomLevel); + cameraOptions.pitch = MAX(0, options.camera.pitch); - 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)); + // Region + mbgl::optional<mbgl::LatLngBounds> coordinateBounds; + if (!MGLCoordinateBoundsIsEmpty(options.coordinateBounds)) { + coordinateBounds = MGLLatLngBoundsFromCoordinateBounds(options.coordinateBounds); + } + + // Create the snapshotter + _mbglMapSnapshotter = std::make_unique<mbgl::MapSnapshotter>(*mbglFileSource, *_mbglThreadPool, styleURL, size, pixelRatio, cameraOptions, coordinateBounds); } @end |