Custom Runtime Style Layer

This example shows how to customize runtime map style layer.

  • Custom building-3d layer style with zoom level

  • Add building layer in Runtime style

  • Custom water layer style with zoom level

  • Custom roads layer with zoom level

docs-image

For all code examples, refer to Maps Code Examples

StyleMapLayerViewController view source

1
//
2
// StyleMapLayerViewController.swift
3
// maps-ios-demo
4
//
5
import Foundation
6
import UIKit
7
import Nbmap
8
enum ActionType{
9
case StyleBuildingExtrusions
10
case StyleWaterWithFunction
11
case StyleRoadsWithFunction
12
case AddBuildingExtrusions
13
}
14
class StyleMapLayerViewController: UIViewController {
15
16
var nbMapView: NGLMapView! {
17
didSet {
18
oldValue?.removeFromSuperview()
19
if let mapView = nbMapView {
20
view.insertSubview(mapView, at: 0)
21
mapView.delegate = self
22
}
23
}
24
}
25
var button: UIButton!
26
27
let typeList = [
28
ActionType.StyleBuildingExtrusions,
29
ActionType.AddBuildingExtrusions,
30
ActionType.StyleWaterWithFunction,
31
ActionType.StyleRoadsWithFunction,
32
]
33
34
35
var points : Array = [CLLocationCoordinate2D]()
36
37
override func viewDidLoad() {
38
super.viewDidLoad()
39
nbMapView = NGLMapView(frame:self.view.bounds)
40
button = UIButton(type: .system)
41
button.setTitle("Settings", for: .normal)
42
button.addTarget(self, action: #selector(showSetings), for: .touchUpInside)
43
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
44
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
45
}
46
47
@objc func showSetings() {
48
let tableViewController = UITableViewController(style: .plain)
49
tableViewController.tableView.delegate = self
50
tableViewController.tableView.dataSource = self
51
tableViewController.title = "Camera Settings"
52
self.present(tableViewController, animated: true)
53
}
54
55
func performeSettings(type: ActionType) {
56
switch type {
57
case ActionType.StyleBuildingExtrusions :
58
styleBuildingExtrusions()
59
break
60
case ActionType.StyleWaterWithFunction :
61
styleWaterWithFunction()
62
break
63
case .StyleRoadsWithFunction:
64
styleRoadsWithFunction()
65
break
66
case .AddBuildingExtrusions:
67
addBuildingExtrusions()
68
break
69
}
70
71
}
72
73
func styleBuildingExtrusions() {
74
let layer = self.nbMapView.style?.layer(withIdentifier: "building-3d") as? NGLFillExtrusionStyleLayer
75
// Set the fill color to that of the existing building footprint layer, if it exists.
76
layer?.fillExtrusionColor = NSExpression(forConstantValue: UIColor.purple)
77
layer?.fillExtrusionOpacity = NSExpression(forConstantValue: 0.55)
78
79
80
nbMapView.setCenter(CLLocationCoordinate2DMake(12.98780156, 77.59956748), zoomLevel: 18, animated: true)
81
}
82
83
func addBuildingExtrusions() {
84
let source = self.nbMapView.style?.source(withIdentifier: "openmaptiles")
85
if let source = source, self.nbMapView.style?.layer(withIdentifier: "extrudedBuildings") == nil {
86
let layer = NGLFillExtrusionStyleLayer(identifier: "extrudedBuildings", source: source)
87
layer.sourceLayerIdentifier = "building"
88
layer.fillExtrusionBase = NSExpression(forConstantValue: 20)
89
layer.fillExtrusionHeight = NSExpression(forConstantValue: 50)
90
// Set the fill color to that of the existing building footprint layer, if it exists.
91
layer.fillExtrusionColor = NSExpression(forConstantValue: UIColor.red)
92
layer.fillExtrusionOpacity = NSExpression(forConstantValue: 0.75)
93
self.nbMapView.style?.addLayer(layer)
94
95
96
}
97
let camera = NGLMapCamera(lookingAtCenter: CLLocationCoordinate2DMake(12.98780156, 77.59956748),
98
acrossDistance:600,
99
pitch:50,
100
heading:0)
101
nbMapView.fly(to: camera, withDuration: 2)
102
}
103
104
105
func styleWaterWithFunction() {
106
107
let waterLayer = self.nbMapView.style?.layer(withIdentifier: "water") as? NGLFillStyleLayer
108
let waterColorStops: [NSNumber: UIColor] = [
109
6.0: UIColor.yellow,
110
8.0: UIColor.blue,
111
10.0: UIColor.red,
112
12.0: UIColor.green,
113
14.0: UIColor.blue
114
]
115
let fillColorExpression = NSExpression(format: "ngl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", waterColorStops)
116
117
waterLayer?.fillColor = fillColorExpression
118
119
let fillAntialiasedStops: [NSNumber: Any] = [
120
11: true,
121
12: false,
122
13: true,
123
14: false,
124
15: true
125
]
126
waterLayer?.fillAntialiased = NSExpression(forNGLStepping: .zoomLevelVariable, from: NSExpression(forConstantValue: false), stops: NSExpression(forConstantValue: fillAntialiasedStops))
127
128
}
129
130
func styleRoadsWithFunction() {
131
132
let roadLayer = self.nbMapView.style?.layer(withIdentifier: "road_primary") as? NGLLineStyleLayer
133
roadLayer?.lineColor = NSExpression(forConstantValue: UIColor.black)
134
135
let lineWidthStops: [NSNumber: NSNumber] = [
136
5: 5,
137
10: 10,
138
15: 15
139
]
140
let lineWidthExpression = NSExpression(format: "ngl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", lineWidthStops)
141
roadLayer?.lineWidth = lineWidthExpression
142
roadLayer?.lineGapWidth = lineWidthExpression
143
144
let roadLineColorStops: [NSNumber: UIColor] = [
145
5: UIColor.purple,
146
10: UIColor.purple,
147
13: UIColor.yellow,
148
16: UIColor.cyan
149
]
150
roadLayer?.lineColor = NSExpression(format: "ngl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", roadLineColorStops)
151
152
roadLayer?.isVisible = true
153
154
}
155
}
156
extension StyleMapLayerViewController: NGLMapViewDelegate {
157
func mapView(_ mapView: NGLMapView, didFinishLoading style: NGLStyle){
158
159
nbMapView.setCenter(CLLocationCoordinate2DMake(12.97780156, 77.59656748), zoomLevel: 10, animated: true)
160
161
}
162
}
163
extension StyleMapLayerViewController: UITableViewDelegate, UITableViewDataSource {
164
165
func settingsTitlesForRaw(index: Int) -> String {
166
let type = typeList[index]
167
switch type {
168
case ActionType.StyleBuildingExtrusions :
169
return "Style Building Extrusions"
170
case ActionType.StyleWaterWithFunction :
171
return "Style Water with function"
172
case ActionType.StyleRoadsWithFunction :
173
return "Style Roads with function"
174
case ActionType.AddBuildingExtrusions :
175
return "Add Building Extrusions"
176
}
177
}
178
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
179
return typeList.count
180
}
181
182
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
183
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
184
cell.textLabel?.text = settingsTitlesForRaw(index: indexPath.row)
185
return cell
186
}
187
188
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
189
tableView.isHidden = true
190
let type = typeList[indexPath.row]
191
dismissSettings(type: type)
192
}
193
194
func dismissSettings(type: ActionType) {
195
dismiss(animated: true)
196
performeSettings(type: type)
197
}
198
}
  • Initializing mapView: The nbMapView property is initialized in the viewDidLoad() method. It creates an instance of NGLMapView and sets it as the main view of the view controller.

  • styleBuildingExtrusions() method: This method is called when the "Style Building Extrusions" option is selected. It retrieves the “building-3d” layer from the map style and sets the fill extrusion color and opacity. It also sets the map center and zoom level.

  • addBuildingExtrusions() method: This method is called when the "Add Building Extrusions" option is selected. It adds a new NGLFillExtrusionStyleLayer to the map style with a source identifier of "openmaptiles". It sets the fill extrusion base and height, fill extrusion color, and opacity. It also sets a specific map camera position for animation.

  • styleWaterWithFunction() method: This method is called when the "Style Water with function" option is selected. It retrieves the “water” layer from the map style and sets the fill color using an interpolation function based on the zoom level. It also sets the fill antialiased property using a stepped interpolation.

  • styleRoadsWithFunction() method: This method is called when the "Style Roads with function" option is selected. It retrieves the “road_primary” layer from the map style and sets the line color using an interpolation function based on the zoom level. It also sets the line width and line gap width using an interpolation function. Additionally, it sets the layer's visibility to true.

© 2024 NextBillion.ai all rights reserved.