From 72a1d714a539a93a90e864cd725f9b0944068183 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 31 Jan 2022 03:38:12 -0600 Subject: Fixed initial zoom, improved map controls and map refactoring --- .../UserInterfaceState.xcuserstate | Bin 58824 -> 0 bytes iosApp/iosApp/Map/BaseMapView.swift | 73 ++++++++------------- iosApp/iosApp/Map/MapWrapperView.swift | 46 +++++++------ iosApp/iosApp/Shared/Utils.swift | 13 ++++ 4 files changed, 68 insertions(+), 64 deletions(-) delete mode 100644 iosApp/iosApp.xcodeproj/project.xcworkspace/xcuserdata/avalos.xcuserdatad/UserInterfaceState.xcuserstate diff --git a/iosApp/iosApp.xcodeproj/project.xcworkspace/xcuserdata/avalos.xcuserdatad/UserInterfaceState.xcuserstate b/iosApp/iosApp.xcodeproj/project.xcworkspace/xcuserdata/avalos.xcuserdatad/UserInterfaceState.xcuserstate deleted file mode 100644 index 9ba4764..0000000 Binary files a/iosApp/iosApp.xcodeproj/project.xcworkspace/xcuserdata/avalos.xcuserdatad/UserInterfaceState.xcuserstate and /dev/null differ diff --git a/iosApp/iosApp/Map/BaseMapView.swift b/iosApp/iosApp/Map/BaseMapView.swift index 46e9876..bf2c82b 100644 --- a/iosApp/iosApp/Map/BaseMapView.swift +++ b/iosApp/iosApp/Map/BaseMapView.swift @@ -17,7 +17,6 @@ */ import SwiftUI import Combine -import CryptoKit import WhirlyGlobeMaplyComponent import shared @@ -60,13 +59,11 @@ struct BaseMapView: UIViewControllerRepresentable { func makeUIViewController(context: Context) -> MaplyViewController { let mapViewController = MaplyViewController(mapType: .typeFlat) - mapViewController.viewWrap = true mapViewController.delegate = context.coordinator - let tileInfo = tileInfoFrom(layer: mapLayer) - setZoomLimits(uiViewController: mapViewController, - minZoom: mapLayer.minZoom, - maxZoom: mapLayer.maxZoom) + let tileInfo = Utils.tileInfoFrom(layer: mapLayer) + mapViewController.setZoomLimits(minZoom: mapLayer.minZoom, + maxZoom: mapLayer.maxZoom) let sampleParams = MaplySamplingParams() sampleParams.coordSys = MaplySphericalMercator(webStandard: ()) @@ -82,10 +79,10 @@ struct BaseMapView: UIViewControllerRepresentable { loader?.imageFormat = .imageUShort565 context.coordinator.loader = loader - let latitude = 23.191 * Float.pi / 180 - let longitude = -100.36 * Float.pi / 180 - let point = MaplyCoordinate(x: longitude, y: latitude) - mapViewController.focusOn(point: point, height: 0.4) + DispatchQueue.main.async { + let point = MaplyCoordinateMakeWithDegrees(-100.36, 23.191) + mapViewController.focusOn(point: point, height: 0.4) + } return mapViewController } @@ -95,41 +92,15 @@ struct BaseMapView: UIViewControllerRepresentable { context.coordinator.link = link // MARK: - Set map layer - context.coordinator.loader?.changeTileInfo(tileInfoFrom(layer: mapLayer)) - setZoomLimits(uiViewController: uiViewController, - minZoom: mapLayer.minZoom, - maxZoom: mapLayer.maxZoom) + context.coordinator.loader?.changeTileInfo(Utils.tileInfoFrom(layer: mapLayer)) + uiViewController.setZoomLimits(minZoom: mapLayer.minZoom, + maxZoom: mapLayer.maxZoom) } - - static func dismantleUIViewController(_ uiViewController: MaplyViewController, coordinator: Coordinator) { coordinator.loader?.shutdown() uiViewController.teardown() } - - private func tileInfoFrom(layer: MapLayer) -> MaplyRemoteTileInfoNew { - let cacheDir = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0] - let thisCacheDir = "\(cacheDir)/\(Utils.MD5(string: mapLayer.url))" - - let tileInfo = MaplyRemoteTileInfoNew(baseURL: mapLayer.url, - minZoom: mapLayer.minZoom, - maxZoom: mapLayer.maxZoom) - tileInfo.cacheDir = thisCacheDir - return tileInfo - } - - private func setZoomLimits(uiViewController: MaplyViewController, minZoom: Int32, maxZoom: Int32) { - uiViewController.setZoomLimitsMin( - uiViewController.height(forMapScale: Float(truncating: - MapCalculus.companion.zoomLevelToScale(zoom: maxZoom) - ?? MapCalculus.companion.zoomLevelToScale(zoom: 21)! - )), - max: uiViewController.height(forMapScale: Float(truncating: - MapCalculus.companion.zoomLevelToScale(zoom: minZoom) - ?? MapCalculus.companion.zoomLevelToScale(zoom: 1)! - ))) - } } extension MaplyViewController { @@ -139,11 +110,13 @@ extension MaplyViewController { } func action(_ action: Action) { - switch action { - case .zoomIn: - zoomIn() - case .zoomOut: - zoomOut() + DispatchQueue.main.async { + switch action { + case .zoomIn: + self.zoomIn() + case .zoomOut: + self.zoomOut() + } } } @@ -167,6 +140,18 @@ extension MaplyViewController { let zoom = currentMapScale() * 2 focusOn(point: pos, height: height(forMapScale: zoom)) } + + func setZoomLimits(minZoom: Int32, maxZoom: Int32) { + setZoomLimitsMin( + height(forMapScale: Float(truncating: + MapCalculus.companion.zoomLevelToScale(zoom: maxZoom) + ?? MapCalculus.companion.zoomLevelToScale(zoom: 21)! + )), + max: height(forMapScale: Float(truncating: + MapCalculus.companion.zoomLevelToScale(zoom: minZoom) + ?? MapCalculus.companion.zoomLevelToScale(zoom: 1)! + ))) + } } // Source: https://stackoverflow.com/questions/65923718 diff --git a/iosApp/iosApp/Map/MapWrapperView.swift b/iosApp/iosApp/Map/MapWrapperView.swift index e9133eb..0dfb823 100644 --- a/iosApp/iosApp/Map/MapWrapperView.swift +++ b/iosApp/iosApp/Map/MapWrapperView.swift @@ -45,27 +45,23 @@ struct MapWrapperView: View { // MARK: - Controls VStack { - Group { - Button { - print ("Zoom in!") - link.zoomIn() - } label: { - Image(systemName: "plus") - .imageScale(.large) - } - - Button { - print("Zoom out!") - link.zoomOut() - } label: { - Image(systemName: "minus") - .imageScale(.large) - } + // MARK: Zoom in + Button { + print ("Zoom in!") + link.zoomIn() + } label: { + Image(systemName: "plus").imageScale(.large) } - .frame(width: 50, height: 50) - .foregroundColor(.primary) - .background(.systemBackground) - .clipShape(Circle()) + .buttonStyle(ControlButtonStyle()) + + // MARK: Zoom out + Button { + print("Zoom out!") + link.zoomOut() + } label: { + Image(systemName: "minus").imageScale(.large) + } + .buttonStyle(ControlButtonStyle()) } .frame(maxWidth: .infinity, maxHeight: .infinity, @@ -74,3 +70,13 @@ struct MapWrapperView: View { } } } + +struct ControlButtonStyle: ButtonStyle { + func makeBody(configuration: Configuration) -> some View { + configuration.label + .frame(width: 50, height: 50) + .foregroundColor(configuration.isPressed ? .secondary : .primary) + .background(configuration.isPressed ? .secondarySystemBackground : .systemBackground) + .clipShape(Circle()) + } +} diff --git a/iosApp/iosApp/Shared/Utils.swift b/iosApp/iosApp/Shared/Utils.swift index 625b082..5a06d8f 100644 --- a/iosApp/iosApp/Shared/Utils.swift +++ b/iosApp/iosApp/Shared/Utils.swift @@ -17,6 +17,8 @@ */ import Foundation import CryptoKit +import WhirlyGlobeMaplyComponent +import shared class Utils { static func MD5(string: String) -> String { @@ -25,4 +27,15 @@ class Utils { String(format: "%02hhx", $0) }.joined() } + + static func tileInfoFrom(layer: MapLayer) -> MaplyRemoteTileInfoNew { + let cacheDir = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0] + let thisCacheDir = "\(cacheDir)/\(Utils.MD5(string: layer.url))" + + let tileInfo = MaplyRemoteTileInfoNew(baseURL: layer.url, + minZoom: layer.minZoom, + maxZoom: layer.maxZoom) + tileInfo.cacheDir = thisCacheDir + return tileInfo + } } -- cgit v1.2.3