Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit 33c2d87

Browse files
author
Sam Fader
committed
update draft of ObjC example
1 parent 6d89cb1 commit 33c2d87

File tree

1 file changed

+124
-25
lines changed

1 file changed

+124
-25
lines changed

Examples/ObjectiveC/SymbolLayerZOrderExample.m

Lines changed: 124 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ - (void)viewDidLoad {
3030
self.mapView.tintColor = [UIColor darkGrayColor];
3131

3232
// Set the map’s center coordinate and zoom level.
33-
self.mapView.centerCoordinate = CLLocationCoordinate2DMake(38.897,-77.039);
34-
self.mapView.zoomLevel = 10.5;
33+
self.mapView.centerCoordinate = CLLocationCoordinate2DMake(-41.292650,174.778768);
34+
self.mapView.zoomLevel = 11.5;
3535

3636
self.mapView.delegate = self;
3737
[self.view addSubview: self.mapView];
@@ -40,32 +40,131 @@ - (void)viewDidLoad {
4040
// Wait until the style is loaded before modifying the map style.
4141
- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {
4242

43-
// "mapbox://examples.2uf7qges" is a map ID referencing a tileset. For more
44-
// more information, see mapbox.com/help/define-map-id/
45-
MGLSource *source = [[MGLVectorTileSource alloc] initWithIdentifier:@"trees" configurationURL:[NSURL URLWithString:@"mapbox://examples.2uf7qges"]];
46-
47-
[self.mapView.style addSource:source];
48-
49-
MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithIdentifier: @"tree-style" source:source];
50-
51-
// The source name from the source's TileJSON metadata: mapbox.com/api-documentation/#retrieve-tilejson-metadata
52-
layer.sourceLayerIdentifier = @"yoshino-trees-a0puw5";
53-
54-
// Stops based on age of tree in years.
55-
NSDictionary *stops = @{
56-
@0: [UIColor colorWithRed:1.00 green:0.72 blue:0.85 alpha:1.0],
57-
@2: [UIColor colorWithRed:0.69 green:0.48 blue:0.73 alpha:1.0],
58-
@4: [UIColor colorWithRed:0.61 green:0.31 blue:0.47 alpha:1.0],
59-
@7: [UIColor colorWithRed:0.43 green:0.20 blue:0.38 alpha:1.0],
60-
@16: [UIColor colorWithRed:0.33 green:0.17 blue:0.25 alpha:1.0]
61-
};
43+
// Add icons to the map's style.
44+
[style setImage:([UIImage imageNamed:@"oval"]) forName:@"oval"];
45+
[style setImage:([UIImage imageNamed:@"squircle"]) forName:@"squircle"];
46+
[style setImage:([UIImage imageNamed:@"star"]) forName:@"star"];
47+
// style.setImage(UIImage(named: "oval")!, forName: "oval")
48+
// style.setImage(UIImage(named: "squircle")!, forName: "squircle")
49+
// style.setImage(UIImage(named: "star")!, forName: "star")
50+
//
51+
MGLPointFeature *feature1 = [MGLPointFeature alloc];
52+
feature1.coordinate = CLLocationCoordinate2DMake(-41.292650, 174.778768);
53+
feature1.attributes = @{@"id": @"squircle"};
54+
// let feature1 = MGLPointFeature()
55+
// feature1.coordinate = CLLocationCoordinate2DMake(-41.292650, 174.778768)
56+
// feature1.attributes = ["id": "squircle"]
57+
MGLPointFeature *feature2 = [MGLPointFeature alloc];
58+
feature2.coordinate = CLLocationCoordinate2DMake(-41.292650, 174.778768);
59+
feature2.attributes = @{@"id": @"oval"};
60+
// let feature2 = MGLPointFeature()
61+
// feature2.coordinate = CLLocationCoordinate2DMake(-41.292650, 174.778768)
62+
// feature2.attributes = ["id": "oval"]
63+
MGLPointFeature *feature3 = [MGLPointFeature alloc];
64+
feature3.coordinate = CLLocationCoordinate2DMake(-41.292650, 174.778768);
65+
feature3.attributes = @{@"id": @"star"};
66+
// let feature3 = MGLPointFeature()
67+
// feature3.coordinate = CLLocationCoordinate2DMake(-41.292650, 174.778768)
68+
// feature3.attributes = ["id": "star"]
69+
//
70+
MGLShapeCollectionFeature *shapeCollection = [MGLShapeCollectionFeature shapeCollectionWithShapes:@[feature1, feature2, feature3]];
71+
// let shapeCollection = MGLShapeCollectionFeature(shapes: [feature1, feature2, feature3])
72+
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"symbol-layer-z-order-example" shape:shapeCollection options:nil];
73+
// let source = MGLShapeSource(identifier: "symbol-layer-z-order-example", shape: shapeCollection, options: nil)
74+
[style addSource:source];
75+
// style.addSource(source)
76+
MGLSymbolStyleLayer *layer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"points-style" source:source];
77+
// let layer = MGLSymbolStyleLayer(identifier: "points-style", source: source)
78+
layer.sourceLayerIdentifier = @"symbol-layer-z-order-example";
79+
// layer.sourceLayerIdentifier = "symbol-layer-z-order-example"
80+
//
81+
// // Create a stops dictionary with keys that are possible values for 'id', paired with icon images that will represent those features.
82+
NSDictionary *icons = @{@"squircle": @"squircle", @"oval": @"oval", @"star": @"star"};
83+
// let icons = ["squircle": "squircle", "oval": "oval", "star": "star"]
84+
// // Use the stops dictionary to assign an icon based on the "POITYPE" for each feature.
85+
layer.iconImageName = [NSExpression expressionWithFormat:@"FUNCTION(%@, 'valueForKeyPath:', id)", icons];
86+
// layer.iconImageName = NSExpression(format: "FUNCTION(%@, 'valueForKeyPath:', id)", icons)
87+
88+
layer.iconAllowsOverlap = [NSExpression expressionForConstantValue:@(YES)];
89+
// layer.iconAllowsOverlap = NSExpression(forConstantValue: true)
90+
layer.symbolZOrder = [NSExpression expressionForConstantValue:@"source"];
91+
// layer.symbolZOrder = NSExpression(forConstantValue: "source")
92+
[style addLayer:layer];
93+
// style.addLayer(layer)
94+
//
95+
// self.symbolLayer = layer
96+
97+
UISegmentedControl *styleToggle =[[UISegmentedControl alloc] initWithItems:@[@"viewport-y", @"source"]];
98+
styleToggle.translatesAutoresizingMaskIntoConstraints = NO;
99+
styleToggle.tintColor = [UIColor colorWithRed:0.976 green:0.843 blue:0.831 alpha:1];
100+
styleToggle.backgroundColor = [UIColor colorWithRed:0.973 green:0.329 blue:0.294 alpha:1];
101+
styleToggle.layer.cornerRadius = 4;
102+
styleToggle.clipsToBounds = YES;
103+
styleToggle.selectedSegmentIndex = 1;
104+
[self.view insertSubview:styleToggle aboveSubview:self.mapView];
105+
[styleToggle addTarget:self action:@selector(changeStyle:) forControlEvents:UIControlEventValueChanged];
106+
// // Create a UISegmentedControl to toggle between map styles
107+
// let styleToggle = UISegmentedControl(items: ["viewport-y", "source"])
108+
// styleToggle.translatesAutoresizingMaskIntoConstraints = false
109+
// styleToggle.tintColor = UIColor(red: 0.976, green: 0.843, blue: 0.831, alpha: 1)
110+
// styleToggle.backgroundColor = UIColor(red: 0.973, green: 0.329, blue: 0.294, alpha: 1)
111+
// styleToggle.layer.cornerRadius = 4
112+
// styleToggle.clipsToBounds = true
113+
// styleToggle.selectedSegmentIndex = 1
114+
// view.insertSubview(styleToggle, aboveSubview: mapView)
115+
// styleToggle.addTarget(self, action: #selector(toggleLayer(sender:)), for: .valueChanged)
116+
//
62117

63-
// Style the circle layer color based on the above stops dictionary.
64-
layer.circleColor = [NSExpression expressionWithFormat:@"mgl_step:from:stops:(AGE, %@, %@)", [UIColor colorWithRed:1.0 green:0.72 blue:0.85 alpha:1.0], stops];
118+
// Configure autolayout constraints for the UISegmentedControl to align
119+
// at the bottom of the map view and above the Mapbox logo and attribution
120+
NSMutableArray *constraints = [NSMutableArray array];
65121

66-
layer.circleRadius = [NSExpression expressionForConstantValue:@3];
122+
[constraints addObject:[NSLayoutConstraint constraintWithItem:styleToggle attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.mapView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:1.0]];
123+
[constraints addObject:[NSLayoutConstraint constraintWithItem:styleToggle attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.mapView.logoView attribute:NSLayoutAttributeTop multiplier:1 constant:-20]];
67124

68-
[self.mapView.style addLayer:layer];
125+
[self.view addConstraints:constraints];
126+
// // Configure autolayout constraints for the UISegmentedControl to align
127+
// // at the bottom of the map view and above the Mapbox logo and attribution
128+
// NSLayoutConstraint.activate([NSLayoutConstraint(item: styleToggle, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: mapView, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0.0)])
129+
// NSLayoutConstraint.activate([NSLayoutConstraint(item: styleToggle, attribute: .bottom, relatedBy: .equal, toItem: mapView.logoView, attribute: .top, multiplier: 1, constant: -20)])
130+
}
131+
132+
// Change the map style based on the selected index of the UISegmentedControl
133+
- (void)changeStyle:(UISegmentedControl *)sender {
134+
switch(sender.selectedSegmentIndex){
135+
case 0:
136+
self.mapView.styleURL = [MGLStyle satelliteStyleURL];
137+
break;
138+
case 1:
139+
self.mapView.styleURL = [MGLStyle streetsStyleURL];
140+
break;
141+
case 2:
142+
self.mapView.styleURL = [MGLStyle lightStyleURL];
143+
break;
144+
}
145+
}
146+
// Change the map style based on the selected index of the UISegmentedControl
147+
//@objc func toggleLayer(sender: UISegmentedControl) {
148+
// switch sender.selectedSegmentIndex {
149+
// case 0:
150+
// useSource()
151+
// case 1:
152+
// useViewportY()
153+
// default:
154+
// useSource()
155+
// }
156+
//}
157+
//
158+
- (void)useSource:(UISegmentedControl *)sender {
159+
}
160+
//func useSource() {
161+
// self.symbolLayer?.symbolZOrder = NSExpression(forConstantValue: "source")
162+
//}
163+
//
164+
- (void)useViewportY:(UISegmentedControl *)sender {
69165
}
166+
//func useViewportY() {
167+
// self.symbolLayer?.symbolZOrder = NSExpression(forConstantValue: "viewport-y")
168+
//}
70169

71170
@end

0 commit comments

Comments
 (0)