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
331
332
333
334
335
336
337
338
|
/**
* 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 UIKit
import WhirlyGlobe
import shared
typealias MarkerCallback = (Int32?) -> ()
class MapViewController: UIViewController {
var markerCallback: MarkerCallback? = nil
var mapLayer: MapLayer = MapLayer.companion.defaultLayer
@IBOutlet weak var mapContainer: UIView!
@IBOutlet weak var attributionText: UITextView!
private var mapView: OurMaplyViewController!
override func viewDidLoad() {
super.viewDidLoad()
// MARK: - Initialise MapViewController
mapView = OurMaplyViewController(mapType: .typeFlat)
mapView.delegate = self
view.sendSubviewToBack(mapContainer)
mapContainer.addSubview(mapView.view)
mapView.view.frame = mapContainer.bounds
addChild(mapView)
setAttributionText(mapLayer)
// MARK: - Configure MaplyViewController
let tileInfo = Utils.tileInfoFrom(layer: mapLayer)
mapView.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
if let loader = MaplyQuadImageLoader(params: sampleParams,
tileInfo: tileInfo,
viewC: mapView) {
mapView.setLoader(loader)
}
mapView.runOnInit {
let point = MaplyCoordinateMakeWithDegrees(-100.36, 23.191)
self.mapView.setPosition(point, height: 0.4)
}
}
func setMapLayer(_ layer: MapLayer) {
setAttributionText(layer)
mapView.setLayer(layer)
}
func display(markers: [Marker],
isReport: Bool,
center: Bool = false) {
DispatchQueue.main.async {
self.mapView.display(markers: markers,
isReport: isReport,
center: center)
}
}
func focusOn(marker: Marker) {
DispatchQueue.main.async {
self.mapView.focusOn(marker: marker)
}
}
func dismantle() {
mapView.dismantle()
}
private func setAttributionText(_ layer: MapLayer) {
DispatchQueue.main.async {
self.attributionText.attributedText = HtmlString.htmlToAttrStr(
layer.attribution,
size: UIFont.smallSystemFontSize,
color: UIColor.label)
}
attributionText.sizeToFit()
attributionText.layoutIfNeeded()
}
@IBAction func onZoomInPressed(_ sender: UIButton) {
mapView.zoomIn()
}
@IBAction func onZoomOutPressed(_ sender: UIButton) {
mapView.zoomOut()
}
}
extension MapViewController: MaplyViewControllerDelegate {
func maplyViewController(_ viewC: MaplyViewController,
didTapAt coord: MaplyCoordinate) {
markerCallback?(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 {
markerCallback?(id)
}
}
}
}
class OurMaplyViewController: MaplyViewController {
private var loader: MaplyQuadImageLoader? = nil
private var objects = [MaplyComponentObject]()
private var geofenceObjects = [MaplyComponentObject]()
func runOnInit(callback: @escaping () -> ()) {
addPostInitBlock {
callback()
}
}
func setLoader(_ loader: MaplyQuadImageLoader) {
self.loader = loader
}
func setLayer(_ layer: MapLayer) {
loader?.changeTileInfo(Utils.tileInfoFrom(layer: layer))
setZoomLimits(minZoom: layer.minZoom,
maxZoom: layer.maxZoom)
}
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 focusOn(marker: Marker, animated: Bool = true) {
let point = MaplyCoordinateMakeWithDegrees(Float(marker.longitude),
Float(marker.latitude))
focusOn(point: point, animated: animated)
}
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 = UIColor.green
let colorLabel = UIColor.secondaryLabel
let colorLabelOutline = UIColor.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)!
)))
}
func dismantle() {
loader?.shutdown()
teardown()
}
private func getIcon(markerType: Marker.Type_) -> UIImage {
return UIImage(named: MarkerTransformations
.markerTypeToImageName(markerType: markerType))!
}
}
|