summaryrefslogtreecommitdiff
path: root/platform/ios/demo/Examples/ObjectiveC/DrawingACustomMarkerExample.m
blob: ad80b49efc4b5204f2ce7fa8e920229d942c8807 (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
#import "DrawingACustomMarkerExample.h"
@import Mapbox;

NSString *const MBXExampleDrawingACustomMarker = @"DrawingACustomMarkerExample";

@interface DrawingACustomMarkerExample () <MGLMapViewDelegate>
@end

@implementation DrawingACustomMarkerExample

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSURL *styleURL = [MGLStyle lightStyleURLWithVersion:9];
    MGLMapView *mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds styleURL:styleURL];
    mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    mapView.tintColor = [UIColor darkGrayColor];
    
    // Set the map‘s bounds to Pisa, Italy.
    MGLCoordinateBounds bounds = MGLCoordinateBoundsMake(
        CLLocationCoordinate2DMake(43.7115, 10.3725),
        CLLocationCoordinate2DMake(43.7318, 10.4222));
    [mapView setVisibleCoordinateBounds:bounds];
    
    [self.view addSubview:mapView];
    
    // Set the map view‘s delegate property.
    mapView.delegate = self;

    // Initialize and add the point annotation.
    MGLPointAnnotation *pisa = [[MGLPointAnnotation alloc] init];
    pisa.coordinate = CLLocationCoordinate2DMake(43.723, 10.396633);
    pisa.title = @"Leaning Tower of Pisa";
    [mapView addAnnotation:pisa];
}

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation {
    // Try to reuse the existing ‘pisa’ annotation image, if it exists.
    MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"pisa"];

    // If the ‘pisa’ annotation image hasn‘t been set yet, initialize it here.
    if (!annotationImage) {
        // Leaning Tower of Pisa by Stefan Spieler from the Noun Project.
        UIImage *image = [UIImage imageNamed:@"pisavector"];

        // The anchor point of an annotation is currently always the center. To
        // shift the anchor point to the bottom of the annotation, the image
        // asset includes transparent bottom padding equal to the original image
        // height.
        //
        // To make this padding non-interactive, we create another image object
        // with a custom alignment rect that excludes the padding.
        image = [image imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, image.size.height/2, 0)];

        // Initialize the ‘pisa’ annotation image with the UIImage we just loaded.
        annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:@"pisa"];
    }
    
    return annotationImage;
}

- (BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id <MGLAnnotation>)annotation {
    // Always allow callouts to popup when annotations are tapped.
    return YES;
}

@end