-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolygonAnnotationExample.m
127 lines (98 loc) · 5.39 KB
/
PolygonAnnotationExample.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// PolygonAnnotationExample.m
// mapboxqs
//
// Created by Tuyen Vu on 11/03/2023.
//
#import "PolygonAnnotationExample.h"
#import <MapboxMaps/MapboxMaps.h>
#import <MapboxCoreMaps/MapboxCoreMaps.h>
#import <MapboxMapObjC/MapboxMapObjC.h>
#import "MapboxMaps-Swift.h"
#import <MapKit/MapKit.h>
@interface PolygonAnnotationExample () <TMBAnnotationInteractionDelegate>
@end
@implementation PolygonAnnotationExample {
MapView* mapView;
NSMutableSet<TMBCancelable*>* cancelables;
}
- (void)viewDidLoad {
[super viewDidLoad];
cancelables = [NSMutableSet new];
// Do any additional setup after loading the view.
CLLocationCoordinate2D centerLocation = CLLocationCoordinate2DMake(25.04579, -88.90136);
TMBCameraOptions* cameraOptions = [[TMBCameraOptions alloc] initWithCenter:centerLocation padding:UIEdgeInsetsMake(0, 0, 0, 0) anchor:CGPointMake(0, 0) zoom:5 bearing:0 pitch:0];
MapInitOptions* options = [MapInitOptionsFactory createWithMapOptions:nil cameraOptions:cameraOptions styleURI:nil styleJSON:nil antialiasingSampleCount:1];
mapView = [MapViewFactory createWithFrame:self.view.bounds
options:options];
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:mapView];
__weak PolygonAnnotationExample *weakSelf = self;
// Allows the delegate to receive information about map events.
[cancelables addObject:[[mapView mapboxMap] onMapLoaded:^(id _Nonnull _) {
[weakSelf setupExample];
if ([weakSelf respondsToSelector:@selector(finish)]) {
[weakSelf finish];
}
[self->cancelables addObject:[[self->mapView location] onLocationChangeWithHandler:^(NSArray<MBXLocation *> * _Nonnull _) {
}]];
[self->cancelables addObject:[[self->mapView location] onHeadingChangeWithHandler:^(TMBHeading * _Nonnull _) {
}]];
}]];
}
// Wait for the map to load before adding an annotation.
- (void) setupExample {
// Create the PolygonAnnotationManager
// Annotation managers are kept alive by `AnnotationOrchestrator`
// (`mapView.annotations`) until you explicitly destroy them
// by calling `mapView.annotations.removeAnnotationManager(withId:)`
TMBPolygonAnnotationManager* polygonAnnotationManager = [[mapView annotations] makePolygonAnnotationManagerWithId:nil
layerPosition:nil];
// TODO Add event handler
// // Set the delegate to receive callback if annotation is tapped or dragged
// polygonAnnotationManager.delegate = self;
// Create the polygon annotation
TMBPolygonAnnotation* polygonAnnotation = [[TMBPolygonAnnotation alloc] initWithId:@"xxx-sample" polygon:[self makePolygon] isSelected:false isDraggable:false];
// Style the polygon annotation
polygonAnnotation.fillColor = [UIColor redColor];
polygonAnnotation.fillOpacity = @0.8;
// Add the polygon annotation to the manager
polygonAnnotationManager.annotations = @[
polygonAnnotation
];
}
- (TMBPolygon*) makePolygon {
// Describe the polygon's geometry
CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(24.51713945052515, -89.857177734375);
CLLocationCoordinate2D coord2 = CLLocationCoordinate2DMake(24.51713945052515, -87.967529296875);
CLLocationCoordinate2D coord3 = CLLocationCoordinate2DMake(26.244156283890756, -87.967529296875);
CLLocationCoordinate2D coord4 = CLLocationCoordinate2DMake(26.244156283890756, -89.857177734375);
CLLocationCoordinate2D coord5 = CLLocationCoordinate2DMake(24.51713945052515, -89.857177734375);
NSArray* outerRingCoords = @[
[NSValue valueWithMKCoordinate: coord1],
[NSValue valueWithMKCoordinate: coord2],
[NSValue valueWithMKCoordinate: coord3],
[NSValue valueWithMKCoordinate: coord4],
[NSValue valueWithMKCoordinate: coord5]
];
CLLocationCoordinate2D icoord1 = CLLocationCoordinate2DMake(25.085598897064752, -89.20898437499999);
CLLocationCoordinate2D icoord2 = CLLocationCoordinate2DMake(25.085598897064752, -88.61572265625);
CLLocationCoordinate2D icoord3 = CLLocationCoordinate2DMake(25.720735134412106, -88.61572265625);
CLLocationCoordinate2D icoord4 = CLLocationCoordinate2DMake(25.720735134412106, -89.20898437499999);
CLLocationCoordinate2D icoord5 = CLLocationCoordinate2DMake(25.085598897064752, -89.20898437499999);
// This polygon has an intererior polygon which represents a hole in the shape.
NSArray* innerRingCoords = @[
[NSValue valueWithMKCoordinate: icoord1],
[NSValue valueWithMKCoordinate: icoord2],
[NSValue valueWithMKCoordinate: icoord3],
[NSValue valueWithMKCoordinate: icoord4],
[NSValue valueWithMKCoordinate: icoord5]
];
TMBPolygonRing* outerRing = [[TMBPolygonRing alloc] initWithCoordinates:outerRingCoords];
TMBPolygonRing* innerRing = [[TMBPolygonRing alloc] initWithCoordinates:innerRingCoords];
return [[TMBPolygon alloc] initWithOuterRing:outerRing innerRings:@[innerRing]];
}
- (void)annotationManager:(id<TMBAnnotationManager> _Nonnull)manager didDetectTappedAnnotations:(NSArray<id<TMBAnnotation>> * _Nonnull)annotations {
NSLog(@"AnnotationManager did detect tapped annotations: %@", annotations);
}
@end