diff options
Diffstat (limited to 'iosApp/iosApp/Map')
-rw-r--r-- | iosApp/iosApp/Map/BaseMapView.swift | 102 | ||||
-rw-r--r-- | iosApp/iosApp/Map/MapView.swift | 10 | ||||
-rw-r--r-- | iosApp/iosApp/Map/MapWrapperView.swift | 63 |
3 files changed, 167 insertions, 8 deletions
diff --git a/iosApp/iosApp/Map/BaseMapView.swift b/iosApp/iosApp/Map/BaseMapView.swift new file mode 100644 index 0000000..a3d2d70 --- /dev/null +++ b/iosApp/iosApp/Map/BaseMapView.swift @@ -0,0 +1,102 @@ +// +// BaseMapView.swift +// iosApp +// +// Created by Iván on 30/01/22. +// Copyright © 2022 orgName. All rights reserved. +// + +import SwiftUI +import WhirlyGlobeMaplyComponent +import CryptoKit +import shared + +struct BaseMapView: UIViewControllerRepresentable { + typealias UIViewControllerType = MaplyViewController + + @Binding var mapLayer: MapLayer + + class Coordinator: NSObject, MaplyViewControllerDelegate { + var parent: BaseMapView + var loader: MaplyQuadImageLoader? = nil + + init(_ uiViewController: BaseMapView) { + self.parent = uiViewController + } + + func maplyViewController(_ viewC: MaplyViewController, didClick annotation: MaplyAnnotation) { + print("Clicked map! :D") + } + } + + func makeCoordinator() -> Coordinator { + Coordinator(self) + } + + 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 sampleParams = MaplySamplingParams() + sampleParams.coordSys = MaplySphericalMercator(webStandard: ()) + sampleParams.coverPoles = true + sampleParams.edgeMatching = true + sampleParams.minZoom = tileInfo.minZoom + sampleParams.maxZoom = tileInfo.maxZoom + sampleParams.singleLevel = true + + let loader = MaplyQuadImageLoader(params: sampleParams, tileInfo: tileInfo, viewC: mapViewController) + loader?.baseDrawPriority = kMaplyImageLayerDrawPriorityDefault + loader?.imageFormat = .imageUShort565 + context.coordinator.loader = loader + + let point = MaplyCoordinate(x: -100.36 * Float.pi / 180, + y: 23.191 * Float.pi / 180) + let height = Float(0.4) + mapViewController.setPosition(point, height: height) + + return mapViewController + } + + func updateUIViewController(_ uiViewController: MaplyViewController, context: Context) { + // MARK: - Set map layer + context.coordinator.loader?.changeTileInfo(tileInfoFrom(layer: mapLayer)) + setZoomLimits(uiViewController: uiViewController, + 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)! + ))) + } +} diff --git a/iosApp/iosApp/Map/MapView.swift b/iosApp/iosApp/Map/MapView.swift index 8732ef4..33b9932 100644 --- a/iosApp/iosApp/Map/MapView.swift +++ b/iosApp/iosApp/Map/MapView.swift @@ -7,18 +7,12 @@ // import SwiftUI +import shared struct MapView: View { @StateObject var unitsViewModel: UnitsViewModel var body: some View { - VStack { - Text("Unimplemented!") - if let deviceName = unitsViewModel.selectedUnit?.device.name { - Text("Selected: \(deviceName)") - .padding() - } - - } + MapWrapperView(layer: $unitsViewModel.mapLayerType) } } diff --git a/iosApp/iosApp/Map/MapWrapperView.swift b/iosApp/iosApp/Map/MapWrapperView.swift new file mode 100644 index 0000000..2a99f87 --- /dev/null +++ b/iosApp/iosApp/Map/MapWrapperView.swift @@ -0,0 +1,63 @@ +// +// MapWrapperView.swift +// iosApp +// +// Created by Iván on 30/01/22. +// Copyright © 2022 orgName. All rights reserved. +// + +import SwiftUI +import WhirlyGlobeMaplyComponent +import shared + +struct MapWrapperView: View { + @Binding var layer: MapLayer + + var body: some View { + ZStack { + // MARK: - Map + BaseMapView(mapLayer: $layer) + + // MARK: - Attribution + VStack { + HyperlinkText(html: layer.attribution) + .font(.footnote) + .lineLimit(3) + .foregroundColor(.label.opacity(0.35)) + .padding() + .background(.systemBackground.opacity(0.35)) + } + .frame(maxWidth: .infinity, + maxHeight: .infinity, + alignment: .bottom) + .allowsHitTesting(false) + + // MARK: - Controls + VStack { + Group { + Button { + print ("Zoom in!") + } label: { + Image(systemName: "plus") + .imageScale(.large) + } + + Button { + print("Zoom out!") + } label: { + Image(systemName: "minus") + .imageScale(.large) + } + } + .frame(width: 50, height: 50) + .foregroundColor(.primary) + .background(.systemBackground) + .clipShape(Circle()) + } + .frame(maxWidth: .infinity, + maxHeight: .infinity, + alignment: .topTrailing) + .padding() + } + } +} |