From 82e2c03d5730d933ca4d081a0937aa62935a6610 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sun, 23 Apr 2023 07:39:19 -0700 Subject: [PATCH] Add back tile server settings --- .../Views/Map/Custom/MapViewSwiftUI.swift | 72 +++++++++++++++++++ Meshtastic/Views/Settings/AppSettings.swift | 13 ++++ 2 files changed, 85 insertions(+) diff --git a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift index 778302ed..af5e576e 100644 --- a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift +++ b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift @@ -30,6 +30,11 @@ struct MapViewSwiftUI: UIViewRepresentable { var customMapOverlay: CustomMapOverlay? @State private var presentCustomMapOverlayHash: CustomMapOverlay? + // Custom Tile Server + @AppStorage("meshMapCustomTileServer") private var tileServerUrl = "" + var tileRenderer: MKTileOverlayRenderer? + let tileServer: MapTileServerLinks = .openStreetMaps + func makeUIView(context: Context) -> MKMapView { // Map View Parameters mapView.mapType = mapViewType @@ -65,6 +70,10 @@ struct MapViewSwiftUI: UIViewRepresentable { mapView.showsScale = true mapView.showsTraffic = true + let overlay = TileServerOverlay() // Offline-Map-Tiles + overlay.canReplaceMapContent = true + mapView.addOverlay(overlay, level: .aboveLabels) + #if targetEnvironment(macCatalyst) // Show the default always visible compass and the mac only controls mapView.showsCompass = true @@ -167,6 +176,29 @@ struct MapViewSwiftUI: UIViewRepresentable { mapView.showsUserLocation = true } mapView.setUserTrackingMode(userTrackingMode, animated: true) + + if tileServerUrl.count > 0 { + tileRenderer?.alpha = 0.0 + let overlays = mapView.overlays + if mapView.mapType == .standard { + let overlay = MKTileOverlay(urlTemplate: tileServerUrl) + if overlays.contains(where: {$0 is MKPolyline}) { + mapView.addOverlay(overlay, level: .aboveLabels) + if let poly_overlay = overlays.filter({$0 is MKPolyline}).first { + mapView.addOverlay(poly_overlay, level: .aboveRoads) + } + } else { + mapView.addOverlay(overlay, level: .aboveRoads) + + } + } else { + for overlay in overlays { + if let ove = overlay as? MKTileOverlay { + mapView.removeOverlay(ove) + } + } + } + } } } } @@ -465,3 +497,43 @@ struct MapViewSwiftUI: UIViewRepresentable { } } } + +class TileServerOverlay: MKTileOverlay { + + override func url(forTilePath path: MKTileOverlayPath) -> URL { // lädt die Map-Tiles + + if path.z <= 5 { // Es wurden nur Map-Tiles für z <= 5 geladen. + + let fileManager = FileManager.default // Objekt zum Verwalten des Dateisystems + + if var url = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first { // Url vom Cache-Verzeichnis + + // Pfad wird erstellt + + url.appendPathComponent("map") + url.appendPathComponent("\(path.z)") + url.appendPathComponent("\(path.x)") + url.appendPathComponent("\(path.y).png") + + if fileManager.fileExists(atPath: url.path) { // Wenn das Map-Tile existiert... + + return url + } + else { + + //logger.info("OSMTileOverlay: MapTiles have not been downloaded yet.") + return Bundle.main.url(forResource: "default", withExtension: "png")! + } + } + else { + + //logger.error("OSMTileOverlay: Could not find cache url.") + return Bundle.main.url(forResource: "default", withExtension: "png")! + } + } + else { + + return Bundle.main.url(forResource: "default", withExtension: "png")! + } + } +} diff --git a/Meshtastic/Views/Settings/AppSettings.swift b/Meshtastic/Views/Settings/AppSettings.swift index c568da57..ee308564 100644 --- a/Meshtastic/Views/Settings/AppSettings.swift +++ b/Meshtastic/Views/Settings/AppSettings.swift @@ -120,6 +120,19 @@ struct AppSettings: View { Label("Show Route Lines", systemImage: "road.lanes") } .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + + HStack { + + Label("Tile Server", systemImage: "square.grid.3x2") + TextField( + "Tile Server", + text: $userSettings.meshMapCustomTileServer, + axis: .vertical + ) + .foregroundColor(.gray) + } + .keyboardType(.asciiCapable) + .disableAutocorrection(true) } } HStack {