Skip to content

Commit 42e6907

Browse files
committed
MapViewPort implementation to listen to raw values of 'camera' at any time
1 parent 72f1320 commit 42e6907

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

Sources/MapLibreSwiftUI/MapViewCoordinator.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ extension MapViewCoordinator: MLNMapViewDelegate {
207207
addLayers(to: mglStyle)
208208
onStyleLoaded?(mglStyle)
209209
}
210-
210+
211211
// MARK: MapViewCamera
212-
212+
213213
@MainActor private func updateParentCamera(mapView: MLNMapView, reason: MLNCameraChangeReason) {
214214
// If any of these are a mismatch, we know the camera is no longer following a desired method, so we should
215215
// detach and revert to a .centered camera. If any one of these is true, the desired camera state still
@@ -227,57 +227,57 @@ extension MapViewCoordinator: MLNMapViewDelegate {
227227
case .centered, .rect, .showcase:
228228
false
229229
}
230-
230+
231231
guard !isProgrammaticallyTracking else {
232232
// Programmatic tracking is still active, we can ignore camera updates until we unset/fail this boolean
233233
// check
234234
return
235235
}
236-
236+
237237
// Publish the MLNMapView's "raw" camera state to the MapView camera binding.
238238
// This path only executes when the map view diverges from the parent state, so this is a "matter of fact"
239239
// state propagation.
240240
let newCamera: MapViewCamera = .center(mapView.centerCoordinate,
241241
zoom: mapView.zoomLevel,
242242
// TODO: Pitch doesn't really describe current state
243243
pitch: .freeWithinRange(
244-
minimum: mapView.minimumPitch,
245-
maximum: mapView.maximumPitch
244+
minimum: mapView.minimumPitch,
245+
maximum: mapView.maximumPitch
246246
),
247247
direction: mapView.direction,
248248
reason: CameraChangeReason(reason))
249249
snapshotCamera = newCamera
250250
parent.camera = newCamera
251251
}
252-
252+
253253
/// The MapView's region has changed with a specific reason.
254254
public func mapView(_ mapView: MLNMapView, regionDidChangeWith reason: MLNCameraChangeReason, animated _: Bool) {
255255
// FIXME: CI complains about MainActor.assumeIsolated being unavailable before iOS 17, despite building on iOS 17.2... This is an epic hack to fix it for now. I can only assume this is an issue with Xcode pre-15.3
256256
// TODO: We could put this in regionIsChangingWith if we calculate significant change/debounce.
257257
Task { @MainActor in
258258
updateViewPort(mapView: mapView)
259259
}
260-
260+
261261
guard !suppressCameraUpdatePropagation else {
262262
return
263263
}
264-
264+
265265
// FIXME: CI complains about MainActor.assumeIsolated being unavailable before iOS 17, despite building on iOS 17.2... This is an epic hack to fix it for now. I can only assume this is an issue with Xcode pre-15.3
266266
Task { @MainActor in
267267
updateParentCamera(mapView: mapView, reason: reason)
268268
}
269269
}
270-
270+
271271
// MARK: MapViewPort
272-
272+
273273
@MainActor private func updateViewPort(mapView: MLNMapView) {
274274
// Calculate the Raw "ViewPort"
275275
let calculatedViewPort = MapViewPort(
276276
center: mapView.centerCoordinate,
277277
zoom: mapView.zoomLevel,
278278
direction: mapView.direction
279279
)
280-
280+
281281
onViewPortChanged(calculatedViewPort)
282282
}
283283
}

Sources/MapLibreSwiftUI/MapViewModifiers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public extension MapView {
9999

100100
return result
101101
}
102-
102+
103103
func onMapViewPortUpdate(_ onViewPortChanged: @escaping (MapViewPort) -> Void) -> Self {
104104
var result = self
105105
result.onViewPortChanged = onViewPortChanged
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
1-
import Foundation
21
import CoreLocation
2+
import Foundation
33

44
/// A representation of the MapView's current ViewPort.
55
///
66
/// This includes similar data to the MapViewCamera, but represents the raw
77
/// values associated with the MapView. This information could used to prepare
88
/// a new MapViewCamera on a scene, to cache the camera state, etc.
99
public struct MapViewPort: Hashable, Equatable {
10-
1110
/// The current center coordinate of the MapView
1211
public let center: CLLocationCoordinate2D
13-
12+
1413
/// The current zoom value of the MapView
1514
public let zoom: Double
16-
15+
1716
/// The current compass direction of the MapView
1817
public let direction: CLLocationDirection
19-
18+
2019
public init(center: CLLocationCoordinate2D, zoom: Double, direction: CLLocationDirection) {
2120
self.center = center
2221
self.zoom = zoom
2322
self.direction = direction
2423
}
25-
24+
2625
public static func zero(zoom: Double = 10) -> MapViewPort {
27-
return MapViewPort(center: CLLocationCoordinate2D(latitude: 0, longitude: 0),
28-
zoom: zoom,
29-
direction: 0)
26+
MapViewPort(center: CLLocationCoordinate2D(latitude: 0, longitude: 0),
27+
zoom: zoom,
28+
direction: 0)
3029
}
3130
}
3231

33-
extension MapViewPort {
34-
32+
public extension MapViewPort {
3533
/// Generate a basic MapViewCamera that represents the MapViewPort
3634
///
3735
/// - Returns: The calculated MapViewCamera
38-
public func asMapViewCamera() -> MapViewCamera {
39-
return .center(center,
40-
zoom: zoom,
41-
direction: direction)
36+
func asMapViewCamera() -> MapViewCamera {
37+
.center(center,
38+
zoom: zoom,
39+
direction: direction)
4240
}
4341
}

0 commit comments

Comments
 (0)