1414@interface SymbolLayerZOrderExample () <MGLMapViewDelegate>
1515
1616@property (nonatomic ) MGLMapView *mapView;
17+ @property (nonatomic ) MGLSymbolStyleLayer *layer;
1718
1819@end
1920
@@ -44,76 +45,42 @@ - (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {
4445 [style setImage: ([UIImage imageNamed: @" oval" ]) forName: @" oval" ];
4546 [style setImage: ([UIImage imageNamed: @" squircle" ]) forName: @" squircle" ];
4647 [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- //
48+
5149 MGLPointFeature *feature1 = [MGLPointFeature alloc ];
5250 feature1.coordinate = CLLocationCoordinate2DMake (-41.292650 , 174.778768 );
5351 feature1.attributes = @{@" id" : @" squircle" };
54- // let feature1 = MGLPointFeature()
55- // feature1.coordinate = CLLocationCoordinate2DMake(-41.292650, 174.778768)
56- // feature1.attributes = ["id": "squircle"]
52+
5753 MGLPointFeature *feature2 = [MGLPointFeature alloc ];
5854 feature2.coordinate = CLLocationCoordinate2DMake (-41.292650 , 174.778768 );
5955 feature2.attributes = @{@" id" : @" oval" };
60- // let feature2 = MGLPointFeature()
61- // feature2.coordinate = CLLocationCoordinate2DMake(-41.292650, 174.778768)
62- // feature2.attributes = ["id": "oval"]
56+
6357 MGLPointFeature *feature3 = [MGLPointFeature alloc ];
6458 feature3.coordinate = CLLocationCoordinate2DMake (-41.292650 , 174.778768 );
6559 feature3.attributes = @{@" id" : @" star" };
66- // let feature3 = MGLPointFeature()
67- // feature3.coordinate = CLLocationCoordinate2DMake(-41.292650, 174.778768)
68- // feature3.attributes = ["id": "star"]
69- //
60+
7061 MGLShapeCollectionFeature *shapeCollection = [MGLShapeCollectionFeature shapeCollectionWithShapes: @[feature1, feature2, feature3]];
71- // let shapeCollection = MGLShapeCollectionFeature(shapes: [feature1, feature2, feature3])
7262 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)
63+
7464 [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.
65+ self.layer = [[MGLSymbolStyleLayer alloc ] initWithIdentifier: @" points-style" source: source];
66+ self.layer .sourceLayerIdentifier = @" symbol-layer-z-order-example" ;
67+ // Create a stops dictionary with keys that are possible values for 'id', paired with icon images that will represent those features.
8268 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
69+ // Use the stops dictionary to assign an icon based on the "POITYPE" for each feature.
70+ self.layer .iconImageName = [NSExpression expressionWithFormat: @" FUNCTION(%@ , 'valueForKeyPath:', id)" , icons];
71+ self.layer .iconAllowsOverlap = [NSExpression expressionForConstantValue: @(YES )];
72+ self.layer .symbolZOrder = [NSExpression expressionForConstantValue: @" source" ];
73+ [style addLayer: self .layer];
9674
9775 UISegmentedControl *styleToggle =[[UISegmentedControl alloc ] initWithItems: @[@" viewport-y" , @" source" ]];
9876 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 ];
77+ styleToggle.backgroundColor = [UIColor colorWithRed: 0.83 green: 0.84 blue: 0.95 alpha: 1.0 ];
78+ styleToggle.tintColor = [UIColor colorWithRed: 0.26 green: 0.39 blue: 0.98 alpha: 1.0 ];
10179 styleToggle.layer .cornerRadius = 4 ;
10280 styleToggle.clipsToBounds = YES ;
10381 styleToggle.selectedSegmentIndex = 1 ;
10482 [self .view insertSubview: styleToggle aboveSubview: self .mapView];
10583 [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- //
11784
11885 // Configure autolayout constraints for the UISegmentedControl to align
11986 // at the bottom of the map view and above the Mapbox logo and attribution
@@ -123,48 +90,17 @@ - (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {
12390 [constraints addObject: [NSLayoutConstraint constraintWithItem: styleToggle attribute: NSLayoutAttributeBottom relatedBy: NSLayoutRelationEqual toItem: self .mapView.logoView attribute: NSLayoutAttributeTop multiplier: 1 constant: -20 ]];
12491
12592 [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)])
13093}
13194
13295// Change the map style based on the selected index of the UISegmentedControl
13396- (void )changeStyle : (UISegmentedControl *)sender {
13497 switch (sender.selectedSegmentIndex ){
13598 case 0 :
136- self.mapView . styleURL = [MGLStyle satelliteStyleURL ];
99+ self.layer . symbolZOrder = [NSExpression expressionForConstantValue: @" viewport-y " ];
137100 break ;
138101 case 1 :
139- self.mapView .styleURL = [MGLStyle streetsStyleURL ];
140- break ;
141- case 2 :
142- self.mapView .styleURL = [MGLStyle lightStyleURL ];
102+ self.layer .symbolZOrder = [NSExpression expressionForConstantValue: @" source" ];;
143103 break ;
144104 }
145105}
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 {
165- }
166- // func useViewportY() {
167- // self.symbolLayer?.symbolZOrder = NSExpression(forConstantValue: "viewport-y")
168- // }
169-
170106@end
0 commit comments