Skip to content

Commit 27ce7a8

Browse files
RD-1009: Fix MTMapViewContainer elements insertion order (#75)
Fixed MTMapView style insertion order and revoked invalid key
1 parent 1f0c8a5 commit 27ce7a8

File tree

3 files changed

+56
-38
lines changed

3 files changed

+56
-38
lines changed

Examples/MapTilerMobileDemo/MainViewController.swift

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,36 @@ class MainViewController: UIViewController {
1616
static let defaultZoomLevel = 14.0
1717
}
1818

19-
@IBOutlet weak var mapView: MTMapView! {
19+
@IBOutlet private weak var mapView: MTMapView! {
2020
didSet {
2121
mapView.delegate = self
2222
}
2323
}
2424

25-
@IBOutlet weak var mapControlView: MapControlView! {
25+
@IBOutlet private weak var mapControlView: MapControlView! {
2626
didSet {
2727
mapControlView.delegate = self
2828
}
2929
}
3030

31-
@IBOutlet weak var mapZoomControlView: MapZoomControlView! {
31+
@IBOutlet private weak var mapZoomControlView: MapZoomControlView! {
3232
didSet {
3333
mapZoomControlView.delegate = self
3434
}
3535
}
3636

37-
@IBOutlet weak var mapProjectionControlView: MapProjectionControlView! {
37+
@IBOutlet private weak var mapProjectionControlView: MapProjectionControlView! {
3838
didSet {
3939
mapProjectionControlView.delegate = self
4040
}
4141
}
4242

43-
@IBOutlet weak var jumpContainerView: UIView!
44-
@IBOutlet weak var benchmarkButton: UIButton!
45-
@IBOutlet weak var loadingActivityIndicator: UIActivityIndicatorView!
46-
@IBOutlet weak var layerViewLeadingConstraint: NSLayoutConstraint!
43+
@IBOutlet private weak var jumpContainerView: UIView!
44+
@IBOutlet private weak var benchmarkButton: UIButton!
45+
@IBOutlet private weak var loadingActivityIndicator: UIActivityIndicatorView!
46+
@IBOutlet private weak var layerViewLeadingConstraint: NSLayoutConstraint!
4747

48-
@IBOutlet weak var layerView: LayerView! {
48+
@IBOutlet private weak var layerView: LayerView! {
4949
didSet {
5050
layerView.delegate = self
5151
}
@@ -81,7 +81,7 @@ class MainViewController: UIViewController {
8181

8282
override func viewWillDisappear(_ animated: Bool) {
8383
super.viewWillDisappear(animated)
84-
84+
8585
AppDelegate.AppUtility.lockOrientation(.all)
8686
}
8787

@@ -132,7 +132,7 @@ class MainViewController: UIViewController {
132132

133133
private func observeJumpCoordinates() {
134134
dataModel.$jumpCoordinates
135-
.compactMap{ $0 }
135+
.compactMap { $0 }
136136
.sink { [weak self] coordinates in
137137
self?.jumpTo(coordinates)
138138
}
@@ -146,17 +146,21 @@ class MainViewController: UIViewController {
146146
}
147147

148148
private func addSources() {
149-
if let contoursURL = URL(string: "https://api.maptiler.com/tiles/contours-v2/{z}/{x}/{y}.pbf?key=F88dOilbnFebWsh4o9oP") {
150-
Task {
151-
let contoursSource = MTVectorTileSource(identifier: "contourssource", tiles: [contoursURL])
152-
try await mapView.style?.addSource(contoursSource)
153-
}
154-
}
155-
156-
if let openMapURL = URL(string: "https://api.maptiler.com/tiles/v3-openmaptiles/{z}/{x}/{y}.pbf?key=F88dOilbnFebWsh4o9oP") {
157-
Task {
158-
let aerowaySource = MTVectorTileSource(identifier: "openmapsource", tiles: [openMapURL])
159-
try await mapView.style?.addSource(aerowaySource)
149+
Task {
150+
if let mapTilerAPIKey = await MTConfig.shared.getAPIKey() {
151+
if let openMapURL = URL(
152+
string: "https://api.maptiler.com/tiles/v3-openmaptiles/tiles.json?key=\(mapTilerAPIKey)"
153+
) {
154+
let aerowaySource = MTVectorTileSource(identifier: "openmapsource", url: openMapURL)
155+
try await mapView.style?.addSource(aerowaySource)
156+
}
157+
158+
if let contoursURL = URL(
159+
string: "https://api.maptiler.com/tiles/contours-v2/tiles.json?key=\(mapTilerAPIKey)"
160+
) {
161+
let contoursSource = MTVectorTileSource(identifier: "contourssource", url: contoursURL)
162+
try await mapView.style?.addSource(contoursSource)
163+
}
160164
}
161165
}
162166
}
@@ -222,7 +226,7 @@ extension MainViewController: MapProjectionControlViewDelegate {
222226
globeEnabled = !globeEnabled
223227
}
224228
}
225-
229+
226230
func mapProjectionControlViewDidTapEnableTerrain(_ mapProjectionControlView: MapProjectionControlView) {
227231
Task {
228232
if terrainEnabled {
@@ -249,12 +253,12 @@ extension MainViewController: MTMapViewDelegate {
249253

250254
loadingActivityIndicator.stopAnimating()
251255

252-
// *** Uncomment for benchmark or use long press on jump view ***
256+
// *** Uncomment for benchmark or use long press on jump view ***
253257
// Task {
254258
// benchmarkButton.isHidden = false
255259
// await MTConfig.shared.setLogLevel(.none)
256260
// }
257-
// *** ***
261+
// *** ***
258262
}
259263

260264
func mapView(_ mapView: MTMapView, didTriggerEvent event: MTEvent, with data: MTData?) {

Sources/MapTilerSDK/Map/MTMapViewContainer.swift

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,32 @@ package struct MTMapViewRepresentable: UIViewRepresentable {
6969
mapView.delegate = coordinator
7070

7171
mapView.didInitialize = {
72-
mapView.style?.setStyle(referenceStyle, styleVariant: styleVariant) { _ in
73-
let sources = content.filter { $0 is MTSource }
74-
let layers = content.filter { $0 is MTLayer }
72+
let sources = content.filter { $0 is MTSource }
7573

76-
for item in sources {
77-
if let item = item as? MTSource {
78-
mapView.style?.addSource(item)
79-
}
74+
for item in sources {
75+
if let item = item as? MTSource {
76+
mapView.style?.addSource(item)
8077
}
78+
}
79+
80+
let layers = content.filter { $0 is MTLayer }
81+
82+
for item in layers {
83+
if let item = item as? MTLayer {
84+
mapView.style?.addLayer(item) { result in
85+
switch result {
86+
case .success:
87+
MTLogger.log("Added layer with id \(item.identifier)", type: .info)
88+
case .failure:
89+
mapView.style?.addLayer(item)
90+
}
8191

82-
for item in layers {
83-
if let item = item as? MTLayer {
84-
mapView.style?.addLayer(item)
8592
}
8693
}
94+
}
8795

88-
for item in content where !((item is MTSource) || (item is MTLayer)) {
89-
item.addToMap(mapView)
90-
}
96+
for item in content where !((item is MTSource) || (item is MTLayer)) {
97+
item.addToMap(mapView)
9198
}
9299
}
93100

Sources/MapTilerSDK/Map/Style/MTStyle.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public class MTStyle {
5151
}
5252

5353
/// Updates the map's style object with a new value.
54+
///
55+
/// Setting the style resets custom sources and layers, so make sure to wait for style to load
56+
/// (``MTEvent/styleDidUpdate``) before adding them.
5457
/// - Parameters:
5558
/// - referenceStyle: Desired reference map style.
5659
/// - styleVariant: Optional variant of the reference style.
@@ -233,6 +236,7 @@ extension MTStyle {
233236
queue.append(layerTask)
234237
}
235238
case .failure(let error):
239+
mapLayers[layer.identifier] = nil
236240
completionHandler?(.failure(error))
237241
}
238242
}
@@ -288,6 +292,9 @@ extension MTStyle {
288292
// Concurrency
289293
extension MTStyle {
290294
/// Updates the map's style object with a new value.
295+
///
296+
/// Setting the style resets custom sources and layers, so make sure to wait for style to load
297+
/// (``MTEvent/styleDidUpdate``) before adding them.
291298
/// - Parameters:
292299
/// - referenceStyle: Desired reference map style.
293300
/// - styleVariant: Optional variant of the reference style.

0 commit comments

Comments
 (0)