summaryrefslogtreecommitdiff
path: root/platform/ios/MBXContextSnapshotViewController.m
blob: 61504f94a5a7b3864d27fc5d356334c32d0e4a7a (plain)
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
68
69
70
71
72
73
74
75
76
77
#import "MBXContextSnapshotViewController.h"
#import <Mapbox/Mapbox.h>

@interface MBXContextSnapshotViewController () <MGLMapViewDelegate>
@property MGLMapView *mapView;
@property UIButton *button;
@property UIImageView *imageView;
@end

@implementation MBXContextSnapshotViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2) styleURL:[MGLStyle lightStyleURL]];
    _mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;


    [_mapView setCenterCoordinate:_mapView.centerCoordinate zoomLevel:1 animated:NO];
    [self.view addSubview:_mapView];


    _button = [[UIButton alloc] initWithFrame:CGRectMake(_mapView.frame.origin.x + 100, _mapView.frame.origin.y + 45, 170, 30)];
    _button.layer.cornerRadius = 15;
    _button.backgroundColor = [UIColor blueColor];
    [_button setTitle:@"Create snapshot" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(createSnapshot) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];

    _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 {

    MGLMapSnapshotOptions *options = [[MGLMapSnapshotOptions alloc] initWithStyleURL:_mapView.styleURL camera:_mapView.camera size:_mapView.bounds.size];

    options.zoomLevel = _mapView.zoomLevel;

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(_imageView.center.x - 30, _imageView.center.y - 30, 60, 60)];
    [self.view addSubview:indicator];
    [indicator startAnimating];

    __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;
//    }];

    [snapshotter startWithOverlayHandler:^(NSString * _Nullable testString) {
        NSLog(@"STRING: %@", testString);
    } completionHandler:^(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