Skip to content

Commit b93f396

Browse files
author
fonok3
committed
🔖 1.1.4 [ci skip]
1 parent fa6e38d commit b93f396

15 files changed

+149
-68
lines changed

Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
5+
import Foundation
56

67
let package = Package(
78
name: "ios-map-utility",
@@ -21,6 +22,9 @@ let package = Package(
2122
name: "GMMapUtility",
2223
dependencies: [
2324
.product(name: "Mapbox", package: "maplibre-gl-native-distribution")
25+
],
26+
resources: [
27+
.process("Resources/PrivacyInfo.xcprivacy")
2428
]
2529
)
2630
]

Sources/GMMapUtility/Extensions/CLLocationCoordinate2D+GeographicMidpoint.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@ extension Collection where Element == CLLocationCoordinate2D {
77
return first ?? CLLocationCoordinate2D(latitude: 0, longitude: 0)
88
}
99

10-
var x = Double(0)
11-
var y = Double(0)
12-
var z = Double(0)
10+
var xCoordinate = Double(0)
11+
var yCoordinate = Double(0)
12+
var zCoordinate = Double(0)
1313

1414
for coordinate in self {
1515
let lat = deg2rad(coordinate.latitude)
1616
let lon = deg2rad(coordinate.longitude)
17-
x += cos(lat) * cos(lon)
18-
y += cos(lat) * sin(lon)
19-
z += sin(lat)
17+
xCoordinate += cos(lat) * cos(lon)
18+
yCoordinate += cos(lat) * sin(lon)
19+
zCoordinate += sin(lat)
2020
}
2121

22-
x /= Double(count)
23-
y /= Double(count)
24-
z /= Double(count)
22+
xCoordinate /= Double(count)
23+
yCoordinate /= Double(count)
24+
zCoordinate /= Double(count)
2525

26-
let lon = atan2(y, x)
27-
let hyp = sqrt(x * x + y * y)
28-
let lat = atan2(z, hyp)
26+
let lon = atan2(yCoordinate, xCoordinate)
27+
let hyp = sqrt(xCoordinate * xCoordinate + yCoordinate * yCoordinate)
28+
let lat = atan2(zCoordinate, hyp)
2929

3030
return CLLocationCoordinate2D(latitude: rad2deg(lat), longitude: rad2deg(lon))
3131
}
3232

3333
private func deg2rad(_ number: Double) -> Double {
34-
return number * .pi / 180
34+
return number * .pi / 180 // swiftlint:disable:this no_magic_numbers
3535
}
3636

3737
private func rad2deg(_ number: Double) -> Double {
38-
return number * 180 / .pi
38+
return number * 180 / .pi // swiftlint:disable:this no_magic_numbers
3939
}
4040
}

Sources/GMMapUtility/Layers/Handler/MapLayerHandler.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import Foundation
22

33
/// Handles the updates and styling of objects shown on a map which belong together.
44
///
5-
/// - note: A `MapLayerHandler` may be self updating to model changes or receive udpates from outside. Often these two purposes are split up.
5+
/// - note: A `MapLayerHandler` may be self updating to model changes or receive udpates from outside.
6+
/// Often these two purposes are split up.
67
public protocol MapLayerHandler {
78
var mapTheme: MapTheme { get }
89

Sources/GMMapUtility/MapLibre/Extensions/MGLPolylineFeature+Geodesic.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ extension MGLPolylineFeature {
66
fromCoordinate src: CLLocationCoordinate2D,
77
toCoordinate dst: CLLocationCoordinate2D
88
) -> MGLPolylineFeature {
9+
// swiftlint:disable:next no_magic_numbers
910
let coordinates = MGLPolylineFeature.createBezierCurve(src: src, dst: dst, offset: 0.4, numberOfSteps: 12)
1011
return MGLPolylineFeature(coordinates: coordinates, count: UInt(coordinates.count))
1112
}
1213

13-
private static var earthRadiusMeter: Double { 6_371_008.8 }
14-
private static var metersNorthToLatitude: Double { 180.0 / Double.pi / earthRadiusMeter }
15-
14+
private static let earthRadiusMeter = 6_371_008.8
15+
// swiftlint:disable:next no_magic_numbers
16+
private static let metersNorthToLatitude = 180.0 / Double.pi / earthRadiusMeter
17+
// swiftlint:disable:next no_magic_numbers
1618
private static func deg2rad(_ number: Double) -> Double { number * .pi / 180 }
1719

1820
private static func metersEastToLongitude(latitude: Double) -> Double {
@@ -21,13 +23,20 @@ extension MGLPolylineFeature {
2123

2224
private static func shiftByCartesian(latitude: Double, longitude: Double, metersNorth: Double, metersEast: Double)
2325
-> CLLocationCoordinate2D {
24-
CLLocationCoordinate2D(latitude: latitude + metersNorth * metersNorthToLatitude,
25-
longitude: longitude + metersEast * metersEastToLongitude(latitude: latitude))
26+
CLLocationCoordinate2D(
27+
latitude: latitude + metersNorth * metersNorthToLatitude,
28+
longitude: longitude + metersEast * metersEastToLongitude(latitude: latitude)
29+
)
2630
}
2731

2832
private static func shiftByCartesian(src: CLLocationCoordinate2D, metersNorth: Double, metersEast: Double)
2933
-> CLLocationCoordinate2D {
30-
shiftByCartesian(latitude: src.latitude, longitude: src.longitude, metersNorth: metersNorth, metersEast: metersEast)
34+
shiftByCartesian(
35+
latitude: src.latitude,
36+
longitude: src.longitude,
37+
metersNorth: metersNorth,
38+
metersEast: metersEast
39+
)
3140
}
3241

3342
private static func distanceMetersNorth(src: CLLocationCoordinate2D, dst: CLLocationCoordinate2D) -> Double {
@@ -49,15 +58,15 @@ extension MGLPolylineFeature {
4958
normalisation = -offset
5059
}
5160

52-
let northMid = 0.5 * northEnd + eastEnd * normalisation
53-
let eastMid = 0.5 * eastEnd - northEnd * normalisation
61+
let northMid = 0.5 * northEnd + eastEnd * normalisation // swiftlint:disable:this no_magic_numbers
62+
let eastMid = 0.5 * eastEnd - northEnd * normalisation // swiftlint:disable:this no_magic_numbers
5463

5564
var positions = [CLLocationCoordinate2D]()
5665

5766
let step = 1.0 / (Double(numberOfSteps) - 1.0)
5867
for index in 0 ..< numberOfSteps {
5968
let weight = Double(index) * step
60-
let factorMid = 2.0 * (1.0 - weight) * weight
69+
let factorMid = 2.0 * (1.0 - weight) * weight // swiftlint:disable:this no_magic_numbers
6170
let factorEnd = weight * weight
6271
let north = northMid * factorMid + northEnd * factorEnd
6372
let east = eastMid * factorMid + eastEnd * factorEnd

Sources/GMMapUtility/MapLibre/Layers/MGLMapViewLifeCycleHandler.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ import Foundation
22
import Mapbox
33
import UIKit
44

5-
public protocol MapLayerHandlerBuilder {
6-
func mapLayerHandler(
7-
for mapView: MGLMapView,
8-
withMapTheme mapTheme: MapTheme
9-
) -> MGLStyleLayersHandler
10-
}
11-
125
public final class MGLMapViewLifeCycleHandler: NSObject {
136
private var mapThemeRepository: MapThemeRepository
147
private let mapStyleUrlProvider: MGLMapStyleUrlProvider
@@ -97,7 +90,8 @@ extension MGLMapViewLifeCycleHandler {
9790
mapStyleLocalizer.localize(style, locale: Locale.current)
9891
}
9992

100-
@objc private func didTapMapView(sender: UITapGestureRecognizer) {
93+
@objc
94+
private func didTapMapView(sender: UITapGestureRecognizer) {
10195
guard let mapView = mapView else {
10296
return
10397
}
@@ -126,7 +120,10 @@ extension MGLMapViewLifeCycleHandler: MGLMapViewDelegate {
126120
}
127121

128122
extension MGLMapViewLifeCycleHandler: UIGestureRecognizerDelegate {
129-
public func gestureRecognizer(_: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith _: UIGestureRecognizer) -> Bool {
123+
public func gestureRecognizer(
124+
_: UIGestureRecognizer,
125+
shouldRecognizeSimultaneouslyWith _: UIGestureRecognizer
126+
) -> Bool {
130127
return true
131128
}
132129
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Foundation
2+
import Mapbox
3+
4+
public protocol MapLayerHandlerBuilder {
5+
func mapLayerHandler(
6+
for mapView: MGLMapView,
7+
withMapTheme mapTheme: MapTheme
8+
) -> MGLStyleLayersHandler
9+
}

Sources/GMMapUtility/MapLibre/Layers/MapboxMapLayerManager.swift

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
import Foundation
22
import Mapbox
33

4-
enum MapboxMapLayerManagerError: LocalizedError {
5-
case layerAlreadyAdded(String)
6-
case sourceUnavailable(String)
7-
8-
// MARK: Internal
9-
10-
var errorDescription: String? {
11-
switch self {
12-
case let .layerAlreadyAdded(layerId): return "Already added layer with id `\(layerId)`."
13-
case let .sourceUnavailable(sourceId): return "Source with id `\(sourceId)` is not added to the style."
14-
}
15-
}
16-
}
17-
184
public class MapboxMapLayerManager {
195
public weak var mapView: MGLMapView?
206

@@ -143,7 +129,8 @@ extension MapboxMapLayerManager {
143129
throw MapboxMapLayerManagerError.layerAlreadyAdded(layerId)
144130
}
145131
guard let layer2 = mapView?.style?.layer(withIdentifier: layerId) else {
146-
return try addOnTop(layer: layer)
132+
try addOnTop(layer: layer)
133+
return
147134
}
148135
mapView?.style?.insertLayer(layer, below: layer2)
149136
}
@@ -153,7 +140,8 @@ extension MapboxMapLayerManager {
153140
throw MapboxMapLayerManagerError.layerAlreadyAdded(layerId)
154141
}
155142
guard let layer2 = mapView?.style?.layer(withIdentifier: layerId) else {
156-
return try addOnTop(layer: layer)
143+
try addOnTop(layer: layer)
144+
return
157145
}
158146
mapView?.style?.insertLayer(layer, above: layer2)
159147
}
@@ -163,7 +151,8 @@ extension MapboxMapLayerManager {
163151
return
164152
}
165153
guard (0 ... (mapView?.style?.layers.count ?? 0)).contains(Int(index)) else {
166-
return try addOnTop(layer: layer)
154+
try addOnTop(layer: layer)
155+
return
167156
}
168157
mapView?.style?.insertLayer(layer, at: index)
169158
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Foundation
2+
3+
public enum MapboxMapLayerManagerError: LocalizedError {
4+
case layerAlreadyAdded(String)
5+
case sourceUnavailable(String)
6+
7+
// MARK: Internal
8+
9+
public var errorDescription: String? {
10+
switch self {
11+
case let .layerAlreadyAdded(layerId): return "Already added layer with id `\(layerId)`."
12+
case let .sourceUnavailable(sourceId): return "Source with id `\(sourceId)` is not added to the style."
13+
}
14+
}
15+
}

Sources/GMMapUtility/MapLibre/Map/Camera/MGLMapViewCameraController.swift

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import Mapbox
22

33
public class MGLMapViewCameraController: MapViewCameraController {
4+
private enum Constants {
5+
public static let maxDeltaLatitudeEquality = 0.0001
6+
public static let incrementWorkaroundZoomStop = 0.01
7+
public static let cameraAltitudeIncrementZoomNorth: Double = 1_000
8+
public static let fallbackCameraViewDistance: Double = 500
9+
public static let cameraAltitudeFactor: Double = 0.75
10+
public static let mapViewCameraAltitudeFactor: Double = 0.25
11+
public static let defaultCameraMovementDuration: Double = 0.5
12+
public static let mapViewFlyDuration: Double = 1
13+
}
414
// MARK: - Attributes
515

616
private weak var mapView: MGLMapView?
@@ -28,7 +38,7 @@ public class MGLMapViewCameraController: MapViewCameraController {
2838
return false
2939
}
3040
return abs(mapView.camera.heading) == 0
31-
&& abs(location.coordinate.latitude - mapView.centerCoordinate.latitude) < 0.0001
41+
&& abs(location.coordinate.latitude - mapView.centerCoordinate.latitude) < Constants.maxDeltaLatitudeEquality
3242
}
3343

3444
public func set(to location: CLLocationCoordinate2D, heading: Double?, completion: (() -> Void)?) {
@@ -59,7 +69,15 @@ public class MGLMapViewCameraController: MapViewCameraController {
5969
zoomLevel: Double?,
6070
completion: (() -> Void)?
6171
) {
62-
mapView?.fly(to: camera(from: location, heading: heading, zoom: zoomLevel), withDuration: 1, completionHandler: completion)
72+
mapView?.fly(
73+
to: camera(
74+
from: location,
75+
heading: heading,
76+
zoom: zoomLevel
77+
),
78+
withDuration: Constants.mapViewFlyDuration,
79+
completionHandler: completion
80+
)
6381
}
6482

6583
public func showNorth(animated _: Bool, completion: (() -> Void)?) {
@@ -70,7 +88,7 @@ public class MGLMapViewCameraController: MapViewCameraController {
7088
let camera = mapView.camera
7189
camera.heading = .zero
7290
camera.pitch = .zero
73-
camera.altitude += 1000
91+
camera.altitude += Constants.cameraAltitudeIncrementZoomNorth
7492
set(
7593
camera: camera,
7694
animationTimingFunction: CAMediaTimingFunction(name: .easeOut),
@@ -83,11 +101,16 @@ public class MGLMapViewCameraController: MapViewCameraController {
83101
guard let bbox = locations.filter({ CLLocationCoordinate2DIsValid($0) }).boundingBox else {
84102
return
85103
}
86-
showBoundingBox(northEast: bbox.northEast, southWest: bbox.southWest, animated: animated, completion: completion)
104+
showBoundingBox(
105+
northEast: bbox.northEast,
106+
southWest: bbox.southWest,
107+
animated: animated,
108+
completion: completion
109+
)
87110
}
88111

89112
public func cancelCameraUpdates() {
90-
mapView?.zoomLevel += 0.01
113+
mapView?.zoomLevel += Constants.incrementWorkaroundZoomStop
91114
}
92115

93116
public func zoomIn(animated _: Bool, completion: (() -> Void)?) {
@@ -194,7 +217,7 @@ public class MGLMapViewCameraController: MapViewCameraController {
194217
zoom: zoom ?? defaultZoom,
195218
pitch: pitch ?? defaultPitch,
196219
latitude: location.latitude
197-
) ?? 500,
220+
) ?? Constants.fallbackCameraViewDistance,
198221
pitch: pitch ?? defaultPitch,
199222
heading: heading ?? .zero
200223
)
@@ -228,13 +251,16 @@ public class MGLMapViewCameraController: MapViewCameraController {
228251
longitude: camera.centerCoordinate.longitude
229252
))
230253

231-
let altitude = (0.75 * camera.altitude + 0.25 * mapView.camera.altitude)
254+
let altitude = (
255+
Constants.cameraAltitudeFactor * camera.altitude
256+
+ Constants.mapViewCameraAltitudeFactor * mapView.camera.altitude
257+
)
232258

233-
guard distance < altitude * 2 else {
259+
guard distance < altitude * 2 else { // swiftlint:disable:this no_magic_numbers
234260
return .zero
235261
}
236262

237-
return defaultDuration ?? max(0.5, distance / altitude)
263+
return defaultDuration ?? max(Constants.defaultCameraMovementDuration, distance / altitude)
238264
}
239265

240266
private func showBoundingBox(
@@ -258,7 +284,9 @@ public class MGLMapViewCameraController: MapViewCameraController {
258284
)
259285
let baseCamera = MGLMapCamera(
260286
lookingAtCenter: [northEast, southWest].geographicMidpoint(),
261-
acrossDistance: .zero, pitch: .zero, heading: .zero
287+
acrossDistance: .zero,
288+
pitch: .zero,
289+
heading: .zero
262290
)
263291
let camera = mapView.camera(
264292
baseCamera,

Sources/GMMapUtility/MapLibre/Style/Layer/DashedLineLayer.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ open class DashedLineLayer: MGLLineStyleLayer {
1111
applyDefaultStyle(to: self, lineColor: lineColor)
1212
}
1313

14+
// swiftlint:disable no_magic_numbers
1415
private func applyDefaultStyle(to layer: MGLLineStyleLayer, lineColor: UIColor) {
1516
layer.minimumZoomLevel = Constants.defaultZoomLevel
1617
layer.lineCap = NSExpression(forConstantValue: "round")
@@ -27,4 +28,5 @@ open class DashedLineLayer: MGLLineStyleLayer {
2728
])
2829
)
2930
}
31+
// swiftlint:enable no_magic_numbers
3032
}

0 commit comments

Comments
 (0)