diff options
Diffstat (limited to 'iosApp/iosApp/Map/BaseMapView.swift')
-rw-r--r-- | iosApp/iosApp/Map/BaseMapView.swift | 102 |
1 files changed, 102 insertions, 0 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)! + ))) + } +} |