@@ -14,6 +14,7 @@ import Mapbox
1414class SymbolLayerZOrderExample_Swift : UIViewController , MGLMapViewDelegate {
1515
1616var mapView : MGLMapView !
17+ var symbolLayer : MGLSymbolStyleLayer ?
1718
1819override 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}
0 commit comments