-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdvancedViewportGesturesExample.m
More file actions
218 lines (178 loc) · 7.85 KB
/
Copy pathAdvancedViewportGesturesExample.m
File metadata and controls
218 lines (178 loc) · 7.85 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// AdvancedViewportGesturesExample.m
// mapboxqs
//
// Created by Tuyen Vu on 11/03/2023.
//
#import "AdvancedViewportGesturesExample.h"
#import <MapboxMaps/MapboxMaps.h>
#import <MapboxCoreMaps/MapboxCoreMaps.h>
#import <MapboxCommon/MapboxCommon.h>
#import <MapboxMapObjC/MapboxMapObjC.h>
#import "MapboxMaps-Swift.h"
#import <MapKit/MapKit.h>
typedef enum {
StateFollowing,
StateOverview
} State;
@interface AdvancedViewportGesturesExample()
@property (nonatomic) State state;
@end
@implementation AdvancedViewportGesturesExample {
MapView* mapView;
TMBFollowPuckViewportState *followPuckViewportState;
TMBOverviewViewportState *overviewViewportState;
}
- (void)setState:(State)state {
if (_state != state) {
_state = state;
[self syncWithState];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
mapView = [MapViewFactory createWithFrame:self.view.bounds
options:nil];
// mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:mapView];
CLLocationCoordinate2D cupertino = CLLocationCoordinate2DMake(37.3230, -122.0322);
[[mapView mapboxMap] setCameraTo:[[TMBCameraOptions alloc] initWithCenter:cupertino
padding:UIEdgeInsetsZero
anchor:CGPointZero
zoom:14
bearing:0
pitch:0]];
TMBLocationManager *locationManager = [mapView location];
TMBLocationOptions *locationOptions = locationManager.options;
locationOptions.puckType = [TMBPuck2DConfiguration makeDefaultWithShowBearing:true];
locationOptions.puckBearing = TMBPuckBearingCourse;
locationOptions.puckBearingEnabled = true;
locationManager.options = locationOptions;
TMBViewportManager *viewportManager = [mapView viewport];
followPuckViewportState = [viewportManager makeFollowPuckViewportStateWithOptions:[[TMBFollowPuckViewportStateOptions alloc] initWithPadding:nil
zoom:nil
bearing:[TMBFollowPuckViewportStateBearing course]
pitch:nil]];
int vertices = 100;
int radius = 20000;
NSMutableArray<NSValue*> *coordinates = [[NSMutableArray alloc] initWithCapacity:vertices];
for (int i=0; i<vertices; i++) {
float bearing = fabs(i * -360.0/vertices);
CLLocationCoordinate2D coord = [TMBLocationManager coordinateFrom:cupertino
at:radius
facing:bearing];
[coordinates addObject: [NSValue valueWithMKCoordinate:coord]];
}
MBXGeometry *polygon = [GeometryHelper createPolygonWithCenter:cupertino radius:20000 vertices:100];
TMBOverviewViewportStateOptions *overviewViewportStateOptions =
[[TMBOverviewViewportStateOptions alloc] initWithGeometry:polygon
geometryPadding:UIEdgeInsetsZero
bearing:nil
pitch:nil
padding:nil
maxZoom:nil
offset:nil
animationDuration:0];
overviewViewportState = [viewportManager makeOverviewViewportStateWithOptions:overviewViewportStateOptions] ;
[mapView gestures].delegate = self;
[self syncWithState];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([self respondsToSelector:@selector(finish)]){
[self finish];
}
[[mapView viewport] addStatusObserver:self];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[mapView viewport] removeStatusObserver:self];
}
- (void) toggleViewportState {
switch (self.state) {
case StateOverview:
self.state = StateFollowing;
break;
case StateFollowing:
self.state = StateOverview;
}
}
- (void) syncWithState {
switch (self.state) {
case StateFollowing:
[[mapView viewport] transitionTo:followPuckViewportState
transition:nil
completion:nil];
break;
case StateOverview:
[[mapView viewport] transitionTo:overviewViewportState
transition:nil
completion:nil];
break;
}
TMBViewportOptions *viewportOptions = [mapView viewport].options;
viewportOptions.transitionsToIdleUponUserInteraction = self.state == StateOverview;
[mapView viewport].options = viewportOptions;
TMBGestureManager *gestureManager = [mapView gestures];
TMBGestureOptions *gestureOptions = gestureManager.gestureOptions;
gestureOptions.panEnabled = self.state == StateOverview;
gestureOptions.pinchEnabled = self.state == StateOverview;
gestureManager.gestureOptions = gestureOptions;
}
/*
#pragma mark - TMBGestureManagerDelegate
*/
- (void)gestureManagerWithDidBegin:(enum TMBGestureType)gestureType {
switch (gestureType) {
case TMBGestureTypePitch:
followPuckViewportState.options.pitch = nil;
break;
case TMBGestureTypeDoubleTapToZoomIn:
case TMBGestureTypeDoubleTouchToZoomOut:
case TMBGestureTypeQuickZoom:
followPuckViewportState.options.zoom = nil;
break;
default:
break;
}
}
- (void)gestureManagerWithDidEnd:(enum TMBGestureType)gestureType willAnimate:(BOOL)willAnimate {
switch (gestureType) {
case TMBGestureTypePitch:
if (self.state == StateFollowing) {
float pitch = [mapView mapboxMap].cameraState.pitch;
followPuckViewportState.options.pitch = [NSNumber numberWithFloat:pitch];
}
break;
case TMBGestureTypeQuickZoom:
if (self.state == StateFollowing) {
float zoom = [mapView mapboxMap].cameraState.zoom;
followPuckViewportState.options.zoom = [NSNumber numberWithFloat:zoom];
}
break;
case TMBGestureTypeSingleTap:
[self toggleViewportState];
break;
default:
break;
}
}
- (void)gestureManagerWithDidEndAnimatingFor:(enum TMBGestureType)gestureType {
switch (gestureType) {
case TMBGestureTypeDoubleTapToZoomIn:
case TMBGestureTypeDoubleTouchToZoomOut:
if (self.state == StateFollowing) {
float zoom = [mapView mapboxMap].cameraState.zoom;
followPuckViewportState.options.zoom = [NSNumber numberWithFloat:zoom];
}
default:
break;
}
}
/*
#pragma mark - TMBViewportStatusObserver
*/
- (void)viewportStatusDidChangeFrom:(TMBViewportStatus * _Nonnull)fromStatus to:(TMBViewportStatus * _Nonnull)toStatus reason:(TMBViewportStatusChangeReason * _Nonnull)reason {
NSLog(@"Viewport.status changed\n from: %@\n to: %@\n with reason: %@", fromStatus, toStatus, reason);
}
@end