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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
|
#import "MBXViewController.h"
#import "MBXAppDelegate.h"
#import "MBXCustomCalloutView.h"
#import "MBXOfflinePacksTableViewController.h"
#import "MBXAnnotationView.h"
#import "MBXUserLocationAnnotationView.h"
#import "LimeGreenStyleLayer.h"
#import "MBXEmbeddedMapViewController.h"
#import "MBXOrnamentsViewController.h"
#import "MBXStateManager.h"
#import "MBXState.h"
#import "MBXFrameTimeGraphView.h"
#import <Mapbox/Mapbox.h>
#import "../src/MGLMapView_Experimental.h"
#import <objc/runtime.h>
static const CLLocationCoordinate2D WorldTourDestinations[] = {
{ .latitude = 38.8999418, .longitude = -77.033996 },
{ .latitude = 37.7884307, .longitude = -122.3998631 },
{ .latitude = 52.5003103, .longitude = 13.4197763 },
{ .latitude = 60.1712627, .longitude = 24.9378866 },
{ .latitude = 53.8948782, .longitude = 27.5558476 },
};
static const MGLCoordinateBounds colorado = {
.sw = { .latitude = 36.986207, .longitude = -109.049896},
.ne = { .latitude = 40.989329, .longitude = -102.062592},
};
static NSString * const MBXViewControllerAnnotationViewReuseIdentifer = @"MBXViewControllerAnnotationViewReuseIdentifer";
typedef NS_ENUM(NSInteger, MBXSettingsSections) {
MBXSettingsDebugTools = 0,
MBXSettingsAnnotations,
MBXSettingsRuntimeStyling,
MBXSettingsMiscellaneous,
};
typedef NS_ENUM(NSInteger, MBXSettingsDebugToolsRows) {
MBXSettingsDebugToolsResetPosition = 0,
MBXSettingsDebugToolsTileBoundaries,
MBXSettingsDebugToolsTileInfo,
MBXSettingsDebugToolsTimestamps,
MBXSettingsDebugToolsCollisionBoxes,
MBXSettingsDebugToolsOverdrawVisualization,
MBXSettingsDebugToolsShowZoomLevel,
MBXSettingsDebugToolsShowFrameTimeGraph,
MBXSettingsDebugToolsShowReuseQueueStats
};
typedef NS_ENUM(NSInteger, MBXSettingsAnnotationsRows) {
MBXSettingsAnnotations100Views = 0,
MBXSettingsAnnotations1000Views,
MBXSettingsAnnotations10000Views,
MBXSettingsAnnotations100Sprites,
MBXSettingsAnnotations1000Sprites,
MBXSettingsAnnotations10000Sprites,
MBXSettingsAnnotationAnimation,
MBXSettingsAnnotationsTestShapes,
MBXSettingsAnnotationsCustomCallout,
MBXSettingsAnnotationsQueryAnnotations,
MBXSettingsAnnotationsCustomUserDot,
MBXSettingsAnnotationsRemoveAnnotations,
MBXSettingsAnnotationSelectRandomOffscreenPointAnnotation,
MBXSettingsAnnotationCenterSelectedAnnotation,
MBXSettingsAnnotationAddVisibleAreaPolyline
};
typedef NS_ENUM(NSInteger, MBXSettingsRuntimeStylingRows) {
MBXSettingsRuntimeStylingBuildingExtrusions = 0,
MBXSettingsRuntimeStylingWater,
MBXSettingsRuntimeStylingRoads,
MBXSettingsRuntimeStylingRaster,
MBXSettingsRuntimeStylingShape,
MBXSettingsRuntimeStylingSymbols,
MBXSettingsRuntimeStylingBuildings,
MBXSettingsRuntimeStylingFerry,
MBXSettingsRuntimeStylingParks,
MBXSettingsRuntimeStylingFilteredFill,
MBXSettingsRuntimeStylingFilteredLines,
MBXSettingsRuntimeStylingNumericFilteredFill,
MBXSettingsRuntimeStylingStyleQuery,
MBXSettingsRuntimeStylingFeatureSource,
MBXSettingsRuntimeStylingPointCollection,
MBXSettingsRuntimeStylingUpdateShapeSourceData,
MBXSettingsRuntimeStylingUpdateShapeSourceURL,
MBXSettingsRuntimeStylingUpdateShapeSourceFeatures,
MBXSettingsRuntimeStylingVectorTileSource,
MBXSettingsRuntimeStylingRasterTileSource,
MBXSettingsRuntimeStylingImageSource,
MBXSettingsRuntimeStylingRouteLine,
MBXSettingsRuntimeStylingAddLimeGreenTriangleLayer,
MBXSettingsRuntimeStylingDDSPolygon,
MBXSettingsRuntimeStylingCustomLatLonGrid,
};
typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
MBXSettingsMiscellaneousWorldTour,
MBXSettingsMiscellaneousRandomTour,
MBXSettingsMiscellaneousScrollView,
MBXSettingsMiscellaneousToggleTwoMaps,
MBXSettingsMiscellaneousLocalizeLabels,
MBXSettingsMiscellaneousShowSnapshots,
MBXSettingsMiscellaneousMissingIcon,
MBXSettingsMiscellaneousShouldLimitCameraChanges,
MBXSettingsMiscellaneousSetContentInsets,
MBXSettingsMiscellaneousShowCustomLocationManager,
MBXSettingsMiscellaneousOrnamentsPlacement,
MBXSettingsMiscellaneousPrintLogFile,
MBXSettingsMiscellaneousDeleteLogFile
};
// Utility methods
CLLocationCoordinate2D coordinateCentered(CLLocationCoordinate2D origin, CLLocationDegrees bearing, CLLocationDistance distance) {
// Convert to radians
double multiplier = M_PI / 180.0;
double sourceLatitude = multiplier * origin.latitude;
double sourceLongitude = multiplier * origin.longitude;
bearing *= multiplier;
distance /= 6378137.0;
// Pulled from MGLRadianCoordinateAtDistanceFacingDirection:
double latitude = asin((sin(sourceLatitude) * cos(distance)) +
(cos(sourceLatitude) * sin(distance) * cos(bearing)));
double longitude = sourceLongitude + atan2((sin(bearing) * sin(distance) * cos(sourceLatitude)),
cos(distance) - (sin(sourceLatitude) * sin(latitude)));
CLLocationCoordinate2D result;
result.latitude = fmin(85.0, fmax(-85.0, (latitude / multiplier)));
result.longitude = longitude / multiplier;
return result;
}
CLLocationCoordinate2D randomWorldCoordinate() {
static const struct {
CLLocationCoordinate2D coordinate;
CLLocationDistance radius;
} landmasses[] = {
// Rough land masses
{{ 38.328531, 94.778736 }, 4100000 }, // Asia
{{ 1.477244, 18.138111 }, 4100000 }, // Africa
{{ 52.310059, 22.295425 }, 2000000 }, // Europe
{{ 42.344216, -96.532700 }, 3000000 }, // N America
{{ -11.537273, -57.035181 }, 2220000 }, // S America
{{ -20.997030, 134.660541 }, 2220000 }, // Australia
// A few cities
{{ 51.504787, -0.106977 }, 33000 }, // London
{{ 37.740186, -122.437086 }, 8500 }, // SF
{{ 52.509978, 13.406510 }, 12000 }, // Berlin
{{ 12.966246, 77.586505 }, 19000 } // Bengaluru
};
NSInteger index = arc4random_uniform(sizeof(landmasses)/sizeof(landmasses[0]));
CLLocationCoordinate2D coordinate = landmasses[index].coordinate;
CLLocationDistance radius = landmasses[index].radius;
// Now create a world coord
CLLocationDegrees heading = (CLLocationDegrees)arc4random_uniform(360);
CLLocationDistance distance = (CLLocationDistance)arc4random_uniform(radius);
CLLocationCoordinate2D newLocation = coordinateCentered(coordinate, heading, distance);
return newLocation;
}
@interface MBXDroppedPinAnnotation : MGLPointAnnotation
@end
@implementation MBXDroppedPinAnnotation
@end
@interface MBXCustomCalloutAnnotation : MGLPointAnnotation
@property (nonatomic, assign) BOOL anchoredToAnnotation;
@property (nonatomic, assign) BOOL dismissesAutomatically;
@end
@implementation MBXCustomCalloutAnnotation
@end
@interface MBXSpriteBackedAnnotation : MGLPointAnnotation
@end
@implementation MBXSpriteBackedAnnotation
@end
@interface MBXViewController () <UITableViewDelegate,
UITableViewDataSource,
MGLMapViewDelegate,
MGLComputedShapeSourceDataSource>
@property (nonatomic) IBOutlet MGLMapView *mapView;
@property (nonatomic) MBXState *currentState;
@property (weak, nonatomic) IBOutlet UIButton *hudLabel;
@property (weak, nonatomic) IBOutlet MBXFrameTimeGraphView *frameTimeGraphView;
@property (nonatomic) NSInteger styleIndex;
@property (nonatomic) BOOL customUserLocationAnnnotationEnabled;
@property (nonatomic, getter=isLocalizingLabels) BOOL localizingLabels;
@property (nonatomic) BOOL reuseQueueStatsEnabled;
@property (nonatomic) BOOL frameTimeGraphEnabled;
@property (nonatomic) BOOL shouldLimitCameraChanges;
@property (nonatomic) BOOL randomWalk;
@property (nonatomic) BOOL zoomLevelOrnamentEnabled;
@property (nonatomic) BOOL debugLoggingEnabled;
@property (nonatomic) NSMutableArray<UIWindow *> *helperWindows;
@property (nonatomic) NSMutableArray<UIView *> *contentInsetsOverlays;
@end
@interface MGLMapView (MBXViewController)
@property (nonatomic) NSDictionary *annotationViewReuseQueueByIdentifier;
@end
@implementation MBXViewController
{
BOOL _isTouringWorld;
BOOL _contentInsetsEnabled;
UIEdgeInsets _originalContentInsets;
}
#pragma mark - Setup & Teardown
- (void)viewDidLoad
{
[super viewDidLoad];
// Keep track of current map state and debug preferences,
// saving and restoring when the application's state changes.
self.currentState = [MBXStateManager sharedManager].currentState;
if (!self.currentState) {
// Create a new state with the below default values
self.currentState = [[MBXState alloc] init];
self.mapView.showsUserHeadingIndicator = YES;
self.mapView.showsScale = YES;
self.zoomLevelOrnamentEnabled = NO;
self.frameTimeGraphEnabled = NO;
self.debugLoggingEnabled = YES;
} else {
// Revert to the previously saved state
[self restoreMapState:nil];
}
[self updateHUD];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCurrentMapState:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restoreMapState:) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCurrentMapState:) name:UIApplicationWillTerminateNotification object:nil];
if ([MGLAccountManager accessToken].length)
{
self.styleIndex = -1;
[self cycleStyles:self];
}
self.mapView.experimental_enableFrameRateMeasurement = YES;
self.hudLabel.titleLabel.font = [UIFont monospacedDigitSystemFontOfSize:10 weight:UIFontWeightRegular];
// Add fall-through single tap gesture recognizer. This will be called when
// the map view's tap recognizers fail.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
for (UIGestureRecognizer *gesture in self.mapView.gestureRecognizers) {
if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
[singleTap requireGestureRecognizerToFail:gesture];
}
}
[self.mapView addGestureRecognizer:singleTap];
// Display a secondary map on any connected external display.
// https://developer.apple.com/documentation/uikit/windows_and_screens/displaying_content_on_a_connected_screen?language=objc
self.helperWindows = [NSMutableArray array];
[[NSNotificationCenter defaultCenter] addObserverForName:UIScreenDidConnectNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
UIScreen *helperScreen = note.object;
UIWindow *helperWindow = [[UIWindow alloc] initWithFrame:helperScreen.bounds];
helperWindow.screen = helperScreen;
UIViewController *helperViewController = [[UIViewController alloc] init];
MGLMapView *helperMapView = [[MGLMapView alloc] initWithFrame:helperWindow.bounds styleURL:MGLStyle.satelliteStreetsStyleURL];
helperMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
helperMapView.camera = self.mapView.camera;
helperMapView.compassView.hidden = YES;
helperViewController.view = helperMapView;
helperWindow.rootViewController = helperViewController;
helperWindow.hidden = NO;
[self.helperWindows addObject:helperWindow];
}];
[[NSNotificationCenter defaultCenter] addObserverForName:UIScreenDidDisconnectNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
UIScreen *helperScreen = note.object;
for (UIWindow *window in self.helperWindows) {
if (window.screen == helperScreen) {
[self.helperWindows removeObject:window];
}
}
}];
}
- (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];
}
#pragma mark - Debugging Interface
- (IBAction)showSettings:(__unused id)sender
{
self.randomWalk = NO;
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 @[
@"Debug Tools",
@"Annotations",
@"Runtime Styling",
@"Miscellaneous"
];
}
- (NSArray <NSString *> *)settingsTitlesForSection:(NSInteger)section
{
NSMutableArray *settingsTitles = [NSMutableArray array];
MGLMapDebugMaskOptions debugMask = self.mapView.debugMask;
switch (section)
{
case MBXSettingsDebugTools:
[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")],
[NSString stringWithFormat:@"%@ zoom level ornament", (self.zoomLevelOrnamentEnabled ? @"Hide" :@"Show")],
[NSString stringWithFormat:@"%@ frame time graph", (self.frameTimeGraphEnabled ? @"Hide" :@"Show")],
[NSString stringWithFormat:@"%@ reuse queue stats", (self.reuseQueueStatsEnabled ? @"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",
@"Animate an Annotation View",
@"Add Test Shapes",
@"Add Point With Custom Callout",
@"Query Annotations",
[NSString stringWithFormat:@"%@ Custom User Dot", (_customUserLocationAnnnotationEnabled ? @"Disable" : @"Enable")],
@"Remove Annotations",
@"Select an offscreen point annotation",
@"Center selected annotation",
@"Add visible area polyline"
]];
break;
case MBXSettingsRuntimeStyling:
[settingsTitles addObjectsFromArray:@[
@"Add Building Extrusions",
@"Style Water With Function",
@"Style Roads With Function",
@"Add Raster & Apply Function",
@"Add Shapes & 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",
@"Query and Style Features",
@"Style Feature",
@"Style Dynamic Point Collection",
@"Update Shape Source: Data",
@"Update Shape Source: URL",
@"Update Shape Source: Features",
@"Style Vector Tile Source",
@"Style Raster Tile Source",
@"Style Image Source",
@"Add Route Line",
@"Add Lime Green Triangle Layer",
@"Dynamically Style Polygon",
@"Add Custom Lat/Lon Grid",
]];
break;
case MBXSettingsMiscellaneous:
[settingsTitles addObjectsFromArray:@[
@"Start World Tour",
@"Random Tour",
@"Embedded Map View",
[NSString stringWithFormat:@"%@ Second Map", ([self.view viewWithTag:2] == nil ? @"Show" : @"Hide")],
[NSString stringWithFormat:@"Show Labels in %@", (_localizingLabels ? @"Default Language" : [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[self bestLanguageForUser]])],
@"Show Snapshots",
@"Missing Icon",
[NSString stringWithFormat:@"%@ Camera Changes", (_shouldLimitCameraChanges ? @"Unlimit" : @"Limit")],
[NSString stringWithFormat:@"Turn %@ Content Insets", (_contentInsetsEnabled ? @"Off" : @"On")],
@"View Route Simulation",
@"Ornaments Placement",
]];
if (self.currentState.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 MBXSettingsDebugTools:
switch (indexPath.row)
{
case MBXSettingsDebugToolsResetPosition:
[self.mapView resetPosition];
break;
case MBXSettingsDebugToolsTileBoundaries:
self.currentState.debugMask ^= MGLMapDebugTileBoundariesMask;
break;
case MBXSettingsDebugToolsTileInfo:
self.currentState.debugMask ^= MGLMapDebugTileInfoMask;
break;
case MBXSettingsDebugToolsTimestamps:
self.currentState.debugMask ^= MGLMapDebugTimestampsMask;
break;
case MBXSettingsDebugToolsCollisionBoxes:
self.currentState.debugMask ^= MGLMapDebugCollisionBoxesMask;
break;
case MBXSettingsDebugToolsOverdrawVisualization:
self.currentState.debugMask ^= MGLMapDebugOverdrawVisualizationMask;
break;
case MBXSettingsDebugToolsShowZoomLevel:
{
self.zoomLevelOrnamentEnabled = !self.zoomLevelOrnamentEnabled;
self.currentState.showsZoomLevelOrnament = self.zoomLevelOrnamentEnabled;
self.hudLabel.hidden = !self.zoomLevelOrnamentEnabled;
self.reuseQueueStatsEnabled = NO;
[self updateHUD];
break;
}
case MBXSettingsDebugToolsShowFrameTimeGraph:
{
self.frameTimeGraphEnabled = !self.frameTimeGraphEnabled;
self.currentState.showsTimeFrameGraph = !self.currentState.showsTimeFrameGraph;
self.frameTimeGraphView.hidden = !self.frameTimeGraphEnabled;
[self updateHUD];
break;
}
case MBXSettingsDebugToolsShowReuseQueueStats:
{
self.reuseQueueStatsEnabled = !self.currentState.reuseQueueStatsEnabled;
self.hudLabel.hidden = !self.currentState.reuseQueueStatsEnabled;
self.zoomLevelOrnamentEnabled = NO;
[self updateHUD];
break;
}
default:
NSAssert(NO, @"All debug tools setting rows should be implemented");
break;
}
self.mapView.debugMask = self.currentState.debugMask;
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 MBXSettingsAnnotationAnimation:
[self animateAnnotationView];
break;
case MBXSettingsAnnotationsTestShapes:
[self addTestShapes];
break;
case MBXSettingsAnnotationsCustomCallout:
[self addAnnotationWithCustomCallout];
break;
case MBXSettingsAnnotationsQueryAnnotations:
[self testQueryPointAnnotations];
break;
case MBXSettingsAnnotationsCustomUserDot:
[self toggleCustomUserDot];
break;
case MBXSettingsAnnotationsRemoveAnnotations:
[self.mapView removeAnnotations:self.mapView.annotations];
break;
case MBXSettingsAnnotationSelectRandomOffscreenPointAnnotation:
[self selectAnOffscreenPointAnnotation];
break;
case MBXSettingsAnnotationCenterSelectedAnnotation:
[self centerSelectedAnnotation];
break;
case MBXSettingsAnnotationAddVisibleAreaPolyline:
[self addVisibleAreaPolyline];
break;
default:
NSAssert(NO, @"All annotations setting rows should be implemented");
break;
}
break;
case MBXSettingsRuntimeStyling:
switch (indexPath.row)
{
case MBXSettingsRuntimeStylingBuildingExtrusions:
[self styleBuildingExtrusions];
break;
case MBXSettingsRuntimeStylingWater:
[self styleWaterLayer];
break;
case MBXSettingsRuntimeStylingRoads:
[self styleRoadLayer];
break;
case MBXSettingsRuntimeStylingRaster:
[self styleRasterLayer];
break;
case MBXSettingsRuntimeStylingShape:
[self styleShapeSource];
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 MBXSettingsRuntimeStylingUpdateShapeSourceURL:
[self updateShapeSourceURL];
break;
case MBXSettingsRuntimeStylingUpdateShapeSourceData:
[self updateShapeSourceData];
break;
case MBXSettingsRuntimeStylingUpdateShapeSourceFeatures:
[self updateShapeSourceFeatures];
break;
case MBXSettingsRuntimeStylingVectorTileSource:
[self styleVectorTileSource];
break;
case MBXSettingsRuntimeStylingRasterTileSource:
[self styleRasterTileSource];
break;
case MBXSettingsRuntimeStylingImageSource:
[self styleImageSource];
break;
case MBXSettingsRuntimeStylingRouteLine:
[self styleRouteLine];
break;
case MBXSettingsRuntimeStylingAddLimeGreenTriangleLayer:
[self styleAddLimeGreenTriangleLayer];
break;
case MBXSettingsRuntimeStylingDDSPolygon:
[self stylePolygonWithDDS];
break;
case MBXSettingsRuntimeStylingCustomLatLonGrid:
[self addLatLonGrid];
break;
default:
NSAssert(NO, @"All runtime styling setting rows should be implemented");
break;
}
break;
case MBXSettingsMiscellaneous:
switch (indexPath.row)
{
case MBXSettingsMiscellaneousLocalizeLabels:
[self toggleStyleLabelsLanguage];
break;
case MBXSettingsMiscellaneousWorldTour:
[self startWorldTour];
break;
case MBXSettingsMiscellaneousRandomTour:
[self randomWorldTour];
break;
case MBXSettingsMiscellaneousPrintLogFile:
[self printTelemetryLogFile];
break;
case MBXSettingsMiscellaneousDeleteLogFile:
[self deleteTelemetryLogFile];
break;
case MBXSettingsMiscellaneousScrollView:
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MBXEmbeddedMapViewController *embeddedMapViewController = (MBXEmbeddedMapViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MBXEmbeddedMapViewController"];
[self.navigationController pushViewController:embeddedMapViewController animated:YES];
break;
}
case MBXSettingsMiscellaneousToggleTwoMaps:
[self toggleSecondMapView];
break;
case MBXSettingsMiscellaneousShowSnapshots:
{
[self performSegueWithIdentifier:@"ShowSnapshots" sender:nil];
break;
}
case MBXSettingsMiscellaneousMissingIcon:
{
[self loadMissingIcon];
break;
}
case MBXSettingsMiscellaneousShowCustomLocationManager:
{
[self performSegueWithIdentifier:@"ShowCustomLocationManger" sender:nil];
break;
}
case MBXSettingsMiscellaneousShouldLimitCameraChanges:
{
self.shouldLimitCameraChanges = !self.shouldLimitCameraChanges;
if (self.shouldLimitCameraChanges) {
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(39.748947, -104.995882) zoomLevel:10 direction:0 animated:NO];
}
break;
}
case MBXSettingsMiscellaneousSetContentInsets:
{
if (!_contentInsetsEnabled) {
_originalContentInsets = [self.mapView contentInset];
}
_contentInsetsEnabled = !_contentInsetsEnabled;
self.automaticallyAdjustsScrollViewInsets = !_contentInsetsEnabled;
UIEdgeInsets contentInsets = self.mapView.bounds.size.width > self.mapView.bounds.size.height
? UIEdgeInsetsMake(_originalContentInsets.top, 0.5 * self.mapView.bounds.size.width, _originalContentInsets.bottom, 0.0)
: UIEdgeInsetsMake(0.25 * self.mapView.bounds.size.height, 0.0, _originalContentInsets.bottom, 0.25 * self.mapView.bounds.size.width);
if (_contentInsetsEnabled) {
if (!self.contentInsetsOverlays)
self.contentInsetsOverlays = [NSMutableArray array];
if (![self.contentInsetsOverlays count]) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.mapView.bounds.size.width, contentInsets.top)];
view.backgroundColor = [UIColor colorWithRed:0.0 green:0.3 blue:0.3 alpha:0.5];
[self.contentInsetsOverlays addObject:view];
[self.view addSubview:view];
view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, contentInsets.left, self.mapView.bounds.size.height)];
view.backgroundColor = [UIColor colorWithRed:0.0 green:0.3 blue:0.3 alpha:0.5];
[self.contentInsetsOverlays addObject:view];
[self.view addSubview:view];
view = [[UIView alloc]initWithFrame:CGRectMake(self.mapView.bounds.size.width - contentInsets.right, 0, contentInsets.right, self.mapView.bounds.size.height)];
view.backgroundColor = [UIColor colorWithRed:0.0 green:0.3 blue:0.3 alpha:0.5];
[self.contentInsetsOverlays addObject:view];
[self.view addSubview:view];
view = [[UIView alloc]initWithFrame:CGRectMake(0, self.mapView.bounds.size.height - contentInsets.bottom, self.mapView.bounds.size.width, self.mapView.bounds.size.height)];
view.backgroundColor = [UIColor colorWithRed:0.0 green:0.3 blue:0.3 alpha:0.5];
[self.contentInsetsOverlays addObject:view];
[self.view addSubview:view];
}
[self.view bringSubviewToFront:self.contentInsetsOverlays[0]];
[self.view bringSubviewToFront:self.contentInsetsOverlays[1]];
[self.view bringSubviewToFront:self.contentInsetsOverlays[2]];
[self.view bringSubviewToFront:self.contentInsetsOverlays[3]];
// Denver streets parallel to cardinal directions help illustrate
// viewport center offset when edge insets are set.
MGLMapCamera *camera = [MGLMapCamera cameraLookingAtCenterCoordinate:CLLocationCoordinate2DMake(39.72707, -104.9986)
acrossDistance:100
pitch:60
heading:0];
__weak typeof(self) weakSelf = self;
[self.mapView setCamera:camera withDuration:0.3 animationTimingFunction:nil completionHandler:^{
[weakSelf.mapView setContentInset:contentInsets animated:YES completionHandler:nil];
}];
} else {
[self.view sendSubviewToBack:self.contentInsetsOverlays[0]];
[self.view sendSubviewToBack:self.contentInsetsOverlays[1]];
[self.view sendSubviewToBack:self.contentInsetsOverlays[2]];
[self.view sendSubviewToBack:self.contentInsetsOverlays[3]];
[self.mapView setContentInset:_originalContentInsets animated:YES completionHandler:nil];
}
break;
}
case MBXSettingsMiscellaneousOrnamentsPlacement:
{
MBXOrnamentsViewController *ornamentsViewController = [[MBXOrnamentsViewController alloc] init];
[self.navigationController pushViewController:ornamentsViewController animated:YES];
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)animateAnnotationView
{
MGLPointAnnotation *annot = [[MGLPointAnnotation alloc] init];
annot.coordinate = self.mapView.centerCoordinate;
[self.mapView addAnnotation:annot];
// Move the annotation to a point that is offscreen.
CGPoint point = CGPointMake(self.view.frame.origin.x - 200, CGRectGetMidY(self.view.frame));
CLLocationCoordinate2D coord = [self.mapView convertPoint:point toCoordinateFromView:self.view];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:10 animations:^{
annot.coordinate = coord;
}];
});
};
- (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];
// West coast polyline
//
CLLocationCoordinate2D lineCoordinates[4] = {
CLLocationCoordinate2DMake(47.6025, -122.3327),
CLLocationCoordinate2DMake(45.5189, -122.6726),
CLLocationCoordinate2DMake(37.7790, -122.4177),
CLLocationCoordinate2DMake(34.0532, -118.2349)
};
MGLPolyline *line = [MGLPolyline polylineWithCoordinates:lineCoordinates count:4];
[self.mapView addAnnotation:line];
// 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]];
polygon.title = feature[@"properties"][@"NAME"];
[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 *firstAnnotation = [[MBXCustomCalloutAnnotation alloc] init];
firstAnnotation.coordinate = CLLocationCoordinate2DMake(48.8533940, 2.3775439);
firstAnnotation.title = @"Open anchored to annotation";
firstAnnotation.anchoredToAnnotation = YES;
firstAnnotation.dismissesAutomatically = NO;
MBXCustomCalloutAnnotation *secondAnnotation = [[MBXCustomCalloutAnnotation alloc] init];
secondAnnotation.coordinate = CLLocationCoordinate2DMake(48.8543940, 2.3775439);
secondAnnotation.title = @"Open not anchored to annotation";
secondAnnotation.anchoredToAnnotation = NO;
secondAnnotation.dismissesAutomatically = NO;
MBXCustomCalloutAnnotation *thirdAnnotation = [[MBXCustomCalloutAnnotation alloc] init];
thirdAnnotation.coordinate = CLLocationCoordinate2DMake(48.8553940, 2.3775439);
thirdAnnotation.title = @"Dismisses automatically";
thirdAnnotation.anchoredToAnnotation = YES;
thirdAnnotation.dismissesAutomatically = YES;
NSArray *annotations = @[firstAnnotation, secondAnnotation, thirdAnnotation];
[self.mapView addAnnotations:annotations];
[self.mapView showAnnotations:annotations animated:YES];
}
- (void)styleBuildingExtrusions
{
MGLSource* source = [self.mapView.style sourceWithIdentifier:@"composite"];
if (source) {
MGLFillExtrusionStyleLayer* layer = [[MGLFillExtrusionStyleLayer alloc] initWithIdentifier:@"extrudedBuildings" source:source];
layer.sourceLayerIdentifier = @"building";
layer.predicate = [NSPredicate predicateWithFormat:@"extrude == 'true' AND CAST(height, 'NSNumber') > 0"];
layer.fillExtrusionBase = [NSExpression expressionForKeyPath:@"min_height"];
layer.fillExtrusionHeight = [NSExpression expressionForKeyPath:@"height"];
// Set the fill color to that of the existing building footprint layer, if it exists.
MGLFillStyleLayer* buildingLayer = (MGLFillStyleLayer*)[self.mapView.style layerWithIdentifier:@"building"];
if (buildingLayer) {
if (buildingLayer.fillColor) {
layer.fillExtrusionColor = buildingLayer.fillColor;
} else {
layer.fillExtrusionColor = [NSExpression expressionForConstantValue:[UIColor whiteColor]];
}
layer.fillExtrusionOpacity = [NSExpression expressionForConstantValue:@0.75];
}
MGLStyleLayer* labelLayer = [self.mapView.style layerWithIdentifier:@"waterway-label"];
if (labelLayer) {
[self.mapView.style insertLayer:layer belowLayer:labelLayer];
} else {
[self.mapView.style addLayer:layer];
}
}
}
- (void)styleWaterLayer
{
MGLFillStyleLayer *waterLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"water"];
NSDictionary *waterColorStops = @{@6.0f: [UIColor yellowColor],
@8.0f: [UIColor blueColor],
@10.0f: [UIColor redColor],
@12.0f: [UIColor greenColor],
@14.0f: [UIColor blueColor]};
NSExpression *fillColorExpression = [NSExpression mgl_expressionForInterpolatingExpression:NSExpression.zoomLevelVariableExpression
withCurveType:MGLExpressionInterpolationModeLinear
parameters:nil
stops:[NSExpression expressionForConstantValue:waterColorStops]];
waterLayer.fillColor = fillColorExpression;
NSDictionary *fillAntialiasedStops = @{@11: @YES,
@12: @NO,
@13: @YES,
@14: @NO,
@15: @YES};
waterLayer.fillAntialiased = [NSExpression mgl_expressionForSteppingExpression:NSExpression.zoomLevelVariableExpression
fromExpression:[NSExpression expressionForConstantValue:@NO]
stops:[NSExpression expressionForConstantValue:fillAntialiasedStops]];
}
- (void)styleRoadLayer
{
MGLLineStyleLayer *roadLayer = (MGLLineStyleLayer *)[self.mapView.style layerWithIdentifier:@"road-primary"];
roadLayer.lineColor = [NSExpression expressionForConstantValue:[UIColor blackColor]];
NSDictionary *lineWidthStops = @{@5: @5,
@10: @15,
@15: @30};
NSExpression *lineWidthExpression = [NSExpression expressionWithFormat:
@"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)",
lineWidthStops];
roadLayer.lineWidth = lineWidthExpression;
roadLayer.lineGapWidth = lineWidthExpression;
NSDictionary *roadLineColorStops = @{@10: [UIColor purpleColor],
@13: [UIColor yellowColor],
@16: [UIColor cyanColor]};
roadLayer.lineColor = [NSExpression expressionWithFormat:
@"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)",
roadLineColorStops];
roadLayer.visible = YES;
roadLayer.maximumZoomLevel = 15;
roadLayer.minimumZoomLevel = 13;
}
- (void)styleRasterLayer
{
NSURL *rasterURL = [NSURL URLWithString:@"mapbox://mapbox.satellite"];
MGLRasterTileSource *rasterTileSource = [[MGLRasterTileSource alloc] initWithIdentifier:@"my-raster-tile-source" configurationURL:rasterURL tileSize:512];
[self.mapView.style addSource:rasterTileSource];
MGLRasterStyleLayer *rasterLayer = [[MGLRasterStyleLayer alloc] initWithIdentifier:@"my-raster-layer" source:rasterTileSource];
NSDictionary *opacityStops = @{@20.0f: @1.0f,
@5.0f: @0.0f};
rasterLayer.rasterOpacity = [NSExpression expressionWithFormat:
@"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)",
opacityStops];
[self.mapView.style addLayer:rasterLayer];
}
- (void)styleShapeSource
{
NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"];
NSURL *geoJSONURL = [NSURL fileURLWithPath:filePath];
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"ams" URL:geoJSONURL options:nil];
[self.mapView.style addSource:source];
MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"test" source:source];
fillLayer.fillColor = [NSExpression expressionForConstantValue:[UIColor purpleColor]];
[self.mapView.style addLayer:fillLayer];
}
- (void)styleSymbolLayer
{
MGLSymbolStyleLayer *stateLayer = (MGLSymbolStyleLayer *)[self.mapView.style layerWithIdentifier:@"state-label-lg"];
stateLayer.textColor = [NSExpression expressionForConstantValue:[UIColor redColor]];
}
- (void)styleBuildingLayer
{
MGLTransition transition = { 5, 1 };
self.mapView.style.transition = transition;
MGLFillStyleLayer *buildingLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"building"];
buildingLayer.fillColor = [NSExpression expressionForConstantValue:[UIColor purpleColor]];
}
- (void)styleFerryLayer
{
MGLLineStyleLayer *ferryLineLayer = (MGLLineStyleLayer *)[self.mapView.style layerWithIdentifier:@"ferry"];
ferryLineLayer.lineColor = [NSExpression expressionForConstantValue:[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 = [NSExpression expressionForConstantValue:[UIColor redColor]];
statesLayer.fillOpacity = [NSExpression expressionForConstantValue:@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 = [NSExpression expressionForConstantValue:[UIColor redColor]];
countiesLayer.lineOpacity = [NSExpression expressionForConstantValue:@0.75];
countiesLayer.lineWidth = [NSExpression expressionForConstantValue:@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:@"CAST(HRRNUM, 'NSNumber') >= %@ AND CAST(HRRNUM, 'NSNumber') < 300", @(200)];
// paint properties
regionsLayer.fillColor = [NSExpression expressionForConstantValue:[UIColor blueColor]];
regionsLayer.fillOpacity = [NSExpression expressionForConstantValue:@0.5];
});
}
- (void)styleQuery
{
CGRect queryRect = CGRectInset(self.mapView.bounds, 100, 200);
NSArray *visibleFeatures = [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(), ^{
MGLShapeSource *shapeSource = [[MGLShapeSource alloc] initWithIdentifier:querySourceID features:visibleFeatures options:nil];
[self.mapView.style addSource:shapeSource];
MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:queryLayerID source:shapeSource];
fillLayer.fillColor = [NSExpression expressionForConstantValue:[UIColor blueColor]];
fillLayer.fillOpacity = [NSExpression expressionForConstantValue:@0.5];
[self.mapView.style addLayer:fillLayer];
});
}
- (void)styleFeature
{
self.mapView.zoomLevel = 10;
self.mapView.centerCoordinate = CLLocationCoordinate2DMake(51.068585180672635, -114.06074523925781);
CLLocationCoordinate2D leafCoords[] = {
{50.9683733218221,-114.07035827636719},
{51.02325750523972,-114.06967163085938},
{51.009434536947786,-114.14245605468749},
{51.030599281184124,-114.12597656249999},
{51.060386316691016,-114.21043395996094},
{51.063838646941576,-114.17816162109375},
{51.08152779888779,-114.19876098632812},
{51.08066507029602,-114.16854858398438},
{51.09662294502995,-114.17472839355469},
{51.07764539352731,-114.114990234375},
{51.13670896949613,-114.12391662597656},
{51.13369295212583,-114.09576416015624},
{51.17546878815025,-114.07585144042969},
{51.140155605265896,-114.04632568359375},
{51.15049396880196,-114.01542663574219},
{51.088860342359965,-114.00924682617186},
{51.12205789681453,-113.94813537597656},
{51.106539930027225,-113.94882202148438},
{51.117747873223344,-113.92616271972656},
{51.10093493903458,-113.92616271972656},
{51.10697105503078,-113.90625},
{51.09144802136697,-113.9117431640625},
{51.04916446529361,-113.97010803222655},
{51.045279344649146,-113.9398956298828},
{51.022825599852496,-114.06211853027344},
{51.045279344649146,-113.9398956298828},
{51.022825599852496,-114.06211853027344},
{51.022825599852496,-114.06280517578125},
{50.968805734317804,-114.06280517578125},
{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"};
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"leaf-source" shape:feature options:nil];
[self.mapView.style addSource:source];
MGLFillStyleLayer *layer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"leaf-fill-layer" source:source];
layer.predicate = [NSPredicate predicateWithFormat:@"color = 'red'"];
layer.fillColor = [NSExpression expressionForConstantValue:[UIColor redColor]];
[self.mapView.style addLayer:layer];
NSString *geoJSON = @"{\"type\": \"Feature\", \"properties\": {\"color\": \"green\"}, \"geometry\": { \"type\": \"Point\", \"coordinates\": [ -114.06847000122069, 51.050459433092655 ] }}";
NSData *data = [geoJSON dataUsingEncoding:NSUTF8StringEncoding];
MGLShape *shape = [MGLShape shapeWithData:data encoding:NSUTF8StringEncoding error:NULL];
MGLShapeSource *pointSource = [[MGLShapeSource alloc] initWithIdentifier:@"leaf-point-source" shape:shape options:nil];
[self.mapView.style addSource:pointSource];
MGLCircleStyleLayer *circleLayer = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"leaf-circle-layer" source:pointSource];
circleLayer.circleColor = [NSExpression expressionForConstantValue:[UIColor greenColor]];
circleLayer.predicate = [NSPredicate predicateWithFormat:@"color = 'green'"];
[self.mapView.style addLayer:circleLayer];
CLLocationCoordinate2D squareCoords[] = {
{51.056070541830934, -114.0274429321289},
{51.07937094724242, -114.0274429321289},
{51.07937094724242, -113.98761749267578},
{51.05607054183093, -113.98761749267578},
{51.056070541830934, -114.0274429321289},
};
MGLPolygon *polygon = [MGLPolygon polygonWithCoordinates:squareCoords count:sizeof(squareCoords)/sizeof(squareCoords[0])];
MGLShapeSource *plainShapeSource = [[MGLShapeSource alloc] initWithIdentifier:@"leaf-plain-shape-source" shape:polygon options:nil];
[self.mapView.style addSource:plainShapeSource];
MGLFillStyleLayer *plainFillLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"leaf-plain-fill-layer" source:plainShapeSource];
plainFillLayer.fillColor = [NSExpression expressionForConstantValue:[UIColor yellowColor]];
[self.mapView.style addLayer:plainFillLayer];
}
- (void)updateShapeSourceData
{
[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];
MGLShape *shape = [MGLShape shapeWithData:data encoding:NSUTF8StringEncoding error:NULL];
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"mutable-data-source-id" shape:shape 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 *updatedGeoJSON = @"{\"type\": \"FeatureCollection\",\"features\": [{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"LineString\",\"coordinates\": [[-107.75390625,40.329795743702064],[-109.34814453125,37.64903402157866]]}}]}";
NSData *updatedData = [updatedGeoJSON dataUsingEncoding:NSUTF8StringEncoding];
MGLShape *updatedShape = [MGLShape shapeWithData:updatedData encoding:NSUTF8StringEncoding error:NULL];
source.shape = updatedShape;
});
}
- (void)updateShapeSourceURL
{
[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];
MGLShapeSource *source = [[MGLShapeSource 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 *threeStatesFilePath = [[NSBundle bundleForClass:self.class] pathForResource:@"threestates" ofType:@"geojson"];
NSURL *updatedGeoJSONURL = [NSURL fileURLWithPath:threeStatesFilePath];
source.URL = updatedGeoJSONURL;
});
}
- (void)updateShapeSourceFeatures
{
[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[] = {
{-41.17710352162799, 288.67298126220703},
{-41.13962313627545, 288.67298126220703},
{-41.13962313627545, 288.7261962890625},
{-41.17710352162799, 288.7261962890625},
{-41.17710352162799, 288.67298126220703}
};
MGLPolygonFeature *smallBoxFeature = [MGLPolygonFeature polygonWithCoordinates:smallBox count:sizeof(smallBox)/sizeof(smallBox[0])];
MGLPolygonFeature *largeBoxFeature = [MGLPolygonFeature polygonWithCoordinates:largeBox count:sizeof(largeBox)/sizeof(largeBox[0])];
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"mutable-data-source-features-id"
shape:smallBoxFeature
options:nil];
[self.mapView.style addSource:source];
MGLFillStyleLayer *layer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"mutable-data-layer-features-id" source:source];
layer.fillColor = [NSExpression expressionForConstantValue:[UIColor redColor]];
[self.mapView.style addLayer:layer];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
source.shape = largeBoxFeature;
});
}
- (void)styleDynamicPointCollection
{
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(36.9979, -109.0441) zoomLevel:14 animated:NO];
CLLocationCoordinate2D coordinates[] = {
{37.00145594210082, -109.04960632324219},
{37.00173012609867, -109.0404224395752},
{36.99453246847359, -109.04960632324219},
{36.99508088541243, -109.04007911682129},
};
MGLPointCollectionFeature *feature = [MGLPointCollectionFeature pointCollectionWithCoordinates:coordinates count:4];
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"wiggle-source" shape:feature options:nil];
[self.mapView.style addSource:source];
MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"wiggle-layer" source:source];
[self.mapView.style addLayer:layer];
}
- (void)styleVectorTileSource
{
NSURL *url = [[NSURL alloc] initWithString:@"mapbox://mapbox.mapbox-terrain-v2"];
MGLVectorTileSource *vectorTileSource = [[MGLVectorTileSource alloc] initWithIdentifier:@"style-vector-tile-source-id" configurationURL:url];
[self.mapView.style addSource:vectorTileSource];
MGLBackgroundStyleLayer *backgroundLayer = [[MGLBackgroundStyleLayer alloc] initWithIdentifier:@"style-vector-background-layer-id"];
backgroundLayer.backgroundColor = [NSExpression expressionForConstantValue:[UIColor blackColor]];
[self.mapView.style addLayer:backgroundLayer];
MGLLineStyleLayer *lineLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"style-vector-line-layer-id" source:vectorTileSource];
lineLayer.sourceLayerIdentifier = @"contour";
lineLayer.lineJoin = [NSExpression expressionForConstantValue:@"round"];
lineLayer.lineCap = [NSExpression expressionForConstantValue:@"round"];
lineLayer.lineColor = [NSExpression expressionForConstantValue:[UIColor greenColor]];
[self.mapView.style addLayer:lineLayer];
}
- (void)styleRasterTileSource
{
NSString *tileURL = [NSString stringWithFormat:@"https://stamen-tiles.a.ssl.fastly.net/terrain-background/{z}/{x}/{y}%@.jpg", UIScreen.mainScreen.nativeScale > 1 ? @"@2x" : @""];
MGLRasterTileSource *rasterTileSource = [[MGLRasterTileSource alloc] initWithIdentifier:@"style-raster-tile-source-id" tileURLTemplates:@[tileURL] options:@{
MGLTileSourceOptionTileSize: @256,
}];
[self.mapView.style addSource:rasterTileSource];
MGLRasterStyleLayer *rasterLayer = [[MGLRasterStyleLayer alloc] initWithIdentifier:@"style-raster-layer-id" source:rasterTileSource];
[self.mapView.style addLayer:rasterLayer];
}
- (void)styleImageSource
{
MGLCoordinateQuad coordinateQuad = {
{ 46.437, -80.425 },
{ 37.936, -80.425 },
{ 37.936, -71.516 },
{ 46.437, -71.516 } };
MGLImageSource *imageSource = [[MGLImageSource alloc] initWithIdentifier:@"style-image-source-id" coordinateQuad:coordinateQuad URL:[NSURL URLWithString:@"https://www.mapbox.com/mapbox-gl-js/assets/radar0.gif"]];
[self.mapView.style addSource:imageSource];
MGLRasterStyleLayer *rasterLayer = [[MGLRasterStyleLayer alloc] initWithIdentifier:@"style-raster-image-layer-id" source:imageSource];
[self.mapView.style addLayer:rasterLayer];
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateAnimatedImageSource:)
userInfo:imageSource
repeats:YES];
}
- (void)updateAnimatedImageSource:(NSTimer *)timer {
static int radarSuffix = 0;
MGLImageSource *imageSource = (MGLImageSource *)timer.userInfo;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.mapbox.com/mapbox-gl-js/assets/radar%d.gif", radarSuffix++]];
[imageSource setValue:url forKey:@"URL"];
if (radarSuffix > 3) {
radarSuffix = 0;
}
}
-(void)toggleStyleLabelsLanguage
{
_localizingLabels = !_localizingLabels;
[self.mapView.style localizeLabelsIntoLocale:_localizingLabels ? [NSLocale localeWithLocaleIdentifier:@"mul"] : nil];
}
- (void)styleRouteLine
{
CLLocationCoordinate2D coords[] = {
{ 43.84455590478528, 10.504238605499268 },
{ 43.84385562343126, 10.504125952720642 },
{ 43.84388657526694, 10.503299832344055 },
{ 43.84332557075269, 10.503235459327698 },
{ 43.843441641085036, 10.502264499664307 },
{ 43.84396395478592, 10.50242006778717 },
{ 43.84406067904351, 10.501744151115416 },
{ 43.84422317544319, 10.501792430877686 }
};
NSInteger count = sizeof(coords) / sizeof(coords[0]);
[self.mapView setCenterCoordinate:coords[0] zoomLevel:16 animated:YES];
MGLPolylineFeature *routeLine = [MGLPolylineFeature polylineWithCoordinates:coords count:count];
MGLShapeSource *routeSource = [[MGLShapeSource alloc] initWithIdentifier:@"style-route-source" shape:routeLine options:nil];
[self.mapView.style addSource:routeSource];
MGLLineStyleLayer *baseRouteLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"style-base-route-layer" source:routeSource];
baseRouteLayer.lineColor = [NSExpression expressionForConstantValue:[UIColor orangeColor]];
baseRouteLayer.lineWidth = [NSExpression expressionForConstantValue:@20];
baseRouteLayer.lineOpacity = [NSExpression expressionForConstantValue:@0.5];
baseRouteLayer.lineCap = [NSExpression expressionForConstantValue:@"round"];
baseRouteLayer.lineJoin = [NSExpression expressionForConstantValue:@"round"];
[self.mapView.style addLayer:baseRouteLayer];
MGLLineStyleLayer *routeLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"style-route-layer" source:routeSource];
routeLayer.lineColor = [NSExpression expressionForConstantValue:[UIColor whiteColor]];
routeLayer.lineWidth = [NSExpression expressionForConstantValue:@15];
routeLayer.lineOpacity = [NSExpression expressionForConstantValue:@0.8];
routeLayer.lineCap = [NSExpression expressionForConstantValue:@"round"];
routeLayer.lineJoin = [NSExpression expressionForConstantValue:@"round"];
[self.mapView.style addLayer:routeLayer];
}
- (void)styleAddLimeGreenTriangleLayer
{
LimeGreenStyleLayer *layer = [[LimeGreenStyleLayer alloc] initWithIdentifier:@"mbx-custom"];
[self.mapView.style addLayer:layer];
}
- (void)stylePolygonWithDDS {
CLLocationCoordinate2D leftCoords[] = {
{37.73081027834234, -122.49412536621094},
{37.7566013348511, -122.49412536621094},
{37.7566013348511, -122.46253967285156},
{37.73081027834234, -122.46253967285156},
{37.73081027834234, -122.49412536621094},
};
CLLocationCoordinate2D rightCoords[] = {
{37.73135334055843, -122.44640350341795},
{37.75741564287944, -122.44640350341795},
{37.75741564287944, -122.41310119628906},
{37.73135334055843, -122.41310119628906},
{37.73135334055843, -122.44640350341795},
};
MGLPolygonFeature *leftFeature = [MGLPolygonFeature polygonWithCoordinates:leftCoords count:5];
leftFeature.attributes = @{@"fill": @(YES)};
MGLPolygonFeature *rightFeature = [MGLPolygonFeature polygonWithCoordinates:rightCoords count:5];
rightFeature.attributes = @{@"opacity": @(0.5)};
MGLShapeSource *shapeSource = [[MGLShapeSource alloc] initWithIdentifier:@"shape-source" features:@[leftFeature, rightFeature] options:nil];
[self.mapView.style addSource:shapeSource];
// source, categorical function that sets any feature with a "fill" attribute value of true to red color and anything without to green
MGLFillStyleLayer *fillStyleLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"fill-layer" source:shapeSource];
fillStyleLayer.fillColor = [NSExpression mgl_expressionForConditional:[NSPredicate predicateWithFormat:@"fill == YES"]
trueExpression:[NSExpression expressionForConstantValue:[UIColor greenColor]]
falseExpresssion:[NSExpression expressionForConstantValue:[UIColor redColor]]];
// source, identity function that sets any feature with an "opacity" attribute to use that value and anything without to 1.0
fillStyleLayer.fillOpacity = [NSExpression mgl_expressionForConditional:[NSPredicate predicateWithFormat:@"opacity != nil"]
trueExpression:[NSExpression expressionForKeyPath:@"opacity"]
falseExpresssion:[NSExpression expressionForConstantValue:@1.0]];
[self.mapView.style addLayer:fillStyleLayer];
}
- (void)addLatLonGrid
{
MGLComputedShapeSource *source = [[MGLComputedShapeSource alloc] initWithIdentifier:@"latlon"
options:@{MGLShapeSourceOptionMaximumZoomLevel:@14}];
source.dataSource = self;
[self.mapView.style addSource:source];
MGLLineStyleLayer *lineLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"latlonlines"
source:source];
[self.mapView.style addLayer:lineLayer];
MGLSymbolStyleLayer *labelLayer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"latlonlabels"
source:source];
labelLayer.text = [NSExpression expressionForKeyPath:@"value"];
[self.mapView.style addLayer:labelLayer];
}
- (NSString *)bestLanguageForUser
{
// https://www.mapbox.com/vector-tiles/mapbox-streets-v8/#name-text--name_lang-code-text
NSArray *supportedLanguages = @[ @"ar", @"de", @"en", @"es", @"fr", @"ja", @"ko", @"pt", @"ru", @"zh", @"zh-Hans", @"zh-Hant" ];
NSArray<NSString *> *preferredLanguages = [NSBundle preferredLocalizationsFromArray:supportedLanguages forPreferences:[NSLocale preferredLanguages]];
NSString *mostSpecificLanguage;
for (NSString *language in preferredLanguages)
{
if (language.length > mostSpecificLanguage.length)
{
mostSpecificLanguage = language;
}
}
return mostSpecificLanguage ?: @"en";
}
- (IBAction)startWorldTour
{
_isTouringWorld = YES;
[self.mapView removeAnnotations:self.mapView.annotations];
NSUInteger numberOfAnnotations = sizeof(WorldTourDestinations) / sizeof(WorldTourDestinations[0]);
NSMutableArray *annotations = [NSMutableArray arrayWithCapacity:numberOfAnnotations];
for (NSUInteger i = 0; i < numberOfAnnotations; i++)
{
MBXDroppedPinAnnotation *annotation = [[MBXDroppedPinAnnotation alloc] init];
annotation.coordinate = WorldTourDestinations[i];
[annotations addObject:annotation];
}
[self.mapView addAnnotations:annotations];
[self continueWorldTourWithRemainingAnnotations:annotations];
}
- (void)continueWorldTourWithRemainingAnnotations:(NSMutableArray<MGLPointAnnotation *> *)annotations
{
MGLPointAnnotation *nextAnnotation = annotations.firstObject;
if (!nextAnnotation || !_isTouringWorld)
{
_isTouringWorld = NO;
return;
}
[annotations removeObjectAtIndex:0];
MGLMapCamera *camera = [MGLMapCamera cameraLookingAtCenterCoordinate:nextAnnotation.coordinate
acrossDistance:10
pitch:arc4random_uniform(60)
heading:arc4random_uniform(360)];
__weak MBXViewController *weakSelf = self;
[self.mapView flyToCamera:camera completionHandler:^{
MBXViewController *strongSelf = weakSelf;
[strongSelf performSelector:@selector(continueWorldTourWithRemainingAnnotations:)
withObject:annotations
afterDelay:2];
}];
}
- (void)toggleCustomUserDot
{
_customUserLocationAnnnotationEnabled = !_customUserLocationAnnnotationEnabled;
self.mapView.showsUserLocation = NO;
self.mapView.userTrackingMode = MGLUserTrackingModeFollow;
}
- (void)testQueryPointAnnotations {
NSNumber *visibleAnnotationCount = @(self.mapView.visibleAnnotations.count);
NSString *message;
if ([visibleAnnotationCount integerValue] == 1) {
message = [NSString stringWithFormat:@"There is %@ visible annotation.", visibleAnnotationCount];
} else {
message = [NSString stringWithFormat:@"There are %@ visible annotations.", visibleAnnotationCount];
}
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Visible Annotations" message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
}
- (id<MGLAnnotation>)randomOffscreenPointAnnotation {
NSPredicate *pointAnnotationPredicate = [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
return [evaluatedObject isKindOfClass:[MGLPointAnnotation class]];
}];
NSArray *annotations = [self.mapView.annotations filteredArrayUsingPredicate:pointAnnotationPredicate];
if (annotations.count == 0) {
return nil;
}
NSArray *visibleAnnotations = [self.mapView.visibleAnnotations filteredArrayUsingPredicate:pointAnnotationPredicate];
if (visibleAnnotations.count == annotations.count) {
return nil;
}
NSMutableArray *invisibleAnnotations = [annotations mutableCopy];
if (visibleAnnotations.count > 0) {
[invisibleAnnotations removeObjectsInArray:visibleAnnotations];
}
// Now pick a random offscreen annotation.
uint32_t index = arc4random_uniform((uint32_t)invisibleAnnotations.count);
return invisibleAnnotations[index];
}
- (void)selectAnOffscreenPointAnnotation {
id<MGLAnnotation> annotation = [self randomOffscreenPointAnnotation];
if (annotation) {
[self.mapView selectAnnotation:annotation animated:YES completionHandler:nil];
NSAssert(self.mapView.selectedAnnotations.firstObject, @"The annotation was not selected");
}
}
- (void)centerSelectedAnnotation {
id<MGLAnnotation> annotation = self.mapView.selectedAnnotations.firstObject;
if (!annotation)
return;
CGPoint point = [self.mapView convertCoordinate:annotation.coordinate toPointToView:self.mapView];
// Animate, so that point becomes the the center
CLLocationCoordinate2D center = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
[self.mapView setCenterCoordinate:center animated:YES];
}
- (void)addVisibleAreaPolyline {
CGRect constrainedRect = UIEdgeInsetsInsetRect(self.mapView.bounds, self.mapView.contentInset);
CLLocationCoordinate2D lineCoords[5];
lineCoords[0] = [self.mapView convertPoint: CGPointMake(CGRectGetMinX(constrainedRect), CGRectGetMinY(constrainedRect)) toCoordinateFromView:self.mapView];
lineCoords[1] = [self.mapView convertPoint: CGPointMake(CGRectGetMaxX(constrainedRect), CGRectGetMinY(constrainedRect)) toCoordinateFromView:self.mapView];
lineCoords[2] = [self.mapView convertPoint: CGPointMake(CGRectGetMaxX(constrainedRect), CGRectGetMaxY(constrainedRect)) toCoordinateFromView:self.mapView];
lineCoords[3] = [self.mapView convertPoint: CGPointMake(CGRectGetMinX(constrainedRect), CGRectGetMaxY(constrainedRect)) toCoordinateFromView:self.mapView];
lineCoords[4] = lineCoords[0];
MGLPolyline *line = [MGLPolyline polylineWithCoordinates:lineCoords
count:sizeof(lineCoords)/sizeof(lineCoords[0])];
[self.mapView addAnnotation:line];
}
- (void)loadMissingIcon
{
self.mapView.centerCoordinate = CLLocationCoordinate2DMake(0, 0);
self.mapView.zoomLevel = 1;
NSURL *customStyleJSON = [[NSBundle mainBundle] URLForResource:@"missing_icon" withExtension:@"json"];
[self.mapView setStyleURL:customStyleJSON];
}
- (UIImage *)mapView:(MGLMapView *)mapView didFailToLoadImage:(NSString *)imageName {
UIImage *backupImage = [UIImage imageNamed:@"AppIcon"];
return backupImage;
}
- (void)printTelemetryLogFile
{
NSString *fileContents = [NSString stringWithContentsOfFile:[self telemetryDebugLogFilePath] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", fileContents);
}
- (void)deleteTelemetryLogFile
{
NSString *filePath = [self telemetryDebugLogFilePath];
if ([[NSFileManager defaultManager] isDeletableFileAtPath:filePath])
{
NSError *error;
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
if (success) {
NSLog(@"Deleted telemetry log.");
} else {
NSLog(@"Error deleting telemetry log: %@", error.localizedDescription);
}
}
}
- (NSString *)telemetryDebugLogFilePath
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"telemetry_log-%@.json", [dateFormatter stringFromDate:[NSDate date]]]];
return filePath;
}
#pragma mark - Random World Tour
- (void)addAnnotations:(NSInteger)numAnnotations aroundCoordinate:(CLLocationCoordinate2D)coordinate radius:(CLLocationDistance)radius {
NSMutableArray *annotations = [[NSMutableArray alloc] initWithCapacity:numAnnotations];
for (NSInteger i = 0; i<numAnnotations; i++) {
CLLocationDegrees heading = (CLLocationDegrees)arc4random_uniform(360);
CLLocationDistance distance = (CLLocationDistance)arc4random_uniform(radius);
CLLocationCoordinate2D newLocation = coordinateCentered(coordinate, heading, distance);
MBXDroppedPinAnnotation *annotation = [[MBXDroppedPinAnnotation alloc] init];
annotation.coordinate = newLocation;
[annotations addObject:annotation];
}
[self.mapView addAnnotations:annotations];
}
- (void)randomWorldTour {
// Consistent initial conditions (consider setting these by test params)
srand48(0);
[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(31, -100) zoomLevel:3 animated:NO];
[self randomWorldTourInternal];
}
- (void)randomWorldTourInternal {
self.randomWalk = YES;
// Remove all annotations
NSTimeInterval duration = 16.0;
__weak MBXViewController *weakSelf = self;
// Remove old annotations, half-way through the flight.
NSArray *annotationsToRemove = [self.mapView.annotations copy];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * 0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.mapView removeAnnotations:annotationsToRemove];
});
MBXDroppedPinAnnotation *annotation = [[MBXDroppedPinAnnotation alloc] init];
annotation.coordinate = randomWorldCoordinate();
[self.mapView addAnnotation:annotation];
// Add annotations around that coord
[self addAnnotations:50 aroundCoordinate:annotation.coordinate radius:100000]; // 100km
MGLMapCamera *camera = [MGLMapCamera cameraLookingAtCenterCoordinate:annotation.coordinate
altitude:10000.0
pitch:(CLLocationDegrees)arc4random_uniform(60)
heading:(CLLocationDegrees)arc4random_uniform(360)];
[self.mapView flyToCamera:camera
withDuration:duration
peakAltitude:2000000.0
completionHandler:^{
// This completion handler is currently called BEFORE the
// region did change delegate method, and we don't have a "reason"
// so we can't tell if the motion was cancelled. We use the delegate
// for that, and set self.randomWalk. But since we want a delay
// anyway, we can just check later. Not ideal though..
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
MBXViewController *strongSelf = weakSelf;
if (strongSelf.randomWalk) {
[strongSelf randomWorldTourInternal];
}
});
}];
}
- (void)toggleSecondMapView {
if ([self.view viewWithTag:2] == nil) {
MGLMapView *secondMapView = [[MGLMapView alloc] initWithFrame:
CGRectMake(0, self.view.bounds.size.height / 2,
self.view.bounds.size.width, self.view.bounds.size.height / 2)];
secondMapView.showsScale = YES;
secondMapView.translatesAutoresizingMaskIntoConstraints = NO;
secondMapView.tag = 2;
for (NSLayoutConstraint *constraint in self.view.constraints)
{
if ((constraint.firstItem == self.mapView && constraint.firstAttribute == NSLayoutAttributeBottom) ||
(constraint.secondItem == self.mapView && constraint.secondAttribute == NSLayoutAttributeBottom))
{
[self.view removeConstraint:constraint];
break;
}
}
[self.view addSubview:secondMapView];
[self.view addConstraints:@[
[NSLayoutConstraint constraintWithItem:self.mapView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0],
[NSLayoutConstraint constraintWithItem:secondMapView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0],
[NSLayoutConstraint constraintWithItem:secondMapView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0],
[NSLayoutConstraint constraintWithItem:secondMapView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0],
[NSLayoutConstraint constraintWithItem:secondMapView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.bottomLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0],
]];
} else {
NSMutableArray *constraintsToRemove = [NSMutableArray array];
MGLMapView *secondMapView = (MGLMapView *)[self.view viewWithTag:2];
for (NSLayoutConstraint *constraint in self.view.constraints)
{
if (constraint.firstItem == secondMapView || constraint.secondItem == secondMapView)
{
[constraintsToRemove addObject:constraint];
}
}
[self.view removeConstraints:constraintsToRemove];
[secondMapView removeFromSuperview];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.mapView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.bottomLayoutGuide
attribute:NSLayoutAttributeTop
multiplier:1
constant:0]];
}
}
#pragma mark - User Actions
- (void)handleSingleTap:(UITapGestureRecognizer *)singleTap {
[self.navigationController setNavigationBarHidden:!self.navigationController.navigationBarHidden animated:YES];
// This is how you'd get the coordinate for the point where the user tapped:
// CGPoint tapPoint = [singleTap locationInView:self.mapView];
// CLLocationCoordinate2D tapCoordinate = [self.mapView convertPoint:tapPoint toCoordinateFromView:nil];
}
- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan)
{
CGPoint point = [longPress locationInView:longPress.view];
NSArray *features = [self.mapView visibleFeaturesAtPoint:point];
NSString *title;
for (id <MGLFeature> feature in features) {
if (!title) {
title = [feature attributeForKey:@"name_en"] ?: [feature attributeForKey:@"name"];
}
}
MBXDroppedPinAnnotation *pin = [[MBXDroppedPinAnnotation alloc] init];
pin.coordinate = [self.mapView convertPoint:point
toCoordinateFromView:self.mapView];
pin.title = title ?: @"Dropped Pin";
pin.subtitle = [[[MGLCoordinateFormatter alloc] init] stringFromCoordinate:pin.coordinate];
// Calling `addAnnotation:` on mapView is required here (since `selectAnnotation:animated` has
// the side effect of adding the annotation if required, but returning an incorrect callout
// positioning rect)
[self.mapView addAnnotation:pin];
[self.mapView selectAnnotation:pin animated:YES completionHandler:nil];
}
}
- (IBAction)cycleStyles:(__unused id)sender
{
static NSArray *styleNames;
static NSArray *styleURLs;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
styleNames = @[
@"Streets",
@"Outdoors",
@"Light",
@"Dark",
@"Satellite",
@"Satellite Streets",
];
styleURLs = @[
[MGLStyle streetsStyleURL],
[MGLStyle outdoorsStyleURL],
[MGLStyle lightStyleURL],
[MGLStyle darkStyleURL],
[MGLStyle satelliteStyleURL],
[MGLStyle satelliteStreetsStyleURL]
];
NSAssert(styleNames.count == styleURLs.count, @"Style names and URLs don’t match.");
// Make sure defaultStyleURLs is up-to-date.
unsigned numMethods = 0;
Method *methods = class_copyMethodList(object_getClass([MGLStyle class]), &numMethods);
unsigned numStyleURLMethods = 0;
for (NSUInteger i = 0; i < numMethods; i++) {
Method method = methods[i];
if (method_getNumberOfArguments(method) == 3 /* _cmd, self, version */) {
SEL selector = method_getName(method);
NSString *name = @(sel_getName(selector));
if ([name hasSuffix:@"StyleURLWithVersion:"]) {
numStyleURLMethods += 1;
}
}
}
NSAssert(numStyleURLMethods == styleNames.count,
@"MGLStyle provides %u default styles but iosapp only knows about %lu of them.",
numStyleURLMethods, (unsigned long)styleNames.count);
});
self.styleIndex = (self.styleIndex + 1) % styleNames.count;
self.mapView.styleURL = styleURLs[self.styleIndex];
UIButton *titleButton = (UIButton *)self.navigationItem.titleView;
[titleButton setTitle:styleNames[self.styleIndex] forState:UIControlStateNormal];
}
- (IBAction)locateUser:(id)sender
{
MGLUserTrackingMode nextMode;
NSString *nextAccessibilityValue;
switch (self.mapView.userTrackingMode) {
case MGLUserTrackingModeNone:
nextMode = MGLUserTrackingModeFollow;
nextAccessibilityValue = @"Follow location";
break;
case MGLUserTrackingModeFollow:
nextMode = MGLUserTrackingModeFollowWithHeading;
nextAccessibilityValue = @"Follow location and heading";
break;
case MGLUserTrackingModeFollowWithHeading:
nextMode = MGLUserTrackingModeFollowWithCourse;
nextAccessibilityValue = @"Follow course";
break;
case MGLUserTrackingModeFollowWithCourse:
nextMode = MGLUserTrackingModeNone;
nextAccessibilityValue = @"Off";
break;
}
self.mapView.userTrackingMode = nextMode;
[sender setAccessibilityValue:nextAccessibilityValue];
}
#pragma mark - UIViewDelegate
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
if (_contentInsetsEnabled)
{
_contentInsetsEnabled = NO;
self.automaticallyAdjustsScrollViewInsets = YES;
[self.mapView setContentInset:UIEdgeInsetsZero];
}
while (self.contentInsetsOverlays && [self.contentInsetsOverlays count]) {
[[self.contentInsetsOverlays lastObject] removeFromSuperview];
[self.contentInsetsOverlays removeLastObject];
}
}
#pragma mark - MGLMapViewDelegate
- (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id<MGLAnnotation>)annotation
{
if (annotation == mapView.userLocation)
{
if (_customUserLocationAnnnotationEnabled)
{
MBXUserLocationAnnotationView *annotationView = [[MBXUserLocationAnnotationView alloc] initWithFrame:CGRectZero];
annotationView.frame = CGRectMake(0, 0, annotationView.intrinsicContentSize.width, annotationView.intrinsicContentSize.height);
return annotationView;
}
return nil;
}
// Use GL backed pins for dropped pin annotations
if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]] || [annotation isKindOfClass:[MBXSpriteBackedAnnotation class]])
{
return nil;
}
MBXAnnotationView *annotationView = (MBXAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:MBXViewControllerAnnotationViewReuseIdentifer];
if (!annotationView)
{
annotationView = [[MBXAnnotationView alloc] initWithReuseIdentifier:MBXViewControllerAnnotationViewReuseIdentifer];
annotationView.frame = CGRectMake(0, 0, 10, 10);
annotationView.backgroundColor = [UIColor whiteColor];
// Note that having two long press gesture recognizers on overlapping
// views (`self.view` & `annotationView`) will cause weird behavior.
// Comment out the pin dropping functionality in the handleLongPress:
// method in this class to make draggable annotation views play nice.
annotationView.draggable = YES;
} else {
// orange indicates that the annotation view was reused
annotationView.backgroundColor = [UIColor orangeColor];
}
return annotationView;
}
- (MGLAnnotationImage *)mapView:(MGLMapView * __nonnull)mapView imageForAnnotation:(id <MGLAnnotation> __nonnull)annotation
{
if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]] || [annotation isKindOfClass:[MBXCustomCalloutAnnotation class]])
{
return nil; // use default marker
}
NSAssert([annotation isKindOfClass:[MBXSpriteBackedAnnotation class]], @"Annotations should be sprite-backed.");
NSString *title = [(MGLPointAnnotation *)annotation title];
if (!title.length) return nil;
NSString *lastTwoCharacters = [title substringFromIndex:title.length - 2];
MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:lastTwoCharacters];
if ( ! annotationImage)
{
UIColor *color;
// make every tenth annotation blue
if ([lastTwoCharacters hasSuffix:@"0"]) {
color = [UIColor blueColor];
} else {
color = [UIColor redColor];
}
UIImage *image = [self imageWithText:lastTwoCharacters backgroundColor:color];
annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:lastTwoCharacters];
// don't allow touches on blue annotations
if ([color isEqual:[UIColor blueColor]]) annotationImage.enabled = NO;
}
return annotationImage;
}
- (UIImage *)imageWithText:(NSString *)text backgroundColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 20, 15);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, [[color colorWithAlphaComponent:0.75] CGColor]);
CGContextFillRect(ctx, rect);
CGContextSetStrokeColorWithColor(ctx, [[UIColor blackColor] CGColor]);
CGContextStrokeRectWithWidth(ctx, rect, 2);
NSAttributedString *drawString = [[NSAttributedString alloc] initWithString:text attributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Arial-BoldMT" size:12],
NSForegroundColorAttributeName: [UIColor whiteColor],
}];
CGSize stringSize = drawString.size;
CGRect stringRect = CGRectMake((rect.size.width - stringSize.width) / 2,
(rect.size.height - stringSize.height) / 2,
stringSize.width,
stringSize.height);
[drawString drawInRect:stringRect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (BOOL)mapView:(__unused MGLMapView *)mapView annotationCanShowCallout:(__unused id <MGLAnnotation>)annotation
{
return YES;
}
- (CGFloat)mapView:(__unused MGLMapView *)mapView alphaForShapeAnnotation:(MGLShape *)annotation
{
return ([annotation isKindOfClass:[MGLPolygon class]] ? 0.5 : 1.0);
}
- (UIColor *)mapView:(__unused MGLMapView *)mapView strokeColorForShapeAnnotation:(MGLShape *)annotation
{
UIColor *color = [annotation isKindOfClass:[MGLPolyline class]] ? [UIColor greenColor] : [UIColor blackColor];
return [color colorWithAlphaComponent:0.9];
}
- (UIColor *)mapView:(__unused MGLMapView *)mapView fillColorForPolygonAnnotation:(__unused MGLPolygon *)annotation
{
UIColor *color = annotation.pointCount > 3 ? [UIColor greenColor] : [UIColor redColor];
return [color colorWithAlphaComponent:0.5];
}
- (void)mapView:(__unused MGLMapView *)mapView didChangeUserTrackingMode:(MGLUserTrackingMode)mode animated:(__unused BOOL)animated
{
UIImage *newButtonImage;
NSString *newButtonTitle;
switch (mode) {
case MGLUserTrackingModeNone:
newButtonImage = [UIImage imageNamed:@"TrackingLocationOffMask.png"];
break;
case MGLUserTrackingModeFollow:
newButtonImage = [UIImage imageNamed:@"TrackingLocationMask.png"];
break;
case MGLUserTrackingModeFollowWithHeading:
newButtonImage = [UIImage imageNamed:@"TrackingHeadingMask.png"];
break;
case MGLUserTrackingModeFollowWithCourse:
newButtonImage = nil;
newButtonTitle = @"Course";
break;
}
self.navigationItem.rightBarButtonItem.title = newButtonTitle;
[UIView animateWithDuration:0.25 animations:^{
self.navigationItem.rightBarButtonItem.image = newButtonImage;
}];
}
- (nullable id <MGLCalloutView>)mapView:(__unused MGLMapView *)mapView calloutViewForAnnotation:(id<MGLAnnotation>)annotation
{
if ([annotation respondsToSelector:@selector(title)]
&& [annotation isKindOfClass:[MBXCustomCalloutAnnotation class]])
{
MBXCustomCalloutAnnotation *customAnnotation = (MBXCustomCalloutAnnotation *)annotation;
MBXCustomCalloutView *calloutView = [[MBXCustomCalloutView alloc] init];
calloutView.representedObject = annotation;
calloutView.anchoredToAnnotation = customAnnotation.anchoredToAnnotation;
calloutView.dismissesAutomatically = customAnnotation.dismissesAutomatically;
return calloutView;
}
return nil;
}
- (UIView *)mapView:(__unused MGLMapView *)mapView leftCalloutAccessoryViewForAnnotation:(__unused id<MGLAnnotation>)annotation
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectZero;
[button setTitle:@"Left" forState:UIControlStateNormal];
[button sizeToFit];
return button;
}
- (UIView *)mapView:(__unused MGLMapView *)mapView rightCalloutAccessoryViewForAnnotation:(__unused id<MGLAnnotation>)annotation
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectZero;
[button setTitle:@"Right" forState:UIControlStateNormal];
[button sizeToFit];
return button;
}
- (void)mapView:(MGLMapView *)mapView tapOnCalloutForAnnotation:(id <MGLAnnotation>)annotation
{
if ( ! [annotation isKindOfClass:[MGLPointAnnotation class]])
{
return;
}
MGLPointAnnotation *point = (MGLPointAnnotation *)annotation;
point.coordinate = [self.mapView convertPoint:self.mapView.center toCoordinateFromView:self.mapView];
}
- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style
{
// Default Mapbox styles use {name_en} as their label language, which means
// that a device with an English-language locale is already effectively
// using locale-based country labels.
_localizingLabels = [[self bestLanguageForUser] isEqualToString:@"en"];
}
- (BOOL)mapView:(MGLMapView *)mapView shouldChangeFromCamera:(MGLMapCamera *)oldCamera toCamera:(MGLMapCamera *)newCamera {
if (_shouldLimitCameraChanges) {
// Get the current camera to restore it after.
MGLMapCamera *currentCamera = mapView.camera;
// From the new camera obtain the center to test if it’s inside the boundaries.
CLLocationCoordinate2D newCameraCenter = newCamera.centerCoordinate;
// Set the map’s visible bounds to newCamera.
mapView.camera = newCamera;
MGLCoordinateBounds newVisibleCoordinates = mapView.visibleCoordinateBounds;
// Revert the camera.
mapView.camera = currentCamera;
// Test if the newCameraCenter and newVisibleCoordinates are inside Colorado.
BOOL inside = MGLCoordinateInCoordinateBounds(newCameraCenter, colorado);
BOOL intersects = MGLCoordinateInCoordinateBounds(newVisibleCoordinates.ne, colorado) && MGLCoordinateInCoordinateBounds(newVisibleCoordinates.sw, colorado);
return inside && intersects;
} else {
return YES;
}
}
- (void)mapViewRegionIsChanging:(MGLMapView *)mapView
{
[self updateHUD];
[self updateHelperMapViews];
}
- (void)mapView:(MGLMapView *)mapView regionDidChangeWithReason:(MGLCameraChangeReason)reason animated:(BOOL)animated
{
if (reason != MGLCameraChangeReasonProgrammatic) {
self.randomWalk = NO;
}
[self updateHUD];
[self updateHelperMapViews];
}
- (void)mapView:(MGLMapView *)mapView didUpdateUserLocation:(MGLUserLocation *)userLocation {
[self updateHUD];
}
- (void)updateHelperMapViews {
for (UIWindow *window in self.helperWindows) {
MGLMapView *mapView = (MGLMapView *)window.rootViewController.view;
mapView.camera = self.mapView.camera;
}
}
- (void)updateHUD {
if (self.reuseQueueStatsEnabled == NO && self.zoomLevelOrnamentEnabled == NO) {
return;
}
NSString *hudString;
if (self.reuseQueueStatsEnabled) {
NSUInteger queuedAnnotations = 0;
for (NSArray *queue in self.mapView.annotationViewReuseQueueByIdentifier.allValues) {
queuedAnnotations += queue.count;
}
hudString = [NSString stringWithFormat:@"Visible: %ld Queued: %ld", (unsigned long)self.mapView.visibleAnnotations.count, (unsigned long)queuedAnnotations];
} else if (self.zoomLevelOrnamentEnabled) {
hudString = [NSString stringWithFormat:@"%.f FPS (%.1fms) ∕ %.2f ∕ ↕\U0000FE0E%.f° ∕ %.f°",
roundf(self.mapView.averageFrameRate), self.mapView.averageFrameTime,
self.mapView.zoomLevel, self.mapView.camera.pitch, self.mapView.direction];
}
[self.hudLabel setTitle:hudString forState:UIControlStateNormal];
}
#pragma mark - MGLComputedShapeSourceDataSource
- (NSArray<id <MGLFeature>>*)featuresInCoordinateBounds:(MGLCoordinateBounds)bounds zoomLevel:(NSUInteger)zoom {
double gridSpacing;
if(zoom >= 13) {
gridSpacing = 0.01;
} else if(zoom >= 11) {
gridSpacing = 0.05;
} else if(zoom == 10) {
gridSpacing = .1;
} else if(zoom == 9) {
gridSpacing = 0.25;
} else if(zoom == 8) {
gridSpacing = 0.5;
} else if (zoom >= 6) {
gridSpacing = 1;
} else if(zoom == 5) {
gridSpacing = 2;
} else if(zoom >= 4) {
gridSpacing = 5;
} else if(zoom == 2) {
gridSpacing = 10;
} else {
gridSpacing = 20;
}
NSMutableArray <id <MGLFeature>> * features = [NSMutableArray array];
CLLocationCoordinate2D coords[2];
for (double y = ceil(bounds.ne.latitude / gridSpacing) * gridSpacing; y >= floor(bounds.sw.latitude / gridSpacing) * gridSpacing; y -= gridSpacing) {
coords[0] = CLLocationCoordinate2DMake(y, bounds.sw.longitude);
coords[1] = CLLocationCoordinate2DMake(y, bounds.ne.longitude);
MGLPolylineFeature *feature = [MGLPolylineFeature polylineWithCoordinates:coords count:2];
feature.attributes = @{@"value": @(y)};
[features addObject:feature];
}
for (double x = floor(bounds.sw.longitude / gridSpacing) * gridSpacing; x <= ceil(bounds.ne.longitude / gridSpacing) * gridSpacing; x += gridSpacing) {
coords[0] = CLLocationCoordinate2DMake(bounds.sw.latitude, x);
coords[1] = CLLocationCoordinate2DMake(bounds.ne.latitude, x);
MGLPolylineFeature *feature = [MGLPolylineFeature polylineWithCoordinates:coords count:2];
feature.attributes = @{@"value": @(x)};
[features addObject:feature];
}
return features;
}
- (void)mapViewDidFinishRenderingFrame:(MGLMapView *)mapView fullyRendered:(BOOL)fullyRendered {
if (self.frameTimeGraphEnabled) {
[self.frameTimeGraphView updatePathWithFrameDuration:mapView.frameTime];
}
}
- (void)saveCurrentMapState:(__unused NSNotification *)notification {
// The following properties can change after the view loads so we need to save their
// state before exiting the view controller.
self.currentState.camera = self.mapView.camera;
self.currentState.showsUserLocation = self.mapView.showsUserLocation;
self.currentState.userTrackingMode = self.mapView.userTrackingMode;
self.currentState.showsUserHeadingIndicator = self.mapView.showsUserHeadingIndicator;
self.currentState.showsMapScale = self.mapView.showsScale;
self.currentState.showsZoomLevelOrnament = self.zoomLevelOrnamentEnabled;
self.currentState.showsTimeFrameGraph = self.frameTimeGraphEnabled;
self.currentState.debugMask = self.mapView.debugMask;
self.currentState.debugLoggingEnabled = self.debugLoggingEnabled;
self.currentState.reuseQueueStatsEnabled = self.reuseQueueStatsEnabled;
[[MBXStateManager sharedManager] saveState:self.currentState];
}
- (void)restoreMapState:(__unused NSNotification *)notification {
MBXState *currentState = [MBXStateManager sharedManager].currentState;
self.mapView.camera = currentState.camera;
self.mapView.showsUserLocation = currentState.showsUserLocation;
self.mapView.userTrackingMode = currentState.userTrackingMode;
self.mapView.showsUserHeadingIndicator = currentState.showsUserHeadingIndicator;
self.mapView.showsScale = currentState.showsMapScale;
self.zoomLevelOrnamentEnabled = currentState.showsZoomLevelOrnament;
self.frameTimeGraphEnabled = currentState.showsTimeFrameGraph;
self.mapView.debugMask = currentState.debugMask;
self.debugLoggingEnabled = currentState.debugLoggingEnabled;
self.reuseQueueStatsEnabled = currentState.reuseQueueStatsEnabled;
self.currentState = currentState;
}
@end
|