This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
StaticSnapshotExample.m
67 lines (52 loc) · 2.99 KB
/
StaticSnapshotExample.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#import "StaticSnapshotExample.h"
@import Mapbox;
NSString *const MBXExampleStaticSnapshot = @"StaticSnapshotExample";
@interface StaticSnapshotExample ()
@property MGLMapView *mapView;
@property UIButton *button;
@property UIImageView *imageView;
@end
@implementation StaticSnapshotExample
- (void)viewDidLoad {
[super viewDidLoad];
_mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2) styleURL:[MGLStyle satelliteStreetsStyleURL]];
_mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Center map on the Giza Pyramid Complex in Egypt.
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(29.9773, 31.1325);
[_mapView setCenterCoordinate:center zoomLevel:14 animated:NO];
[self.view addSubview:_mapView];
// Create a button to take a map snapshot.
_button = [[UIButton alloc] initWithFrame:CGRectMake(_mapView.bounds.size.width / 2 - 15, _mapView.bounds.size.height - 40, 80, 30)];
_button.layer.cornerRadius = 15;
_button.backgroundColor = [UIColor colorWithRed:0.96f green:0.65f blue:0.14f alpha:1];
[_button setImage:[UIImage imageNamed:@"camera"] forState:UIControlStateNormal];
[_button addTarget:self action:@selector(createSnapshot) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
// Create a UIImageView that will store the map snapshot.
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height / 2, self.view.bounds.size.width, self.view.bounds.size.height / 2)];
_imageView.backgroundColor = [UIColor blackColor];
_imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_imageView];
}
- (void)createSnapshot {
// Use the map's style, camera, size, and zoom level to set the snapshot's options.
MGLMapSnapshotOptions *options = [[MGLMapSnapshotOptions alloc] initWithStyleURL:_mapView.styleURL camera:_mapView.camera size:_mapView.bounds.size];
options.zoomLevel = _mapView.zoomLevel;
// Add an activity indicator to show that the snapshot is loading.
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(_imageView.center.x - 30, _imageView.center.y - 30, 60, 60)];
[self.view addSubview:indicator];
[indicator startAnimating];
// Create the map snapshot.
__block MGLMapSnapshotter *snapshotter = [[MGLMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MGLMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Unable to create a map snapshot.");
} else if (snapshot != nil) {
// Add the map snapshot's image to the image view.
[indicator stopAnimating];
self.imageView.image = snapshot.image;
}
snapshotter = nil;
}];
}
@end