This repository was archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathMBXViewController.m
1550 lines (1325 loc) · 64.6 KB
/
MBXViewController.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "MBXViewController.h"
#import "MBXAppDelegate.h"
#import "MBXCustomCalloutView.h"
#import "MBXOfflinePacksTableViewController.h"
#import "MBXAnnotationView.h"
#import "MBXUserLocationAnnotationView.h"
#import "MGLFillStyleLayer.h"
#import <Mapbox/Mapbox.h>
#import <objc/runtime.h>
static const CLLocationCoordinate2D WorldTourDestinations[] = {
{ .latitude = 38.9131982, .longitude = -77.0325453144239 },
{ .latitude = 37.7757368, .longitude = -122.4135302 },
{ .latitude = 12.9810816, .longitude = 77.6368034 },
{ .latitude = -13.15589555, .longitude = -74.2178961777998 },
};
static NSString * const MBXViewControllerAnnotationViewReuseIdentifer = @"MBXViewControllerAnnotationViewReuseIdentifer";
typedef NS_ENUM(NSInteger, MBXSettingsSections) {
MBXSettingsCoreRendering = 0,
MBXSettingsAnnotations,
MBXSettingsRuntimeStyling,
MBXSettingsMiscellaneous,
};
typedef NS_ENUM(NSInteger, MBXSettingsCoreRenderingRows) {
MBXSettingsCoreRenderingResetPosition = 0,
MBXSettingsCoreRenderingTileBoundaries,
MBXSettingsCoreRenderingTileInfo,
MBXSettingsCoreRenderingTimestamps,
MBXSettingsCoreRenderingCollisionBoxes,
MBXSettingsCoreRenderingOverdrawVisualization,
};
typedef NS_ENUM(NSInteger, MBXSettingsAnnotationsRows) {
MBXSettingsAnnotations100Views = 0,
MBXSettingsAnnotations1000Views,
MBXSettingsAnnotations10000Views,
MBXSettingsAnnotations100Sprites,
MBXSettingsAnnotations1000Sprites,
MBXSettingsAnnotations10000Sprites,
MBXSettingsAnnotationsTestShapes,
MBXSettingsAnnotationsCustomCallout,
MBXSettingsAnnotationsQueryAnnotations,
MBXSettingsAnnotationsRemoveAnnotations,
};
typedef NS_ENUM(NSInteger, MBXSettingsRuntimeStylingRows) {
MBXSettingsRuntimeStylingWater = 0,
MBXSettingsRuntimeStylingRoads,
MBXSettingsRuntimeStylingRaster,
MBXSettingsRuntimeStylingGeoJSON,
MBXSettingsRuntimeStylingSymbols,
MBXSettingsRuntimeStylingBuildings,
MBXSettingsRuntimeStylingFerry,
MBXSettingsRuntimeStylingParks,
MBXSettingsRuntimeStylingFilteredFill,
MBXSettingsRuntimeStylingFilteredLines,
MBXSettingsRuntimeStylingNumericFilteredFill,
MBXSettingsRuntimeStylingStyleQuery,
MBXSettingsRuntimeStylingFeatureSource,
MBXSettingsRuntimeStylingPointCollection,
MBXSettingsRuntimeStylingUpdateGeoJSONSourceData,
MBXSettingsRuntimeStylingUpdateGeoJSONSourceURL,
MBXSettingsRuntimeStylingUpdateGeoJSONSourceFeatures,
MBXSettingsRuntimeStylingVectorSource,
MBXSettingsRuntimeStylingRasterSource,
MBXSettingsRuntimeStylingCountryLabels,
};
typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
MBXSettingsMiscellaneousShowReuseQueueStats = 0,
MBXSettingsMiscellaneousWorldTour,
MBXSettingsMiscellaneousCustomUserDot,
MBXSettingsMiscellaneousPrintLogFile,
MBXSettingsMiscellaneousDeleteLogFile,
};
@interface MBXDroppedPinAnnotation : MGLPointAnnotation
@end
@implementation MBXDroppedPinAnnotation
@end
@interface MBXCustomCalloutAnnotation : MGLPointAnnotation
@end
@implementation MBXCustomCalloutAnnotation
@end
@interface MBXSpriteBackedAnnotation : MGLPointAnnotation
@end
@implementation MBXSpriteBackedAnnotation
@end
@interface MBXViewController () <UITableViewDelegate,
UITableViewDataSource,
MGLMapViewDelegate>
@property (nonatomic) IBOutlet MGLMapView *mapView;
@property (weak, nonatomic) IBOutlet UILabel *hudLabel;
@property (nonatomic) NSInteger styleIndex;
@property (nonatomic) BOOL debugLoggingEnabled;
@property (nonatomic) BOOL customUserLocationAnnnotationEnabled;
@property (nonatomic) BOOL usingLocaleBasedCountryLabels;
@property (nonatomic) BOOL reuseQueueStatsEnabled;
@end
@interface MGLMapView (MBXViewController)
@property (nonatomic) BOOL usingLocaleBasedCountryLabels;
@property (nonatomic) NSDictionary *annotationViewReuseQueueByIdentifier;
@end
@implementation MBXViewController
{
BOOL _isTouringWorld;
}
#pragma mark - Setup & Teardown
+ (void)initialize
{
if (self == [MBXViewController class])
{
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"MBXUserTrackingMode": @(MGLUserTrackingModeNone),
@"MBXShowsUserLocation": @NO,
@"MBXDebug": @NO,
}];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveState:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restoreState:) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveState:) name:UIApplicationWillTerminateNotification object:nil];
[self restoreState:nil];
self.debugLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsDebugLoggingEnabled"];
self.hudLabel.hidden = YES;
if ([MGLAccountManager accessToken].length)
{
self.styleIndex = -1;
[self cycleStyles:self];
}
else
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Access Token" message:@"Enter your Mapbox access token to load Mapbox-hosted tiles and styles:" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField)
{
textField.keyboardType = UIKeyboardTypeURL;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
UITextField *textField = alertController.textFields.firstObject;
NSString *accessToken = textField.text;
[[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:MBXMapboxAccessTokenDefaultsKey];
[MGLAccountManager setAccessToken:accessToken];
self.styleIndex = -1;
[self cycleStyles:self];
[self.mapView reloadStyle:self];
}];
[alertController addAction:OKAction];
if ([alertController respondsToSelector:@selector(setPreferredAction:)])
{
alertController.preferredAction = OKAction;
}
[self presentViewController:alertController animated:YES completion:nil];
}
}
- (void)saveState:(__unused NSNotification *)notification
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *archivedCamera = [NSKeyedArchiver archivedDataWithRootObject:self.mapView.camera];
[defaults setObject:archivedCamera forKey:@"MBXCamera"];
[defaults setInteger:self.mapView.userTrackingMode forKey:@"MBXUserTrackingMode"];
[defaults setBool:self.mapView.showsUserLocation forKey:@"MBXShowsUserLocation"];
[defaults setInteger:self.mapView.debugMask forKey:@"MBXDebugMask"];
[defaults synchronize];
}
- (void)restoreState:(__unused NSNotification *)notification
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *archivedCamera = [defaults objectForKey:@"MBXCamera"];
MGLMapCamera *camera = archivedCamera ? [NSKeyedUnarchiver unarchiveObjectWithData:archivedCamera] : nil;
if (camera)
{
self.mapView.camera = camera;
}
NSInteger uncheckedTrackingMode = [defaults integerForKey:@"MBXUserTrackingMode"];
if (uncheckedTrackingMode >= 0 &&
(NSUInteger)uncheckedTrackingMode >= MGLUserTrackingModeNone &&
(NSUInteger)uncheckedTrackingMode <= MGLUserTrackingModeFollowWithCourse)
{
self.mapView.userTrackingMode = (MGLUserTrackingMode)uncheckedTrackingMode;
}
self.mapView.showsUserLocation = [defaults boolForKey:@"MBXShowsUserLocation"];
NSInteger uncheckedDebugMask = [defaults integerForKey:@"MBXDebugMask"];
if (uncheckedDebugMask >= 0)
{
self.mapView.debugMask = (MGLMapDebugMaskOptions)uncheckedDebugMask;
}
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(__unused id)sender {
if ([segue.identifier isEqualToString:@"ShowOfflinePacks"]) {
MBXOfflinePacksTableViewController *controller = [segue destinationViewController];
controller.mapView = self.mapView;
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self saveState:nil];
}
#pragma mark - Debugging Interface
- (IBAction)showSettings:(__unused id)sender
{
UITableViewController *settingsViewController = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
settingsViewController.tableView.delegate = self;
settingsViewController.tableView.dataSource = self;
settingsViewController.title = @"Debugging";
settingsViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissSettings:)];
UINavigationController *wrapper = [[UINavigationController alloc] initWithRootViewController:settingsViewController];
wrapper.navigationBar.tintColor = self.navigationController.navigationBar.tintColor;
[self.navigationController presentViewController:wrapper animated:YES completion:nil];
}
- (void)dismissSettings:(__unused id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (NSArray <NSString *> *)settingsSectionTitles
{
return @[
@"Core Rendering",
@"Annotations",
@"Runtime Styling",
@"Miscellaneous"
];
}
- (NSArray <NSString *> *)settingsTitlesForSection:(NSInteger)section
{
NSMutableArray *settingsTitles = [NSMutableArray array];
MGLMapDebugMaskOptions debugMask = self.mapView.debugMask;
switch (section)
{
case MBXSettingsCoreRendering:
[settingsTitles addObjectsFromArray:@[
@"Reset Position",
[NSString stringWithFormat:@"%@ Tile Boundaries",
(debugMask & MGLMapDebugTileBoundariesMask ? @"Hide" :@"Show")],
[NSString stringWithFormat:@"%@ Tile Info",
(debugMask & MGLMapDebugTileInfoMask ? @"Hide" :@"Show")],
[NSString stringWithFormat:@"%@ Tile Timestamps",
(debugMask & MGLMapDebugTimestampsMask ? @"Hide" :@"Show")],
[NSString stringWithFormat:@"%@ Collision Boxes",
(debugMask & MGLMapDebugCollisionBoxesMask ? @"Hide" :@"Show")],
[NSString stringWithFormat:@"%@ Overdraw Visualization",
(debugMask & MGLMapDebugOverdrawVisualizationMask ? @"Hide" :@"Show")],
]];
break;
case MBXSettingsAnnotations:
[settingsTitles addObjectsFromArray:@[
@"Add 100 Views",
@"Add 1,000 Views",
@"Add 10,000 Views",
@"Add 100 Sprites",
@"Add 1,000 Sprites",
@"Add 10,000 Sprites",
@"Add Test Shapes",
@"Add Point With Custom Callout",
@"Query Annotations",
@"Remove Annotations",
]];
break;
case MBXSettingsRuntimeStyling:
[settingsTitles addObjectsFromArray:@[
@"Style Water With Function",
@"Style Roads With Function",
@"Add Raster & Apply Function",
@"Add GeoJSON & Apply Fill",
@"Style Symbol Color",
@"Style Building Fill Color",
@"Style Ferry Line Color",
@"Remove Parks",
@"Style Fill With Filter",
@"Style Lines With Filter",
@"Style Fill With Numeric Filter",
@"Style Query For GeoJSON",
@"Style Feature",
@"Style Dynamic Point Collection",
@"Update GeoJSON Source: Data",
@"Update GeoJSON Source: URL",
@"Update GeoJSON Source: Features",
@"Style Vector Source",
@"Style Raster Source",
[NSString stringWithFormat:@"Label Countries in %@", (_usingLocaleBasedCountryLabels ? @"Local Language" : [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[self bestLanguageForUser]])],
]];
break;
case MBXSettingsMiscellaneous:
[settingsTitles addObject:@"Show Reuse Queue Stats"];
[settingsTitles addObjectsFromArray:@[
@"Start World Tour",
[NSString stringWithFormat:@"%@ Custom User Dot", (_customUserLocationAnnnotationEnabled ? @"Disable" : @"Enable")],
]];
if (self.debugLoggingEnabled)
{
[settingsTitles addObjectsFromArray:@[
@"Print Telemetry Logfile",
@"Delete Telemetry Logfile",
]];
};
break;
default:
NSAssert(NO, @"All settings sections should be implemented");
break;
}
return settingsTitles;
}
- (void)performActionForSettingAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section)
{
case MBXSettingsCoreRendering:
switch (indexPath.row)
{
case MBXSettingsCoreRenderingResetPosition:
[self.mapView resetPosition];
break;
case MBXSettingsCoreRenderingTileBoundaries:
self.mapView.debugMask ^= MGLMapDebugTileBoundariesMask;
break;
case MBXSettingsCoreRenderingTileInfo:
self.mapView.debugMask ^= MGLMapDebugTileInfoMask;
break;
case MBXSettingsCoreRenderingTimestamps:
self.mapView.debugMask ^= MGLMapDebugTimestampsMask;
break;
case MBXSettingsCoreRenderingCollisionBoxes:
self.mapView.debugMask ^= MGLMapDebugCollisionBoxesMask;
break;
case MBXSettingsCoreRenderingOverdrawVisualization:
self.mapView.debugMask ^= MGLMapDebugOverdrawVisualizationMask;
break;
default:
NSAssert(NO, @"All core rendering setting rows should be implemented");
break;
}
break;
case MBXSettingsAnnotations:
switch (indexPath.row)
{
case MBXSettingsAnnotations100Views:
[self parseFeaturesAddingCount:100 usingViews:YES];
break;
case MBXSettingsAnnotations1000Views:
[self parseFeaturesAddingCount:1000 usingViews:YES];
break;
case MBXSettingsAnnotations10000Views:
[self parseFeaturesAddingCount:10000 usingViews:YES];
break;
case MBXSettingsAnnotations100Sprites:
[self parseFeaturesAddingCount:100 usingViews:NO];
break;
case MBXSettingsAnnotations1000Sprites:
[self parseFeaturesAddingCount:1000 usingViews:NO];
break;
case MBXSettingsAnnotations10000Sprites:
[self parseFeaturesAddingCount:10000 usingViews:NO];
break;
case MBXSettingsAnnotationsTestShapes:
[self addTestShapes];
break;
case MBXSettingsAnnotationsCustomCallout:
[self addAnnotationWithCustomCallout];
break;
case MBXSettingsAnnotationsQueryAnnotations:
[self testQueryPointAnnotations];
break;
case MBXSettingsAnnotationsRemoveAnnotations:
[self.mapView removeAnnotations:self.mapView.annotations];
break;
default:
NSAssert(NO, @"All annotations setting rows should be implemented");
break;
}
break;
case MBXSettingsRuntimeStyling:
switch (indexPath.row)
{
case MBXSettingsRuntimeStylingWater:
[self styleWaterLayer];
break;
case MBXSettingsRuntimeStylingRoads:
[self styleRoadLayer];
break;
case MBXSettingsRuntimeStylingRaster:
[self styleRasterLayer];
break;
case MBXSettingsRuntimeStylingGeoJSON:
[self styleGeoJSONSource];
break;
case MBXSettingsRuntimeStylingSymbols:
[self styleSymbolLayer];
break;
case MBXSettingsRuntimeStylingBuildings:
[self styleBuildingLayer];
break;
case MBXSettingsRuntimeStylingFerry:
[self styleFerryLayer];
break;
case MBXSettingsRuntimeStylingParks:
[self removeParkLayer];
break;
case MBXSettingsRuntimeStylingFilteredFill:
[self styleFilteredFill];
break;
case MBXSettingsRuntimeStylingFilteredLines:
[self styleFilteredLines];
break;
case MBXSettingsRuntimeStylingNumericFilteredFill:
[self styleNumericFilteredFills];
break;
case MBXSettingsRuntimeStylingStyleQuery:
[self styleQuery];
break;
case MBXSettingsRuntimeStylingFeatureSource:
[self styleFeature];
break;
case MBXSettingsRuntimeStylingPointCollection:
[self styleDynamicPointCollection];
break;
case MBXSettingsRuntimeStylingUpdateGeoJSONSourceURL:
[self updateGeoJSONSourceURL];
break;
case MBXSettingsRuntimeStylingUpdateGeoJSONSourceData:
[self updateGeoJSONSourceData];
break;
case MBXSettingsRuntimeStylingUpdateGeoJSONSourceFeatures:
[self updateGeoJSONSourceFeatures];
break;
case MBXSettingsRuntimeStylingVectorSource:
[self styleVectorSource];
break;
case MBXSettingsRuntimeStylingRasterSource:
[self styleRasterSource];
break;
case MBXSettingsRuntimeStylingCountryLabels:
[self styleCountryLabelsLanguage];
break;
default:
NSAssert(NO, @"All runtime styling setting rows should be implemented");
break;
}
break;
case MBXSettingsMiscellaneous:
switch (indexPath.row)
{
case MBXSettingsMiscellaneousWorldTour:
[self startWorldTour];
break;
case MBXSettingsMiscellaneousCustomUserDot:
[self toggleCustomUserDot];
break;
case MBXSettingsMiscellaneousPrintLogFile:
[self printTelemetryLogFile];
break;
case MBXSettingsMiscellaneousDeleteLogFile:
[self deleteTelemetryLogFile];
break;
case MBXSettingsMiscellaneousShowReuseQueueStats:
{
self.reuseQueueStatsEnabled = YES;
self.hudLabel.hidden = NO;
break;
}
default:
NSAssert(NO, @"All miscellaneous setting rows should be implemented");
break;
}
break;
default:
NSAssert(NO, @"All settings sections should be implemented");
break;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self settingsSectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self settingsTitlesForSection:section] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
{
return [[self settingsSectionTitles] objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = [[self settingsTitlesForSection:indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[self dismissViewControllerAnimated:YES completion:^
{
[self performActionForSettingAtIndexPath:indexPath];
}];
}
#pragma mark - Debugging Actions
- (void)parseFeaturesAddingCount:(NSUInteger)featuresCount usingViews:(BOOL)useViews
{
[self.mapView removeAnnotations:self.mapView.annotations];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
NSData *featuresData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"points" ofType:@"geojson"]];
id features = [NSJSONSerialization JSONObjectWithData:featuresData
options:0
error:nil];
if ([features isKindOfClass:[NSDictionary class]])
{
NSMutableArray *annotations = [NSMutableArray array];
for (NSDictionary *feature in features[@"features"])
{
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([feature[@"geometry"][@"coordinates"][1] doubleValue],
[feature[@"geometry"][@"coordinates"][0] doubleValue]);
NSString *title = feature[@"properties"][@"NAME"];
MGLPointAnnotation *annotation = (useViews ? [MGLPointAnnotation new] : [MBXSpriteBackedAnnotation new]);
annotation.coordinate = coordinate;
annotation.title = title;
[annotations addObject:annotation];
if (annotations.count == featuresCount) break;
}
dispatch_async(dispatch_get_main_queue(), ^
{
[self.mapView addAnnotations:annotations];
[self.mapView showAnnotations:annotations animated:YES];
});
}
});
}
- (void)addTestShapes
{
// Pacific Northwest triangle
//
CLLocationCoordinate2D triangleCoordinates[3] =
{
CLLocationCoordinate2DMake(44, -122),
CLLocationCoordinate2DMake(46, -122),
CLLocationCoordinate2DMake(46, -121)
};
MGLPolygon *triangle = [MGLPolygon polygonWithCoordinates:triangleCoordinates count:3];
[self.mapView addAnnotation:triangle];
// Orcas Island, WA hike polyline
//
NSDictionary *hike = [NSJSONSerialization JSONObjectWithData:
[NSData dataWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"polyline" ofType:@"geojson"]]
options:0
error:nil];
NSArray *hikeCoordinatePairs = hike[@"features"][0][@"geometry"][@"coordinates"];
CLLocationCoordinate2D *polylineCoordinates = (CLLocationCoordinate2D *)malloc([hikeCoordinatePairs count] * sizeof(CLLocationCoordinate2D));
for (NSUInteger i = 0; i < [hikeCoordinatePairs count]; i++)
{
polylineCoordinates[i] = CLLocationCoordinate2DMake([hikeCoordinatePairs[i][1] doubleValue], [hikeCoordinatePairs[i][0] doubleValue]);
}
MGLPolyline *polyline = [MGLPolyline polylineWithCoordinates:polylineCoordinates
count:[hikeCoordinatePairs count]];
[self.mapView addAnnotation:polyline];
free(polylineCoordinates);
// PA/NJ/DE polygons
//
NSDictionary *threestates = [NSJSONSerialization JSONObjectWithData:
[NSData dataWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"threestates" ofType:@"geojson"]]
options:0
error:nil];
for (NSDictionary *feature in threestates[@"features"])
{
NSArray *stateCoordinatePairs = feature[@"geometry"][@"coordinates"];
while ([stateCoordinatePairs count] == 1) stateCoordinatePairs = stateCoordinatePairs[0];
CLLocationCoordinate2D *polygonCoordinates = (CLLocationCoordinate2D *)malloc([stateCoordinatePairs count] * sizeof(CLLocationCoordinate2D));
for (NSUInteger i = 0; i < [stateCoordinatePairs count]; i++)
{
polygonCoordinates[i] = CLLocationCoordinate2DMake([stateCoordinatePairs[i][1] doubleValue], [stateCoordinatePairs[i][0] doubleValue]);
}
MGLPolygon *polygon = [MGLPolygon polygonWithCoordinates:polygonCoordinates count:[stateCoordinatePairs count]];
[self.mapView addAnnotation:polygon];
free(polygonCoordinates);
}
// Null Island polygon with an interior hole
//
CLLocationCoordinate2D innerCoordinates[] = {
CLLocationCoordinate2DMake(-5, -5),
CLLocationCoordinate2DMake(-5, 5),
CLLocationCoordinate2DMake(5, 5),
CLLocationCoordinate2DMake(5, -5),
};
MGLPolygon *innerPolygon = [MGLPolygon polygonWithCoordinates:innerCoordinates count:sizeof(innerCoordinates) / sizeof(innerCoordinates[0])];
CLLocationCoordinate2D outerCoordinates[] = {
CLLocationCoordinate2DMake(-10, -10),
CLLocationCoordinate2DMake(-10, 10),
CLLocationCoordinate2DMake(10, 10),
CLLocationCoordinate2DMake(10, -10),
};
MGLPolygon *outerPolygon = [MGLPolygon polygonWithCoordinates:outerCoordinates count:sizeof(outerCoordinates) / sizeof(outerCoordinates[0]) interiorPolygons:@[innerPolygon]];
[self.mapView addAnnotation:outerPolygon];
}
- (void)addAnnotationWithCustomCallout
{
[self.mapView removeAnnotations:self.mapView.annotations];
MBXCustomCalloutAnnotation *annotation = [[MBXCustomCalloutAnnotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(48.8533940, 2.3775439);
annotation.title = @"Custom Callout";
[self.mapView addAnnotation:annotation];
[self.mapView showAnnotations:@[annotation] animated:YES];
}
- (void)styleWaterLayer
{
MGLFillStyleLayer *waterLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"water"];
MGLStyleValue *waterColorFunction = [MGLStyleValue<UIColor *> valueWithStops:@{
@6.0f: [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor yellowColor]],
@8.0f: [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor blueColor]],
@10.0f: [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor redColor]],
@12.0f: [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor greenColor]],
@14.0f: [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor blueColor]],
}];
waterLayer.fillColor = waterColorFunction;
MGLStyleValue *fillAntialias = [MGLStyleValue<NSNumber *> valueWithStops:@{
@11: [MGLStyleValue<NSNumber *> valueWithRawValue:@YES],
@12: [MGLStyleValue<NSNumber *> valueWithRawValue:@NO],
@13: [MGLStyleValue<NSNumber *> valueWithRawValue:@YES],
@14: [MGLStyleValue<NSNumber *> valueWithRawValue:@NO],
@15: [MGLStyleValue<NSNumber *> valueWithRawValue:@YES],
}];
waterLayer.fillAntialias = fillAntialias;
}
- (void)styleRoadLayer
{
MGLLineStyleLayer *roadLayer = (MGLLineStyleLayer *)[self.mapView.style layerWithIdentifier:@"road-primary"];
roadLayer.lineColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor blackColor]];
MGLStyleValue *lineWidthFunction = [MGLStyleValue<NSNumber *> valueWithStops:@{}];
MGLStyleValue *roadLineColor = [MGLStyleValue<UIColor *> valueWithStops:@{
@10: [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor purpleColor]],
@13: [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor yellowColor]],
@16: [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor cyanColor]],
}];
roadLayer.lineColor = roadLineColor;
roadLayer.lineWidth = lineWidthFunction;
roadLayer.lineGapWidth = lineWidthFunction;
roadLayer.visible = YES;
roadLayer.maximumZoomLevel = 15;
roadLayer.minimumZoomLevel = 13;
}
- (void)styleRasterLayer
{
NSURL *rasterURL = [NSURL URLWithString:@"mapbox://mapbox.satellite"];
MGLRasterSource *rasterSource = [[MGLRasterSource alloc] initWithIdentifier:@"my-raster-source" URL:rasterURL tileSize:512];
[self.mapView.style addSource:rasterSource];
MGLRasterStyleLayer *rasterLayer = [[MGLRasterStyleLayer alloc] initWithIdentifier:@"my-raster-layer" source:rasterSource];
MGLStyleValue *opacityFunction = [MGLStyleValue<NSNumber *> valueWithStops:@{
@20.0f: [MGLStyleValue<NSNumber *> valueWithRawValue:@1.0f],
@5.0f: [MGLStyleValue<NSNumber *> valueWithRawValue:@0.0f],
}];
rasterLayer.rasterOpacity = opacityFunction;
[self.mapView.style addLayer:rasterLayer];
}
- (void)styleGeoJSONSource
{
NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"];
NSURL *geoJSONURL = [NSURL fileURLWithPath:filePath];
MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"ams" URL:geoJSONURL options:nil];
[self.mapView.style addSource:source];
MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"test" source:source];
fillLayer.fillColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor purpleColor]];
[self.mapView.style addLayer:fillLayer];
}
- (void)styleSymbolLayer
{
MGLSymbolStyleLayer *stateLayer = (MGLSymbolStyleLayer *)[self.mapView.style layerWithIdentifier:@"state-label-lg"];
stateLayer.textColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor redColor]];
}
- (void)styleBuildingLayer
{
MGLFillStyleLayer *buildingLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"building"];
buildingLayer.fillColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor blackColor]];
}
- (void)styleFerryLayer
{
MGLLineStyleLayer *ferryLineLayer = (MGLLineStyleLayer *)[self.mapView.style layerWithIdentifier:@"ferry"];
ferryLineLayer.lineColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor redColor]];
}
- (void)removeParkLayer
{
MGLFillStyleLayer *parkLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"park"];
[self.mapView.style removeLayer:parkLayer];
}
- (void)styleFilteredFill
{
// set style and focus on Texas
[self.mapView setStyleURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"fill_filter_style" ofType:@"json"]]];
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(31, -100) zoomLevel:3 animated:NO];
// after slight delay, fill in Texas (atypical use; we want to clearly see the change for test purposes)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
MGLFillStyleLayer *statesLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"states"];
// filter
statesLayer.predicate = [NSPredicate predicateWithFormat:@"name == 'Texas'"];
// paint properties
statesLayer.fillColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor redColor]];
statesLayer.fillOpacity = [MGLStyleValue<NSNumber *> valueWithRawValue:@0.25];
});
}
- (void)styleFilteredLines
{
// set style and focus on lower 48
[self.mapView setStyleURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"line_filter_style" ofType:@"json"]]];
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(40, -97) zoomLevel:5 animated:NO];
// after slight delay, change styling for all Washington-named counties (atypical use; we want to clearly see the change for test purposes)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
MGLLineStyleLayer *countiesLayer = (MGLLineStyleLayer *)[self.mapView.style layerWithIdentifier:@"counties"];
// filter
countiesLayer.predicate = [NSPredicate predicateWithFormat:@"NAME10 == 'Washington'"];
// paint properties
countiesLayer.lineColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor redColor]];
countiesLayer.lineOpacity = [MGLStyleValue<NSNumber *> valueWithRawValue:@0.75];
countiesLayer.lineWidth = [MGLStyleValue<NSNumber *> valueWithRawValue:@5];
});
}
- (void)styleNumericFilteredFills
{
// set style and focus on lower 48
[self.mapView setStyleURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"numeric_filter_style" ofType:@"json"]]];
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(40, -97) zoomLevel:5 animated:NO];
// after slight delay, change styling for regions 200-299 (atypical use; we want to clearly see the change for test purposes)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
MGLFillStyleLayer *regionsLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"regions"];
// filter (testing both inline and format strings)
regionsLayer.predicate = [NSPredicate predicateWithFormat:@"HRRNUM >= %@ AND HRRNUM < 300", @(200)];
// paint properties
regionsLayer.fillColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor blueColor]];
regionsLayer.fillOpacity = [MGLStyleValue<NSNumber *> valueWithRawValue:@0.5];
});
}
- (void)styleQuery
{
CGRect queryRect = CGRectInset(self.mapView.bounds, 100, 200);
NSArray *features = [self.mapView visibleFeaturesInRect:queryRect];
NSString *querySourceID = @"query-source-id";
NSString *queryLayerID = @"query-layer-id";
// RTE if you don't remove the layer first
// RTE if you pass a nill layer to remove layer
MGLStyleLayer *layer = [self.mapView.style layerWithIdentifier:queryLayerID];
if (layer) {
[self.mapView.style removeLayer:layer];
}
// RTE if you pass a nill source to remove source
MGLSource *source = [self.mapView.style sourceWithIdentifier:querySourceID];
if (source) {
[self.mapView.style removeSource:source];
}
dispatch_async(dispatch_get_main_queue(), ^{
MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:querySourceID features:features options:nil];
[self.mapView.style addSource:source];
MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:queryLayerID source:source];
fillLayer.fillColor = [MGLStyleConstantValue<UIColor *> valueWithRawValue:[UIColor blueColor]];
fillLayer.fillOpacity = [MGLStyleConstantValue<NSNumber *> valueWithRawValue:@0.5];
[self.mapView.style addLayer:fillLayer];
});
}
- (void)styleFeature
{
self.mapView.zoomLevel = 10;
self.mapView.centerCoordinate = CLLocationCoordinate2DMake(51.068585180672635, -114.06074523925781);
CLLocationCoordinate2D leafCoords[] = {
CLLocationCoordinate2DMake(50.9683733218221,-114.07035827636719),
CLLocationCoordinate2DMake(51.02325750523972,-114.06967163085938),
CLLocationCoordinate2DMake(51.009434536947786,-114.14245605468749),
CLLocationCoordinate2DMake(51.030599281184124,-114.12597656249999),
CLLocationCoordinate2DMake(51.060386316691016,-114.21043395996094),
CLLocationCoordinate2DMake(51.063838646941576,-114.17816162109375),
CLLocationCoordinate2DMake(51.08152779888779,-114.19876098632812),
CLLocationCoordinate2DMake(51.08066507029602,-114.16854858398438),
CLLocationCoordinate2DMake(51.09662294502995,-114.17472839355469),
CLLocationCoordinate2DMake(51.07764539352731,-114.114990234375),
CLLocationCoordinate2DMake(51.13670896949613,-114.12391662597656),
CLLocationCoordinate2DMake(51.13369295212583,-114.09576416015624),
CLLocationCoordinate2DMake(51.17546878815025,-114.07585144042969),
CLLocationCoordinate2DMake(51.140155605265896,-114.04632568359375),
CLLocationCoordinate2DMake(51.15049396880196,-114.01542663574219),
CLLocationCoordinate2DMake(51.088860342359965,-114.00924682617186),
CLLocationCoordinate2DMake(51.12205789681453,-113.94813537597656),
CLLocationCoordinate2DMake(51.106539930027225,-113.94882202148438),
CLLocationCoordinate2DMake(51.117747873223344,-113.92616271972656),
CLLocationCoordinate2DMake(51.10093493903458,-113.92616271972656),
CLLocationCoordinate2DMake(51.10697105503078,-113.90625),
CLLocationCoordinate2DMake(51.09144802136697,-113.9117431640625),
CLLocationCoordinate2DMake(51.04916446529361,-113.97010803222655),
CLLocationCoordinate2DMake(51.045279344649146,-113.9398956298828),
CLLocationCoordinate2DMake(51.022825599852496,-114.06211853027344),
CLLocationCoordinate2DMake(51.045279344649146,-113.9398956298828),
CLLocationCoordinate2DMake(51.022825599852496,-114.06211853027344),
CLLocationCoordinate2DMake(51.022825599852496,-114.06280517578125),
CLLocationCoordinate2DMake(50.968805734317804,-114.06280517578125),
CLLocationCoordinate2DMake(50.9683733218221,-114.07035827636719),
};
NSUInteger coordsCount = sizeof(leafCoords) / sizeof(leafCoords[0]);
MGLPolygonFeature *feature = [MGLPolygonFeature polygonWithCoordinates:leafCoords count:coordsCount];
feature.identifier = @"leaf-feature";
feature.attributes = @{@"color": @"red"};
MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"leaf-source" features:@[feature] options:nil];
[self.mapView.style addSource:source];
MGLFillStyleLayer *layer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"leaf-fill-layer" source:source];
layer.predicate = [NSPredicate predicateWithFormat:@"color = %@", @"red"];
MGLStyleValue *fillColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor redColor]];
layer.fillColor = fillColor;
[self.mapView.style addLayer:layer];
}
- (void)updateGeoJSONSourceData
{
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(40.329795743702064, -107.75390625) zoomLevel:11 animated:NO];
NSString *geoJSON = @"{\"type\": \"FeatureCollection\",\"features\": [{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"LineString\",\"coordinates\": [[-107.75390625,40.329795743702064],[-104.34814453125,37.64903402157866]]}}]}";
NSData *data = [geoJSON dataUsingEncoding:NSUTF8StringEncoding];
MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"mutable-data-source-id" geoJSONData:data options:nil];
[self.mapView.style addSource:source];
MGLLineStyleLayer *layer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"mutable-data-layer-id" source:source];
[self.mapView.style addLayer:layer];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSString *geoJSON = @"{\"type\": \"FeatureCollection\",\"features\": [{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"LineString\",\"coordinates\": [[-107.75390625,40.329795743702064],[-109.34814453125,37.64903402157866]]}}]}";
NSData *data = [geoJSON dataUsingEncoding:NSUTF8StringEncoding];
source.geoJSONData = data;
});
}
- (void)updateGeoJSONSourceURL
{
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(48.668731, -122.857151) zoomLevel:11 animated:NO];
NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"polyline" ofType:@"geojson"];
NSURL *geoJSONURL = [NSURL fileURLWithPath:filePath];
MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"mutable-data-source-url-id" URL:geoJSONURL options:nil];
[self.mapView.style addSource:source];
MGLLineStyleLayer *layer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"mutable-data-layer-url-id" source:source];
[self.mapView.style addLayer:layer];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(41.563986787078704, -75.04843935793578) zoomLevel:8 animated:NO];
NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"threestates" ofType:@"geojson"];
NSURL *geoJSONURL = [NSURL fileURLWithPath:filePath];
source.URL = geoJSONURL;
});
}
- (void)updateGeoJSONSourceFeatures
{
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(-41.1520, 288.6592) zoomLevel:10 animated:NO];
CLLocationCoordinate2D smallBox[] = {
{-41.14763798539186, 288.68019104003906},
{-41.140915920129665, 288.68019104003906},
{-41.140915920129665, 288.6887741088867},
{-41.14763798539186, 288.6887741088867},
{-41.14763798539186, 288.68019104003906}
};
CLLocationCoordinate2D largeBox[] = {