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

Commit 6d89cb1

Browse files
author
Sam Fader
committed
add toggle to switch between z order type
1 parent 1f3345c commit 6d89cb1

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Examples/Swift/SymbolLayerZOrderExample.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Mapbox
1414
class SymbolLayerZOrderExample_Swift: UIViewController, MGLMapViewDelegate {
1515

1616
var mapView: MGLMapView!
17+
var symbolLayer: MGLSymbolStyleLayer?
1718

1819
override func viewDidLoad() {
1920
super.viewDidLoad()
@@ -58,8 +59,44 @@ func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
5859
layer.iconImageName = NSExpression(format: "FUNCTION(%@, 'valueForKeyPath:', id)", icons)
5960

6061
layer.iconAllowsOverlap = NSExpression(forConstantValue: true)
61-
layer.iconIgnoresPlacement = NSExpression(forConstantValue: true)
6262
layer.symbolZOrder = NSExpression(forConstantValue: "source")
6363
style.addLayer(layer)
64+
65+
self.symbolLayer = layer
66+
67+
// Create a UISegmentedControl to toggle between map styles
68+
let styleToggle = UISegmentedControl(items: ["viewport-y", "source"])
69+
styleToggle.translatesAutoresizingMaskIntoConstraints = false
70+
styleToggle.tintColor = UIColor(red: 0.976, green: 0.843, blue: 0.831, alpha: 1)
71+
styleToggle.backgroundColor = UIColor(red: 0.973, green: 0.329, blue: 0.294, alpha: 1)
72+
styleToggle.layer.cornerRadius = 4
73+
styleToggle.clipsToBounds = true
74+
styleToggle.selectedSegmentIndex = 1
75+
view.insertSubview(styleToggle, aboveSubview: mapView)
76+
styleToggle.addTarget(self, action: #selector(toggleLayer(sender:)), for: .valueChanged)
77+
78+
// Configure autolayout constraints for the UISegmentedControl to align
79+
// at the bottom of the map view and above the Mapbox logo and attribution
80+
NSLayoutConstraint.activate([NSLayoutConstraint(item: styleToggle, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: mapView, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0.0)])
81+
NSLayoutConstraint.activate([NSLayoutConstraint(item: styleToggle, attribute: .bottom, relatedBy: .equal, toItem: mapView.logoView, attribute: .top, multiplier: 1, constant: -20)])
82+
}
83+
// Change the map style based on the selected index of the UISegmentedControl
84+
@objc func toggleLayer(sender: UISegmentedControl) {
85+
switch sender.selectedSegmentIndex {
86+
case 0:
87+
useSource()
88+
case 1:
89+
useViewportY()
90+
default:
91+
useSource()
92+
}
93+
}
94+
95+
func useSource() {
96+
self.symbolLayer?.symbolZOrder = NSExpression(forConstantValue: "source")
97+
}
98+
99+
func useViewportY() {
100+
self.symbolLayer?.symbolZOrder = NSExpression(forConstantValue: "viewport-y")
64101
}
65102
}

Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ SPEC CHECKSUMS:
5050

5151
PODFILE CHECKSUM: d7f13c38c14f3595d1289272a6b68d0d6486d1f8
5252

53-
COCOAPODS: 1.6.0.beta.1
53+
COCOAPODS: 1.5.3

0 commit comments

Comments
 (0)