Skip to content

Commit f8142b6

Browse files
committed
adding defaultTile functionality
1 parent 9ccaa99 commit f8142b6

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

Sources/MapViewModule/MapViewModule.swift

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public struct MapView: UIViewRepresentable {
109109
let overlay = CustomMapOverlaySource(
110110
parent: self,
111111
mapName: customMapOverlay.mapName,
112-
tileType: customMapOverlay.tileType
112+
tileType: customMapOverlay.tileType,
113+
defaultTile: customMapOverlay.defaultTile
113114
)
114115

115116
if let minZ = customMapOverlay.minimumZoomLevel {
@@ -296,51 +297,75 @@ public struct MapView: UIViewRepresentable {
296297
}
297298

298299
}
300+
301+
/// is supposed to be located in the folder with the map name
302+
public struct DefaultTile: Hashable {
303+
let tileName: String
304+
let tileType: String
305+
}
299306

300307
public struct CustomMapOverlay: Equatable, Hashable {
301308
let mapName: String
302309
let tileType: String
303310
var canReplaceMapContent: Bool
304311
var minimumZoomLevel: Int?
305312
var maximumZoomLevel: Int?
313+
let defaultTile: DefaultTile?
314+
306315
public init(
307316
mapName: String,
308317
tileType: String,
309318
canReplaceMapContent: Bool = true, // false for transparent tiles
310319
minimumZoomLevel: Int? = nil,
311-
maximumZoomLevel: Int? = nil
320+
maximumZoomLevel: Int? = nil,
321+
defaultTile: DefaultTile? = nil
312322
) {
313323
self.mapName = mapName
314324
self.tileType = tileType
315325
self.canReplaceMapContent = canReplaceMapContent
316326
self.minimumZoomLevel = minimumZoomLevel
317327
self.maximumZoomLevel = maximumZoomLevel
328+
self.defaultTile = defaultTile
318329
}
319330
}
320-
331+
321332
public class CustomMapOverlaySource: MKTileOverlay {
322-
333+
323334
// requires folder: tiles/{mapName}/z/y/y,{tileType}
324-
335+
325336
private var parent: MapView
326337
private let mapName: String
327338
private let tileType: String
328-
329-
public init(parent: MapView, mapName: String, tileType: String) {
339+
private let defaultTile: DefaultTile?
340+
341+
public init(
342+
parent: MapView,
343+
mapName: String,
344+
tileType: String,
345+
defaultTile: DefaultTile?
346+
) {
330347
self.parent = parent
331348
self.mapName = mapName
332349
self.tileType = tileType
350+
self.defaultTile = defaultTile
333351
super.init(urlTemplate: "")
334352
}
335-
353+
336354
public override func url(forTilePath path: MKTileOverlayPath) -> URL {
337355
if let tileUrl = Bundle.main.url(
338356
forResource: "\(path.y)",
339-
withExtension: "\(self.tileType)",
357+
withExtension: self.tileType,
340358
subdirectory: "tiles/\(self.mapName)/\(path.z)/\(path.x)",
341359
localization: nil
342360
) {
343361
return tileUrl
362+
} else if let defaultTile = self.defaultTile, let defaultTileUrl = Bundle.main.url(
363+
forResource: defaultTile.tileName,
364+
withExtension: defaultTile.tileType,
365+
subdirectory: "tiles/\(self.mapName)",
366+
localization: nil
367+
) {
368+
return defaultTileUrl
344369
} else {
345370
return URL(string: "https://tile.openstreetmap.org/\(path.z)/\(path.x)/\(path.y).png")!
346371
// Bundle.main.url(forResource: "surrounding", withExtension: "png", subdirectory: "tiles")!
@@ -358,7 +383,7 @@ public struct MapView: UIViewRepresentable {
358383
lhs.shape.coordinate.longitude == rhs.shape.coordinate.longitude &&
359384
lhs.fillColor == rhs.fillColor
360385
}
361-
386+
362387
var shape: MKOverlay
363388
var fillColor: UIColor?
364389
var strokeColor: UIColor?
@@ -402,7 +427,7 @@ public struct MapView: UIViewRepresentable {
402427
public struct MapViewDemo: View {
403428

404429
@State private var locationManager: CLLocationManager
405-
430+
406431
@State private var mapRegion: MKCoordinateRegion = MKCoordinateRegion(
407432
center: CLLocationCoordinate2D(
408433
latitude: 49.293,
@@ -413,42 +438,42 @@ public struct MapViewDemo: View {
413438
longitudeDelta: 0.01
414439
)
415440
)
416-
441+
417442
@State private var customMapOverlay: MapView.CustomMapOverlay?
418-
443+
419444
@State private var mapType: MKMapType = MKMapType.standard
420-
445+
421446
@State private var zoomEnabled: Bool = true
422447
@State private var showZoomScale: Bool = true
423448
@State private var useMinZoomBoundary: Bool = false
424449
@State private var minZoom: Double = 0
425450
@State private var useMaxZoomBoundary: Bool = false
426451
@State private var maxZoom: Double = 3000000
427-
452+
428453
@State private var scrollEnabled: Bool = true
429454
@State private var useScrollBoundaries: Bool = false
430455
@State private var scrollBoundaries: MKCoordinateRegion = MKCoordinateRegion()
431-
456+
432457
@State private var rotationEnabled: Bool = true
433458
@State private var showCompassWhenRotated: Bool = true
434-
459+
435460
@State private var showUserLocation: Bool = true
436461
@State private var userTrackingMode: MKUserTrackingMode = MKUserTrackingMode.none
437462
@State private var userLocation: CLLocationCoordinate2D?
438-
463+
439464
@State private var showAnnotations: Bool = true
440465
@State private var annotations: [MKPointAnnotation] = []
441-
466+
442467
@State private var showOverlays: Bool = true
443468
@State private var overlays: [MapView.Overlay] = []
444-
469+
445470
@State private var showMapCenter: Bool = false
446-
471+
447472
public init() {
448473
self.locationManager = CLLocationManager()
449474
self.locationManager.requestWhenInUseAuthorization()
450475
}
451-
476+
452477
public var body: some View {
453478

454479
NavigationView {

0 commit comments

Comments
 (0)