Skip to content

Commit 682ef63

Browse files
alanchenboyweichen-grabtaxilouwers
authored
support hide attribution on iOS side (#3952)
Co-authored-by: Wei Chen <[email protected]> Co-authored-by: Bart Louwers <[email protected]>
1 parent 2809961 commit 682ef63

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

platform/darwin/src/MLNMapSnapshotter.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,15 @@ MLN_EXPORT
7979
// MARK: - Configuring the Map
8080

8181
/**
82-
:nodoc:
8382
Whether to include the MapLibre logo. Note this is not required.
8483
*/
8584
@property (nonatomic, readwrite) BOOL showsLogo;
8685

86+
/**
87+
Whether to add attribution from the sources on the snapshots. Enabled by default.
88+
*/
89+
@property (nonatomic, readwrite) BOOL showsAttribution;
90+
8791
/**
8892
URL of the map style to snapshot.
8993
*/

platform/darwin/src/MLNMapSnapshotter.mm

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ - (instancetype _Nonnull)initWithStyleURL:(nullable NSURL *)styleURL camera:(MLN
146146
_size = size;
147147
_camera = camera;
148148
_showsLogo = YES;
149+
_showsAttribution = YES;
149150
#if TARGET_OS_IPHONE
150151
_scale = [UIScreen mainScreen].scale;
151152
#else
@@ -161,6 +162,7 @@ - (nonnull id)copyWithZone:(nullable NSZone *)zone {
161162
copy.coordinateBounds = _coordinateBounds;
162163
copy.scale = _scale;
163164
copy.showsLogo = _showsLogo;
165+
copy.showsAttribution = _showsAttribution;
164166
return copy;
165167
}
166168

@@ -403,22 +405,24 @@ - (void)startWithQueue:(dispatch_queue_t)queue overlayHandler:(MLNMapSnapshotOve
403405
return nil;
404406
}
405407

406-
if (options.showsLogo) {
408+
if (options.showsLogo && logoImage) {
407409
[logoImage drawInRect:logoImageRect];
408410
}
409411

410-
UIImage *currentImage = UIGraphicsGetImageFromCurrentImageContext();
411-
CGImageRef attributionImageRef = CGImageCreateWithImageInRect([currentImage CGImage], cropRect);
412-
UIImage *attributionImage = [UIImage imageWithCGImage:attributionImageRef];
413-
CGImageRelease(attributionImageRef);
412+
if (options.showsAttribution) {
413+
UIImage *currentImage = UIGraphicsGetImageFromCurrentImageContext();
414+
CGImageRef attributionImageRef = CGImageCreateWithImageInRect([currentImage CGImage], cropRect);
415+
UIImage *attributionImage = [UIImage imageWithCGImage:attributionImageRef];
416+
CGImageRelease(attributionImageRef);
414417

415-
CIImage *ciAttributionImage = [[CIImage alloc] initWithCGImage:attributionImage.CGImage];
418+
CIImage *ciAttributionImage = [[CIImage alloc] initWithCGImage:attributionImage.CGImage];
416419

417-
UIImage *blurredAttributionBackground = [MLNMapSnapshotter blurredAttributionBackground:ciAttributionImage];
420+
UIImage *blurredAttributionBackground = [MLNMapSnapshotter blurredAttributionBackground:ciAttributionImage];
418421

419-
[blurredAttributionBackground drawInRect:attributionBackgroundFrame];
422+
[blurredAttributionBackground drawInRect:attributionBackgroundFrame];
420423

421-
[MLNMapSnapshotter drawAttributionTextWithStyle:attributionInfoStyle origin:attributionTextPosition attributionInfo:attributionInfo];
424+
[MLNMapSnapshotter drawAttributionTextWithStyle:attributionInfoStyle origin:attributionTextPosition attributionInfo:attributionInfo];
425+
}
422426

423427
UIImage *compositedImage = UIGraphicsGetImageFromCurrentImageContext();
424428

@@ -478,19 +482,21 @@ - (void)startWithQueue:(dispatch_queue_t)queue overlayHandler:(MLNMapSnapshotOve
478482
return nil;
479483
}
480484

481-
if (logoImage) {
485+
if (options.showsLogo) {
482486
[logoImage drawInRect:logoImageRect];
483487
}
484488

485-
NSBitmapImageRep *attributionBackground = [[NSBitmapImageRep alloc] initWithFocusedViewRect:attributionBackgroundFrame];
489+
if (options.showsAttribution) {
490+
NSBitmapImageRep *attributionBackground = [[NSBitmapImageRep alloc] initWithFocusedViewRect:attributionBackgroundFrame];
486491

487-
CIImage *attributionBackgroundImage = [[CIImage alloc] initWithCGImage:[attributionBackground CGImage]];
492+
CIImage *attributionBackgroundImage = [[CIImage alloc] initWithCGImage:[attributionBackground CGImage]];
488493

489-
NSImage *blurredAttributionBackground = [MLNMapSnapshotter blurredAttributionBackground:attributionBackgroundImage];
494+
NSImage *blurredAttributionBackground = [MLNMapSnapshotter blurredAttributionBackground:attributionBackgroundImage];
490495

491-
[blurredAttributionBackground drawInRect:attributionBackgroundFrame];
496+
[blurredAttributionBackground drawInRect:attributionBackgroundFrame];
492497

493-
[MLNMapSnapshotter drawAttributionTextWithStyle:attributionInfoStyle origin:attributionTextPosition attributionInfo:attributionInfo];
498+
[MLNMapSnapshotter drawAttributionTextWithStyle:attributionInfoStyle origin:attributionTextPosition attributionInfo:attributionInfo];
499+
}
494500

495501
[compositedImage unlockFocus];
496502

platform/ios/app/MBXSnapshotsViewController.m

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,23 @@ - (void)viewDidLoad {
3232
[super viewDidLoad];
3333

3434
// Start snapshotters
35-
topLeftSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTL coordinates:CLLocationCoordinate2DMake(37.7184, -122.4365)];
36-
topCenterSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTM coordinates:CLLocationCoordinate2DMake(38.8936, -77.0146)];
37-
topRightSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTR coordinates:CLLocationCoordinate2DMake(-13.1356, -74.2442)];
35+
topLeftSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTL coordinates:CLLocationCoordinate2DMake(37.7184, -122.4365) hideAttribution:YES];
36+
topCenterSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTM coordinates:CLLocationCoordinate2DMake(38.8936, -77.0146) hideAttribution:NO];
37+
topRightSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTR coordinates:CLLocationCoordinate2DMake(-13.1356, -74.2442) hideAttribution:YES];
3838

39-
bottomLeftSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBL coordinates:CLLocationCoordinate2DMake(52.5072, 13.4247)];
40-
bottomCenterSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBM coordinates:CLLocationCoordinate2DMake(60.2118, 24.6754)];
41-
bottomRightSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBR coordinates:CLLocationCoordinate2DMake(31.2780, 121.4286)];
39+
bottomLeftSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBL coordinates:CLLocationCoordinate2DMake(52.5072, 13.4247) hideAttribution:NO];
40+
bottomCenterSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBM coordinates:CLLocationCoordinate2DMake(60.2118, 24.6754) hideAttribution:YES];
41+
bottomRightSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBR coordinates:CLLocationCoordinate2DMake(31.2780, 121.4286) hideAttribution:NO];
4242
}
4343

44-
- (MLNMapSnapshotter*) startSnapshotterForImageView:(UIImageView*) imageView coordinates:(CLLocationCoordinate2D) coordinates {
44+
- (MLNMapSnapshotter*) startSnapshotterForImageView:(UIImageView*) imageView coordinates:(CLLocationCoordinate2D) coordinates hideAttribution:(BOOL) hideAttribution {
4545
// Create snapshot options
4646
MLNMapCamera* mapCamera = [[MLNMapCamera alloc] init];
4747
mapCamera.pitch = 20;
4848
mapCamera.centerCoordinate = coordinates;
49-
MLNMapSnapshotOptions* options = [[MLNMapSnapshotOptions alloc] initWithStyleURL:[[MLNStyle predefinedStyle:@"Hybrid"] url] camera:mapCamera size:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
49+
MLNMapSnapshotOptions* options = [[MLNMapSnapshotOptions alloc] initWithStyleURL:[NSURL URLWithString:@"https://tiles.openfreemap.org/styles/liberty"] camera:mapCamera size:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
5050
options.zoomLevel = 10;
51+
options.showsAttribution = !hideAttribution;
5152

5253
// Create and start the snapshotter
5354
__weak UIImageView *weakImageView = imageView;

0 commit comments

Comments
 (0)