forked from applidium/ADClusterMapView
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCDMapViewController.m
More file actions
362 lines (258 loc) · 12.2 KB
/
CDMapViewController.m
File metadata and controls
362 lines (258 loc) · 12.2 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// CDMapViewController.m
// ClusterDemo
//
// Created by Patrick Nollet on 09/10/12.
// Copyright (c) 2012 Applidium. All rights reserved.
//
#import "CDMapViewController.h"
#import "ADBaseAnnotation.h"
#import "TSBathroomAnnotation.h"
#import "TSStreetLightAnnotation.h"
#import "TSDemoClusteredAnnotationView.h"
@interface CDMapViewController ()
@property (strong, nonatomic) NSDate *startTime;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@end
@implementation CDMapViewController
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(48.857617, 2.338820), MKCoordinateSpanMake(1.0, 1.0))];
_mapView.clusterDiscrimination = 1.0;
[_tabBar setSelectedItem:_bathroomTabBarItem];
[self parseJsonData];
[self refreshBadges];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(kdTreeLoadingProgress:)
name:KDTreeClusteringProgress
object:nil];
}
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *view;
if ([annotation isKindOfClass:[TSStreetLightAnnotation class]]) {
view = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:NSStringFromClass([TSStreetLightAnnotation class])];
if (!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:NSStringFromClass([TSStreetLightAnnotation class])];
view.image = [UIImage imageNamed:kStreetLightAnnotationImage];
view.canShowCallout = YES;
view.centerOffset = CGPointMake(view.centerOffset.x, -view.frame.size.height/2);
}
}
else if ([annotation isKindOfClass:[TSBathroomAnnotation class]]) {
view = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:NSStringFromClass([TSBathroomAnnotation class])];
if (!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:NSStringFromClass([TSBathroomAnnotation class])];
view.image = [UIImage imageNamed:kBathroomAnnotationImage];
view.canShowCallout = YES;
view.centerOffset = CGPointMake(view.centerOffset.x, -view.frame.size.height/2);
}
}
return view;
}
#pragma mark - ADClusterMapView Delegate
- (MKAnnotationView *)mapView:(TSClusterMapView *)mapView viewForClusterAnnotation:(ADClusterAnnotation *)annotation {
TSDemoClusteredAnnotationView * view = (TSDemoClusteredAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:NSStringFromClass([TSDemoClusteredAnnotationView class])];
if (!view) {
view = [[TSDemoClusteredAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:NSStringFromClass([TSDemoClusteredAnnotationView class])];
}
return view;
}
- (void)mapView:(TSClusterMapView *)mapView willBeginBuildingClusterTreeForMapPoints:(NSSet<ADMapPointAnnotation *> *)annotations {
NSLog(@"Kd-tree will begin mapping item count %lu", (unsigned long)annotations.count);
_startTime = [NSDate date];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if (annotations.count > 10000) {
[_progressView setHidden:NO];
}
else {
[_progressView setHidden:YES];
}
}];
}
- (void)mapView:(TSClusterMapView *)mapView didFinishBuildingClusterTreeForMapPoints:(NSSet<ADMapPointAnnotation *> *)annotations {
NSLog(@"Kd-tree finished mapping item count %lu", (unsigned long)annotations.count);
NSLog(@"Took %f seconds", -[_startTime timeIntervalSinceNow]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[_progressView setHidden:YES];
_progressView.progress = 0.0;
}];
}
- (void)mapViewWillBeginClusteringAnimation:(TSClusterMapView *)mapView{
NSLog(@"Animation operation will begin");
}
- (void)mapViewDidCancelClusteringAnimation:(TSClusterMapView *)mapView {
NSLog(@"Animation operation cancelled");
}
- (void)mapViewDidFinishClusteringAnimation:(TSClusterMapView *)mapView{
NSLog(@"Animation operation finished");
}
- (void)userWillPanMapView:(TSClusterMapView *)mapView {
NSLog(@"Map will pan from user interaction");
}
- (void)userDidPanMapView:(TSClusterMapView *)mapView {
NSLog(@"Map did pan from user interaction");
}
- (BOOL)mapView:(TSClusterMapView *)mapView shouldForceSplitClusterAnnotation:(ADClusterAnnotation *)clusterAnnotation {
return YES;
}
- (BOOL)mapView:(TSClusterMapView *)mapView shouldRepositionAnnotations:(NSArray<ADClusterAnnotation *> *)annotations toAvoidClashAtCoordinate:(CLLocationCoordinate2D)coordinate {
return YES;
}
#pragma mark - Tab Bar Delegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
[self resetsStepperValues];
}
- (void)resetsStepperValues {
if (_tabBar.selectedItem == _bathroomTabBarItem) {
_stepper.value = _bathroomAnnotationsAdded.count;
_stepper.minimumValue = 0;
_stepper.maximumValue = _bathroomAnnotations.count;
}
else if (_tabBar.selectedItem == _streetLightsTabBarItem) {
_stepper.value = _streetLightAnnotationsAdded.count;
_stepper.minimumValue = 0;
_stepper.maximumValue = _streetLightAnnotations.count;
}
}
#pragma mark - Controls
- (IBAction)addAll:(id)sender {
if (_tabBar.selectedItem == _bathroomTabBarItem) {
NSLog(@"Adding All %@", CDToiletJsonFile);
[_mapView addClusteredAnnotations:_bathroomAnnotations toGroup:CDToiletJsonFile];
_bathroomAnnotationsAdded = [NSMutableArray arrayWithArray:_bathroomAnnotations];
}
else if (_tabBar.selectedItem == _streetLightsTabBarItem) {
NSLog(@"Adding All %@", CDStreetLightJsonFile);
[_mapView addClusteredAnnotations:_streetLightAnnotations toGroup:CDStreetLightJsonFile];
_streetLightAnnotationsAdded = [NSMutableArray arrayWithArray:_streetLightAnnotations];
}
[self refreshBadges];
[self resetsStepperValues];
}
- (IBAction)removeAll:(id)sender {
if (_tabBar.selectedItem == _bathroomTabBarItem) {
[_mapView removeAnnotations:_bathroomAnnotationsAdded fromGroup:CDToiletJsonFile];
[_bathroomAnnotationsAdded removeAllObjects];
NSLog(@"Removing All %@", CDToiletJsonFile);
}
else if (_tabBar.selectedItem == _streetLightsTabBarItem) {
[_mapView removeAnnotations:_streetLightAnnotationsAdded fromGroup:CDStreetLightJsonFile];
[_streetLightAnnotationsAdded removeAllObjects];
NSLog(@"Removing All %@", CDStreetLightJsonFile);
}
[self refreshBadges];
}
- (IBAction)stepperValueChanged:(id)sender {
if (_tabBar.selectedItem == _bathroomTabBarItem) {
if (_stepper.value >= _bathroomAnnotationsAdded.count) {
[self addNewBathroom];
}
else {
[self removeLastBathroom];
}
_stepper.maximumValue = _bathroomAnnotations.count;
_stepper.value = _bathroomAnnotationsAdded.count;
}
else if (_tabBar.selectedItem == _streetLightsTabBarItem) {
if (_stepper.value >= _streetLightAnnotationsAdded.count) {
[self addNewStreetLight];
}
else {
[self removeLastStreetLight];
}
_stepper.maximumValue = _streetLightAnnotations.count;
_stepper.value = _streetLightAnnotationsAdded.count;
}
[self refreshBadges];
}
- (void)refreshBadges {
_bathroomTabBarItem.badgeValue = [NSString stringWithFormat:@"%lu", (unsigned long)_bathroomAnnotationsAdded.count];
_streetLightsTabBarItem.badgeValue = [NSString stringWithFormat:@"%lu", (unsigned long)_streetLightAnnotationsAdded.count];
}
- (void)addNewBathroom {
if (_bathroomAnnotationsAdded.count >= _bathroomAnnotations.count) {
return;
}
NSLog(@"Adding 1 %@", CDToiletJsonFile);
TSBathroomAnnotation *annotation = [_bathroomAnnotations objectAtIndex:_bathroomAnnotationsAdded.count];
[_bathroomAnnotationsAdded addObject:annotation];
[_mapView addClusteredAnnotation:annotation toGroup:CDToiletJsonFile];
}
- (void)addNewStreetLight {
if (_streetLightAnnotationsAdded.count >= _streetLightAnnotations.count) {
return;
}
NSLog(@"Adding 1 %@", CDStreetLightJsonFile);
TSStreetLightAnnotation *annotation = [_streetLightAnnotations objectAtIndex:_streetLightAnnotationsAdded.count];
[_streetLightAnnotationsAdded addObject:annotation];
[_mapView addClusteredAnnotation:annotation toGroup:CDStreetLightJsonFile];
}
- (void)removeLastBathroom {
NSLog(@"Removing 1 %@", CDToiletJsonFile);
TSBathroomAnnotation *annotation = [_bathroomAnnotationsAdded lastObject];
[_bathroomAnnotationsAdded removeObject:annotation];
[_mapView removeAnnotation:annotation fromGroup:CDToiletJsonFile];
}
- (void)removeLastStreetLight {
NSLog(@"Removing 1 %@", CDStreetLightJsonFile);
TSStreetLightAnnotation *annotation = [_streetLightAnnotationsAdded lastObject];
[_streetLightAnnotationsAdded removeObject:annotation];
[_mapView removeAnnotation:annotation fromGroup:CDStreetLightJsonFile];
}
- (IBAction)segmentedControlValueChanged:(id)sender {
switch (_segmentedControl.selectedSegmentIndex) {
case 0:
_mapView.clusterEdgeBufferSize = ADClusterBufferNone;
break;
case 1:
_mapView.clusterEdgeBufferSize = ADClusterBufferSmall;
break;
case 2:
_mapView.clusterEdgeBufferSize = ADClusterBufferMedium;
break;
case 3:
_mapView.clusterEdgeBufferSize = ADClusterBufferLarge;
break;
default:
break;
}
}
- (IBAction)sliderValueChanged:(id)sender {
_mapView.clusterPreferredVisibleCount = roundf(_slider.value);
_label.text = [NSString stringWithFormat:@"%lu", (unsigned long)_mapView.clusterPreferredVisibleCount];
}
- (void)parseJsonData {
_streetLightAnnotations = [[NSMutableArray alloc] initWithCapacity:10];
_bathroomAnnotations = [[NSMutableArray alloc] initWithCapacity:10];
_streetLightAnnotationsAdded = [[NSMutableArray alloc] initWithCapacity:10];
_bathroomAnnotationsAdded = [[NSMutableArray alloc] initWithCapacity:10];
NSLog(@"Loading data…");
[[NSOperationQueue new] addOperationWithBlock:^{
NSData * JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:CDStreetLightJsonFile ofType:@"json"]];
for (NSDictionary * annotationDictionary in [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:NULL]) {
TSStreetLightAnnotation * annotation = [[TSStreetLightAnnotation alloc] initWithDictionary:annotationDictionary];
[_streetLightAnnotations addObject:annotation];
}
NSLog(@"Finished CDStreetLightJsonFile");
}];
[[NSOperationQueue new] addOperationWithBlock:^{
NSData * JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:CDToiletJsonFile ofType:@"json"]];
for (NSDictionary * annotationDictionary in [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:NULL]) {
TSBathroomAnnotation * annotation = [[TSBathroomAnnotation alloc] initWithDictionary:annotationDictionary];
[_bathroomAnnotations addObject:annotation];
}
NSLog(@"Finished CDToiletJsonFile");
}];
}
- (void)kdTreeLoadingProgress:(NSNotification *)notification {
NSNumber *number = [notification object];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
_progressView.progress = number.floatValue;
}];
}
@end