aboutsummaryrefslogtreecommitdiff
path: root/iosApp/iosApp/Map/MapView.swift
blob: 572db6408848793e1672ed483d5377c4fe6a13e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
 * 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 Combine
import WhirlyGlobeMaplyComponent
import shared

struct MapView: UIViewControllerRepresentable {
    typealias UIViewControllerType = OurMaplyViewController
    
    @Binding var mapLayer: MapLayer
    @Binding var markers: [Marker]
    var markerCallback: ((Int32?) -> Void)?
    var link: BaseMapLink
    
    class Coordinator: NSObject, MaplyViewControllerDelegate {
        var parent: MapView
        var uiViewController: OurMaplyViewController?
        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: MapView) {
            self.parent = uiViewController
        }
        
        func maplyViewController(_ viewC: MaplyViewController,
                                 didTapAt coord: MaplyCoordinate) {
            if let callback = parent.markerCallback {
                callback(nil)
            }
        }
        
        func maplyViewController(_ viewC: MaplyViewController,
                                 didSelect selectedObj: NSObject,
                                 atLoc coord: MaplyCoordinate,
                                 onScreen screenPt: CGPoint) {
            if let marker = selectedObj as? MaplyScreenMarker {
                if let id = marker.userObject as? Int32 {
                    if let callback = parent.markerCallback {
                        callback(id)
                    }
                }
            }
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    func makeUIViewController(context: Context) -> OurMaplyViewController {
        let mapViewController = OurMaplyViewController(mapType: .typeFlat)
        mapViewController.delegate = context.coordinator
        
        let tileInfo = Utils.tileInfoFrom(layer: mapLayer)
        mapViewController.setZoomLimits(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
        sampleParams.maxTiles = 25
        
        let loader = MaplyQuadImageLoader(params: sampleParams, tileInfo: tileInfo, viewC: mapViewController)
        loader?.baseDrawPriority = kMaplyImageLayerDrawPriorityDefault
        loader?.imageFormat = .imageUShort565
        context.coordinator.loader = loader
        
        DispatchQueue.main.async {
            let point = MaplyCoordinateMakeWithDegrees(-100.36, 23.191)
            mapViewController.focusOn(point: point, height: 0.4, animated: false)
        }
        
        return mapViewController
    }
    
    func updateUIViewController(_ uiViewController: OurMaplyViewController, context: Context) {
        context.coordinator.uiViewController = uiViewController
        context.coordinator.link = link
        
        // MARK: - Set map layer
        context.coordinator.loader?.changeTileInfo(Utils.tileInfoFrom(layer: mapLayer))
        uiViewController.setZoomLimits(minZoom: mapLayer.minZoom,
                                       maxZoom: mapLayer.maxZoom)
        
        // MARK: - Set markers
        uiViewController.display(markers: markers,
                                 isReport: false)
    }
    
    static func dismantleUIViewController(_ uiViewController: MaplyViewController, coordinator: Coordinator) {
        coordinator.loader?.shutdown()
        uiViewController.teardown()
    }
}

class OurMaplyViewController: MaplyViewController {
    enum Action {
        case zoomIn
        case zoomOut
    }
    
    private var objects = [MaplyComponentObject]()
    private var geofenceObjects = [MaplyComponentObject]()
    
    func action(_ action: Action) {
        DispatchQueue.main.async {
            switch action {
            case .zoomIn:
                self.zoomIn()
            case .zoomOut:
                self.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))
    }
    
    func clear(geofences: Bool = false) {
        if geofences {
            remove(geofenceObjects)
            geofenceObjects.removeAll()
        } else {
            remove(objects)
            objects.removeAll()
        }
    }
    
    func display(markers: [Marker],
                 isReport: Bool,
                 center: Bool = false) {
        clear()
        
        let points = markers.map { marker in
            MaplyCoordinateMakeWithDegrees(Float(marker.longitude),
                                           Float(marker.latitude))
        }
        
        let fontSize = 11.0
        let colorReport = Color.green
        let colorLabel = Color.secondary
        let colorLabelOutline = Color.systemBackground
        
        let vectorDesc: [AnyHashable : Any] = [
            kMaplyColor: colorReport,
            kMaplyVecWidth: 20.0
        ]
        
        let labelDesc: [AnyHashable : Any] = [
            kMaplyFont: UIFont.boldSystemFont(ofSize: fontSize),
            kMaplyTextColor: colorLabel,
            kMaplyTextOutlineColor: colorLabelOutline,
            kMaplyTextOutlineSize: 3.0
        ]
    
        /* MARK: - Draw markers for positions */
        let screenMarkers = markers.enumerated().map { (i, marker) -> MaplyScreenMarker in
            let screenMarker = MaplyScreenMarker()
            screenMarker.layoutImportance = .greatestFiniteMagnitude
            screenMarker.loc = MaplyCoordinateMakeWithDegrees(Float(marker.longitude),
                                                              Float(marker.latitude))
            var type: Marker.Type_ = .default_
            if isReport {
                // For reports, position, start and end icons must be different
                switch i {
                case markers.startIndex: type = .reportStart
                case markers.endIndex: type = .reportEnd
                default: type = .reportPosition
                }
            } else {
                type = marker.type
            }
            screenMarker.image = getIcon(markerType: type)
            
            var size = 50.0
            if isReport {
                // For reports, position, start and end sizes must be different
                switch i {
                case markers.startIndex: size = 50.0
                case markers.endIndex: size = 50.0
                default: size = 25.0
                }
            }
            screenMarker.size = CGSize(width: size, height: size)
            screenMarker.userObject = marker.id
            screenMarker.selectable = true
            
            return screenMarker
        }
        
        if let objs = addScreenMarkers(screenMarkers, desc: nil, mode: .any) {
            objects.append(objs)
        }
        
        /* MARK: - Add labels for markers */
        if !isReport && !markers.isEmpty {
            let screenLabels = markers.map { marker -> MaplyScreenLabel in
                let label = MaplyScreenLabel()
                label.layoutImportance = .greatestFiniteMagnitude
                var text = marker.name
                if marker.name.count >= 20 {
                    let end = marker.name.index(marker.name.startIndex, offsetBy: 20)
                    text = String(marker.name[..<end])
                }
                label.text = text
                label.loc = MaplyCoordinateMakeWithDegrees(Float(marker.longitude),
                                                           Float(marker.latitude))
                label.offset = CGPoint(x: 0.0, y: 25.0)
                
                return label
            }
            
            if let objs = addScreenLabels(screenLabels, desc: labelDesc) {
                objects.append(objs)
            }
        }
        
        /* MARK: - Draw polyline for report */
        if isReport && !markers.isEmpty {
            let geoJSON: [AnyHashable : Any] = [
                "type": "FeatureCollection",
                "features": [
                    [
                        "type": "LineString",
                        "coordinates": points.map({ point in
                            [point.x, point.y]
                        })
                    ]
                ]
            ]
            if let vector = MaplyVectorObject(fromGeoJSONDictionary: geoJSON) {
                if let objs = addVectors([vector], desc: vectorDesc, mode: .any) {
                    objects.append(objs)
                }
            }
        }
        
        /* MARK: - Center map to bounds */
        if center && !markers.isEmpty {
            let box = MaplyBoundingBoxExpandByFraction(
                MaplyBoundingBoxFromCoordinates(points, UInt32(points.count)), 0.1)
            let center = MaplyCoordinate(x: (box.ur.x + box.ll.x) / 2,
                                         y: (box.ur.y + box.ll.y) / 2)
            let zoom = max(findHeight(toViewBounds: box, pos: center), getMinZoom())
            setPosition(center, height: 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)!
            )))
    }
    
    private func getIcon(markerType: Marker.Type_) -> UIImage {
        return UIImage(named: MarkerTransformations
                        .markerTypeToImageName(markerType: markerType))!
    }
}

// Source: https://stackoverflow.com/questions/65923718
class BaseMapLink: ObservableObject {
    @Published var action: OurMaplyViewController.Action?
    
    func zoomIn() {
        action = .zoomIn
    }
    
    func zoomOut() {
        action = .zoomOut
    }
}