[darwin] map snapshotter - snapshotter mutability

This commit is contained in:
Ivo van Dongen
2017-10-05 14:19:07 +03:00
committed by Fabian Guerra Soto
parent fd3587608b
commit e9568ccda7
2 changed files with 121 additions and 6 deletions

View File

@@ -46,7 +46,7 @@ MGL_EXPORT
@property (nonatomic) MGLMapCamera *camera;
/**
The cooordinate rectangle that encompasses the bounds to capture.
The coordinate rectangle that encompasses the bounds to capture.
If this property is non-empty and the camera property is non-nil, the camera’s
center coordinate and altitude are ignored in favor of this property’s value.
@@ -139,6 +139,44 @@ MGL_EXPORT
*/
- (void)cancel;
/**
The zoom level.
The default zoom level is 0. This overwrites the camera zoom level if set.
*/
@property (nonatomic) double zoomLevel;
/**
A camera representing the viewport visible in the snapshot.
If this property is non-nil and the `coordinateBounds` property is set to a non-empty
coordinate bounds, the camera’s center coordinate and altitude are ignored in favor
of the `coordinateBounds` property.
*/
@property (nonatomic) MGLMapCamera *camera;
/**
The coordinate rectangle that encompasses the bounds to capture.
If this property is non-empty and the camera property is non-nil, the camera’s
center coordinate and altitude are ignored in favor of this property’s value.
*/
@property (nonatomic) MGLCoordinateBounds coordinateBounds;
/**
URL of the map style to snapshot.
The URL may be a full HTTP or HTTPS URL, a Mapbox URL indicating the style’s
map ID (`mapbox://styles/{user}/{style`}), or a path to a local file relative
to the application’s resource path. Specify `nil` for the default style.
*/
@property (nonatomic) NSURL* styleURL;
/**
The size of the output image, measured in points.
*/
@property (nonatomic) CGSize size;
/**
Indicates whether as snapshot is currently being generated.
*/

View File

@@ -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