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
|
//
// 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)!
)))
}
}
|