Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit fed3eda

Browse files
Jesse Crockerkkaefer
Jesse Crocker
authored andcommitted
[ios,macos] Add custom vector source example to demo apps
1 parent ba59fc2 commit fed3eda

File tree

3 files changed

+168
-2
lines changed

3 files changed

+168
-2
lines changed

platform/ios/app/MBXViewController.m

+70-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ typedef NS_ENUM(NSInteger, MBXSettingsRuntimeStylingRows) {
7272
MBXSettingsRuntimeStylingCountryLabels,
7373
MBXSettingsRuntimeStylingRouteLine,
7474
MBXSettingsRuntimeStylingDDSPolygon,
75+
MBXSettingsRuntimeStylingCustomLatLonGrid,
7576
};
7677

7778
typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
@@ -105,7 +106,8 @@ @implementation MBXSpriteBackedAnnotation
105106

106107
@interface MBXViewController () <UITableViewDelegate,
107108
UITableViewDataSource,
108-
MGLMapViewDelegate>
109+
MGLMapViewDelegate,
110+
MGLComputedShapeSourceDataSource>
109111

110112

111113
@property (nonatomic) IBOutlet MGLMapView *mapView;
@@ -342,6 +344,7 @@ - (void)dismissSettings:(__unused id)sender
342344
[NSString stringWithFormat:@"Label Countries in %@", (_usingLocaleBasedCountryLabels ? @"Local Language" : [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[self bestLanguageForUser]])],
343345
@"Add Route Line",
344346
@"Dynamically Style Polygon",
347+
@"Add Custom Lat/Lon Grid",
345348
]];
346349
break;
347350
case MBXSettingsMiscellaneous:
@@ -581,6 +584,9 @@ - (void)performActionForSettingAtIndexPath:(NSIndexPath *)indexPath
581584
case MBXSettingsRuntimeStylingDDSPolygon:
582585
[self stylePolygonWithDDS];
583586
break;
587+
case MBXSettingsRuntimeStylingCustomLatLonGrid:
588+
[self addLatLonGrid];
589+
break;
584590
default:
585591
NSAssert(NO, @"All runtime styling setting rows should be implemented");
586592
break;
@@ -1330,6 +1336,21 @@ - (void)stylePolygonWithDDS {
13301336
[self.mapView.style addLayer:fillStyleLayer];
13311337
}
13321338

1339+
- (void)addLatLonGrid
1340+
{
1341+
MGLComputedShapeSource *source = [[MGLComputedShapeSource alloc] initWithIdentifier:@"latlon"
1342+
options:@{MGLShapeSourceOptionMaximumZoomLevel:@14}];
1343+
source.dataSource = self;
1344+
[self.mapView.style addSource:source];
1345+
MGLLineStyleLayer *lineLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"latlonlines"
1346+
source:source];
1347+
[self.mapView.style addLayer:lineLayer];
1348+
MGLSymbolStyleLayer *labelLayer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"latlonlabels"
1349+
source:source];
1350+
labelLayer.text = [MGLStyleValue valueWithRawValue:@"{value}"];
1351+
[self.mapView.style addLayer:labelLayer];
1352+
}
1353+
13331354
- (void)styleLabelLanguageForLayersNamed:(NSArray<NSString *> *)layers
13341355
{
13351356
_usingLocaleBasedCountryLabels = !_usingLocaleBasedCountryLabels;
@@ -1811,4 +1832,52 @@ - (void)mapView:(MGLMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
18111832
}
18121833
}
18131834

1835+
#pragma mark - MGLComputedShapeSourceDataSource
1836+
1837+
- (NSArray<id <MGLFeature>>*)featuresInCoordinateBounds:(MGLCoordinateBounds)bounds zoomLevel:(NSUInteger)zoom {
1838+
double gridSpacing;
1839+
if(zoom >= 13) {
1840+
gridSpacing = 0.01;
1841+
} else if(zoom >= 11) {
1842+
gridSpacing = 0.05;
1843+
} else if(zoom == 10) {
1844+
gridSpacing = .1;
1845+
} else if(zoom == 9) {
1846+
gridSpacing = 0.25;
1847+
} else if(zoom == 8) {
1848+
gridSpacing = 0.5;
1849+
} else if (zoom >= 6) {
1850+
gridSpacing = 1;
1851+
} else if(zoom == 5) {
1852+
gridSpacing = 2;
1853+
} else if(zoom >= 4) {
1854+
gridSpacing = 5;
1855+
} else if(zoom == 2) {
1856+
gridSpacing = 10;
1857+
} else {
1858+
gridSpacing = 20;
1859+
}
1860+
1861+
NSMutableArray <id <MGLFeature>> * features = [NSMutableArray array];
1862+
CLLocationCoordinate2D coords[2];
1863+
1864+
for (double y = ceil(bounds.ne.latitude / gridSpacing) * gridSpacing; y >= floor(bounds.sw.latitude / gridSpacing) * gridSpacing; y -= gridSpacing) {
1865+
coords[0] = CLLocationCoordinate2DMake(y, bounds.sw.longitude);
1866+
coords[1] = CLLocationCoordinate2DMake(y, bounds.ne.longitude);
1867+
MGLPolylineFeature *feature = [MGLPolylineFeature polylineWithCoordinates:coords count:2];
1868+
feature.attributes = @{@"value": @(y)};
1869+
[features addObject:feature];
1870+
}
1871+
1872+
for (double x = floor(bounds.sw.longitude / gridSpacing) * gridSpacing; x <= ceil(bounds.ne.longitude / gridSpacing) * gridSpacing; x += gridSpacing) {
1873+
coords[0] = CLLocationCoordinate2DMake(bounds.sw.latitude, x);
1874+
coords[1] = CLLocationCoordinate2DMake(bounds.ne.latitude, x);
1875+
MGLPolylineFeature *feature = [MGLPolylineFeature polylineWithCoordinates:coords count:2];
1876+
feature.attributes = @{@"value": @(x)};
1877+
[features addObject:feature];
1878+
}
1879+
1880+
return features;
1881+
}
1882+
18141883
@end

platform/macos/app/Base.lproj/MainMenu.xib

+6
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@
544544
<action selector="insertCustomStyleLayer:" target="-1" id="LE5-lz-kx3"/>
545545
</connections>
546546
</menuItem>
547+
<menuItem title="Add Graticule" id="Msk-p2-Lwt">
548+
<modifierMask key="keyEquivalentModifierMask"/>
549+
<connections>
550+
<action selector="insertGraticuleLayer:" target="-1" id="LE5-lz-kx4"/>
551+
</connections>
552+
</menuItem>
547553
<menuItem title="Show All Annnotations" keyEquivalent="A" id="yMj-uM-8SN">
548554
<modifierMask key="keyEquivalentModifierMask" shift="YES" command="YES"/>
549555
<connections>

platform/macos/app/MapDocument.m

+92-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
return flattenedShapes;
5050
}
5151

52-
@interface MapDocument () <NSWindowDelegate, NSSharingServicePickerDelegate, NSMenuDelegate, NSSplitViewDelegate, MGLMapViewDelegate>
52+
@interface MapDocument () <NSWindowDelegate, NSSharingServicePickerDelegate, NSMenuDelegate, NSSplitViewDelegate, MGLMapViewDelegate, MGLComputedShapeSourceDataSource>
5353

5454
@property (weak) IBOutlet NSArrayController *styleLayersArrayController;
5555
@property (weak) IBOutlet NSTableView *styleLayersTableView;
@@ -639,6 +639,47 @@ - (IBAction)removeCustomStyleLayer:(id)sender {
639639
[self.mapView.style removeLayer:layer];
640640
}
641641

642+
- (IBAction)insertGraticuleLayer:(id)sender {
643+
[self.undoManager registerUndoWithTarget:self handler:^(id _Nonnull target) {
644+
[self removeGraticuleLayer:sender];
645+
}];
646+
647+
if (!self.undoManager.isUndoing) {
648+
[self.undoManager setActionName:@"Add Graticule Layer"];
649+
}
650+
651+
MGLComputedShapeSource *source = [[MGLComputedShapeSource alloc] initWithIdentifier:@"graticule"
652+
options:@{MGLShapeSourceOptionMaximumZoomLevel:@14}];
653+
source.dataSource = self;
654+
[self.mapView.style addSource:source];
655+
MGLLineStyleLayer *lineLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"graticule.lines"
656+
source:source];
657+
[self.mapView.style addLayer:lineLayer];
658+
MGLSymbolStyleLayer *labelLayer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"graticule.labels"
659+
source:source];
660+
labelLayer.text = [MGLStyleValue valueWithRawValue:@"{value}"];
661+
[self.mapView.style addLayer:labelLayer];
662+
}
663+
664+
- (IBAction)removeGraticuleLayer:(id)sender {
665+
[self.undoManager registerUndoWithTarget:self handler:^(id _Nonnull target) {
666+
[self insertGraticuleLayer:sender];
667+
}];
668+
669+
if (!self.undoManager.isUndoing) {
670+
[self.undoManager setActionName:@"Delete Graticule Layer"];
671+
}
672+
673+
MGLStyleLayer *layer = [self.mapView.style layerWithIdentifier:@"graticule.lines"];
674+
[self.mapView.style removeLayer:layer];
675+
676+
layer = [self.mapView.style layerWithIdentifier:@"graticule.labels"];
677+
[self.mapView.style removeLayer:layer];
678+
679+
MGLSource *source = [self.mapView.style sourceWithIdentifier:@"graticule"];
680+
[self.mapView.style removeSource:source];
681+
}
682+
642683
#pragma mark Offline packs
643684

644685
- (IBAction)addOfflinePack:(id)sender {
@@ -926,6 +967,9 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
926967
if (menuItem.action == @selector(insertCustomStyleLayer:)) {
927968
return ![self.mapView.style layerWithIdentifier:@"mbx-custom"];
928969
}
970+
if (menuItem.action == @selector(insertGraticuleLayer:)) {
971+
return ![self.mapView.style sourceWithIdentifier:@"graticule"];
972+
}
929973
if (menuItem.action == @selector(showAllAnnotations:) || menuItem.action == @selector(removeAllAnnotations:)) {
930974
return self.mapView.annotations.count > 0;
931975
}
@@ -1095,6 +1139,53 @@ - (CGFloat)mapView:(MGLMapView *)mapView alphaForShapeAnnotation:(MGLShape *)ann
10951139
return 0.8;
10961140
}
10971141

1142+
#pragma mark - MGLComputedShapeSourceDataSource
1143+
- (NSArray<id <MGLFeature>>*)featuresInCoordinateBounds:(MGLCoordinateBounds)bounds zoomLevel:(NSUInteger)zoom {
1144+
double gridSpacing;
1145+
if(zoom >= 13) {
1146+
gridSpacing = 0.01;
1147+
} else if(zoom >= 11) {
1148+
gridSpacing = 0.05;
1149+
} else if(zoom == 10) {
1150+
gridSpacing = .1;
1151+
} else if(zoom == 9) {
1152+
gridSpacing = 0.25;
1153+
} else if(zoom == 8) {
1154+
gridSpacing = 0.5;
1155+
} else if (zoom >= 6) {
1156+
gridSpacing = 1;
1157+
} else if(zoom == 5) {
1158+
gridSpacing = 2;
1159+
} else if(zoom >= 4) {
1160+
gridSpacing = 5;
1161+
} else if(zoom == 2) {
1162+
gridSpacing = 10;
1163+
} else {
1164+
gridSpacing = 20;
1165+
}
1166+
1167+
NSMutableArray <id <MGLFeature>> * features = [NSMutableArray array];
1168+
CLLocationCoordinate2D coords[2];
1169+
1170+
for (double y = ceil(bounds.ne.latitude / gridSpacing) * gridSpacing; y >= floor(bounds.sw.latitude / gridSpacing) * gridSpacing; y -= gridSpacing) {
1171+
coords[0] = CLLocationCoordinate2DMake(y, bounds.sw.longitude);
1172+
coords[1] = CLLocationCoordinate2DMake(y, bounds.ne.longitude);
1173+
MGLPolylineFeature *feature = [MGLPolylineFeature polylineWithCoordinates:coords count:2];
1174+
feature.attributes = @{@"value": @(y)};
1175+
[features addObject:feature];
1176+
}
1177+
1178+
for (double x = floor(bounds.sw.longitude / gridSpacing) * gridSpacing; x <= ceil(bounds.ne.longitude / gridSpacing) * gridSpacing; x += gridSpacing) {
1179+
coords[0] = CLLocationCoordinate2DMake(bounds.sw.latitude, x);
1180+
coords[1] = CLLocationCoordinate2DMake(bounds.ne.latitude, x);
1181+
MGLPolylineFeature *feature = [MGLPolylineFeature polylineWithCoordinates:coords count:2];
1182+
feature.attributes = @{@"value": @(x)};
1183+
[features addObject:feature];
1184+
}
1185+
1186+
return features;
1187+
}
1188+
10981189
@end
10991190

11001191
@interface ValidatedToolbarItem : NSToolbarItem

0 commit comments

Comments
 (0)