aboutsummaryrefslogtreecommitdiff
path: root/iosApp/iosApp/Map/BaseMapView.swift
diff options
context:
space:
mode:
Diffstat (limited to 'iosApp/iosApp/Map/BaseMapView.swift')
-rw-r--r--iosApp/iosApp/Map/BaseMapView.swift107
1 files changed, 94 insertions, 13 deletions
diff --git a/iosApp/iosApp/Map/BaseMapView.swift b/iosApp/iosApp/Map/BaseMapView.swift
index a3d2d70..46e9876 100644
--- a/iosApp/iosApp/Map/BaseMapView.swift
+++ b/iosApp/iosApp/Map/BaseMapView.swift
@@ -1,25 +1,50 @@
-//
-// BaseMapView.swift
-// iosApp
-//
-// Created by Iván on 30/01/22.
-// Copyright © 2022 orgName. All rights reserved.
-//
-
+/**
+ * TrackerMap
+ * Copyright (C) 2021-2022 Iván Ávalos <avalos@disroot.org>, Henoch Ojeda <imhenoch@protonmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
import SwiftUI
-import WhirlyGlobeMaplyComponent
+import Combine
import CryptoKit
+import WhirlyGlobeMaplyComponent
import shared
struct BaseMapView: UIViewControllerRepresentable {
typealias UIViewControllerType = MaplyViewController
@Binding var mapLayer: MapLayer
+ var link: BaseMapLink
class Coordinator: NSObject, MaplyViewControllerDelegate {
var parent: BaseMapView
+ var uiViewController: MaplyViewController?
var loader: MaplyQuadImageLoader? = nil
+ // Source: https://stackoverflow.com/questions/65923718
+ var cancellable: AnyCancellable?
+ var link: BaseMapLink? {
+ didSet {
+ cancellable = link?.$action.sink(receiveValue: { action in
+ guard let action = action else {
+ return
+ }
+ self.uiViewController?.action(action)
+ })
+ }
+ }
+
init(_ uiViewController: BaseMapView) {
self.parent = uiViewController
}
@@ -50,21 +75,25 @@ struct BaseMapView: UIViewControllerRepresentable {
sampleParams.minZoom = tileInfo.minZoom
sampleParams.maxZoom = tileInfo.maxZoom
sampleParams.singleLevel = true
+ sampleParams.maxTiles = 25
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)
+ 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)
return mapViewController
}
func updateUIViewController(_ uiViewController: MaplyViewController, context: Context) {
+ context.coordinator.uiViewController = uiViewController
+ context.coordinator.link = link
+
// MARK: - Set map layer
context.coordinator.loader?.changeTileInfo(tileInfoFrom(layer: mapLayer))
setZoomLimits(uiViewController: uiViewController,
@@ -72,6 +101,8 @@ struct BaseMapView: UIViewControllerRepresentable {
maxZoom: mapLayer.maxZoom)
}
+
+
static func dismantleUIViewController(_ uiViewController: MaplyViewController, coordinator: Coordinator) {
coordinator.loader?.shutdown()
uiViewController.teardown()
@@ -100,3 +131,53 @@ struct BaseMapView: UIViewControllerRepresentable {
)))
}
}
+
+extension MaplyViewController {
+ enum Action {
+ case zoomIn
+ case zoomOut
+ }
+
+ func action(_ action: Action) {
+ switch action {
+ case .zoomIn:
+ zoomIn()
+ case .zoomOut:
+ zoomOut()
+ }
+ }
+
+ func focusOn(point: MaplyCoordinate, height: Float = 0.0000264, animated: Bool = true) {
+ let z = max(height, getMinZoom())
+ if animated {
+ animate(toPosition: point, height: z, time: 0.2)
+ } else {
+ setPosition(point, height: z)
+ }
+ }
+
+ func zoomIn() {
+ let pos = getPosition()
+ let zoom = currentMapScale() / 2
+ focusOn(point: pos, height: height(forMapScale: zoom))
+ }
+
+ func zoomOut() {
+ let pos = getPosition()
+ let zoom = currentMapScale() * 2
+ focusOn(point: pos, height: height(forMapScale: zoom))
+ }
+}
+
+// Source: https://stackoverflow.com/questions/65923718
+class BaseMapLink: ObservableObject {
+ @Published var action: MaplyViewController.Action?
+
+ func zoomIn() {
+ action = .zoomIn
+ }
+
+ func zoomOut() {
+ action = .zoomOut
+ }
+}