Skip to content

Commit

Permalink
Update CustomRasterSource example (#2205)
Browse files Browse the repository at this point in the history
  • Loading branch information
maios committed Jul 15, 2024
1 parent 7cc9012 commit 22ad304
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
56 changes: 29 additions & 27 deletions Apps/Examples/Examples/All Examples/CustomRasterSourceExample.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import UIKit
import os
@_spi(Experimental) import MapboxMaps

final class CustomRasterSourceExample: UIViewController, ExampleProtocol {
private var mapView: MapView!
private var cancelables: Set<AnyCancelable> = []
private var timer: Timer?
private var requiredTile: CanonicalTileID?
private var requiredTiles: [CanonicalTileID] = []

private enum ID {
static let customRasterSource = "custom-raster-source"
static let rasterLayer = "customRaster"
}

deinit {
timer?.invalidate()
}

override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -34,12 +30,23 @@ final class CustomRasterSourceExample: UIViewController, ExampleProtocol {
private func setupExample() {
let rasterSourceOptions = CustomRasterSourceOptions(
tileStatusChangedFunction: { [weak self] tileID, status in
os_log(.info, "Tile status changed: tileId={%@}, status=%@", tileID.log, status.log)
guard let self else { return }
if status == CustomRasterSourceTileStatus.required {
requiredTile = tileID

switch status {
case .required:
if !self.requiredTiles.contains(where: { $0 == tileID }) {
self.requiredTiles.append(tileID)
}
self.refreshTiles()
case .notNeeded, .optional:
if let index = self.requiredTiles.firstIndex(of: tileID) {
self.requiredTiles.remove(at: index)
}
try! self.mapView.mapboxMap.setCustomRasterSourceTileData(
forSourceId: ID.customRasterSource,
tiles: [CustomRasterSourceTileData(tileId: tileID, image: rasterImages[self.currentImageIndex])])
tiles: [CustomRasterSourceTileData(tileId: tileID, image: nil)])
@unknown default: break
}
},
minZoom: 0,
Expand Down Expand Up @@ -71,12 +78,19 @@ final class CustomRasterSourceExample: UIViewController, ExampleProtocol {
}
)
try mapView.mapboxMap.addLayer(rasterLayer)
scheduleNextRasterImage()
refreshTiles()
} catch {
print("[Example/CustomRasterSourceExample] Error:\(error)")
}
}

private func refreshTiles() {
let rasterImage = nextImage()
let tiles = requiredTiles
.map { CustomRasterSourceTileData(tileId: $0, image: rasterImage) }
try! mapView.mapboxMap.setCustomRasterSourceTileData(forSourceId: ID.customRasterSource, tiles: tiles)
}

// MARK: Raster Images

private var currentImageIndex = 0
Expand All @@ -87,23 +101,11 @@ final class CustomRasterSourceExample: UIViewController, ExampleProtocol {
UIImage(named: "RasterSource/wind_3")!,
]

private func scheduleNextRasterImage() {
guard timer == nil else {
timer?.invalidate()
return timer = nil
}

timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] _ in
guard let self else { return }

var currentImageIndex = self.currentImageIndex + 1
if currentImageIndex >= self.rasterImages.endIndex {
currentImageIndex = 0
}
self.currentImageIndex = currentImageIndex
try! self.mapView.mapboxMap.setCustomRasterSourceTileData(
forSourceId: ID.customRasterSource,
tiles: [CustomRasterSourceTileData(tileId: requiredTile!, image: rasterImages[self.currentImageIndex])])
private func nextImage() -> UIImage {
var currentImageIndex = self.currentImageIndex + 1
if currentImageIndex >= self.rasterImages.endIndex {
currentImageIndex = 0
}
return rasterImages[currentImageIndex]
}
}
11 changes: 11 additions & 0 deletions Apps/Examples/Examples/All Examples/Lab/MapEventsExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ extension CanonicalTileID {
}
}

extension CustomRasterSourceTileStatus {
var log: String {
switch self {
case .required: return "required"
case .optional: return "optional"
case .notNeeded: return "notNeeded"
default: return "unknown"
}
}
}

extension StyleDataLoadedType: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
Expand Down

0 comments on commit 22ad304

Please sign in to comment.