Skip to content

Commit 546e912

Browse files
authored
Merge pull request #31 from Rallista/feat/camera-setters-1
Added setters for camera parameters
2 parents d330665 + e85c300 commit 546e912

File tree

8 files changed

+96
-8
lines changed

8 files changed

+96
-8
lines changed

Sources/MapLibreSwiftUI/Extensions/MapLibre/MLNMapViewCameraUpdating.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ protocol MLNMapViewCameraUpdating: AnyObject {
99
@MainActor var userTrackingMode: MLNUserTrackingMode { get set }
1010
@MainActor var minimumPitch: CGFloat { get set }
1111
@MainActor var maximumPitch: CGFloat { get set }
12+
@MainActor var direction: CLLocationDirection { get set }
1213
@MainActor func setCenter(_ coordinate: CLLocationCoordinate2D,
1314
zoomLevel: Double,
1415
direction: CLLocationDirection,
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import Foundation
2+
3+
public extension MapViewCamera {
4+
// MARK: Zoom
5+
6+
/// Set a new zoom for the current camera state.
7+
///
8+
/// - Parameter newZoom: The new zoom value.
9+
mutating func setZoom(_ newZoom: Double) {
10+
switch state {
11+
case let .centered(onCoordinate, _, pitch, direction):
12+
state = .centered(onCoordinate: onCoordinate,
13+
zoom: newZoom,
14+
pitch: pitch,
15+
direction: direction)
16+
case let .trackingUserLocation(_, pitch, direction):
17+
state = .trackingUserLocation(zoom: newZoom, pitch: pitch, direction: direction)
18+
case let .trackingUserLocationWithHeading(_, pitch):
19+
state = .trackingUserLocationWithHeading(zoom: newZoom, pitch: pitch)
20+
case let .trackingUserLocationWithCourse(_, pitch):
21+
state = .trackingUserLocationWithCourse(zoom: newZoom, pitch: pitch)
22+
case let .rect(boundingBox, edgePadding):
23+
return
24+
case let .showcase(shapeCollection):
25+
return
26+
}
27+
28+
lastReasonForChange = .programmatic
29+
}
30+
31+
/// Increment the zoom of the current camera state.
32+
///
33+
/// - Parameter newZoom: The value to increment the zoom by. Negative decrements the value.
34+
mutating func incrementZoom(by increment: Double) {
35+
switch state {
36+
case let .centered(onCoordinate, zoom, pitch, direction):
37+
state = .centered(onCoordinate: onCoordinate,
38+
zoom: zoom + increment,
39+
pitch: pitch,
40+
direction: direction)
41+
case let .trackingUserLocation(zoom, pitch, direction):
42+
state = .trackingUserLocation(zoom: zoom + increment, pitch: pitch, direction: direction)
43+
case let .trackingUserLocationWithHeading(zoom, pitch):
44+
state = .trackingUserLocationWithHeading(zoom: zoom + increment, pitch: pitch)
45+
case let .trackingUserLocationWithCourse(zoom, pitch):
46+
state = .trackingUserLocationWithCourse(zoom: zoom + increment, pitch: pitch)
47+
case let .rect(boundingBox, edgePadding):
48+
return
49+
case let .showcase(shapeCollection):
50+
return
51+
}
52+
53+
lastReasonForChange = .programmatic
54+
}
55+
56+
// MARK: Pitch
57+
58+
/// Set a new pitch for the current camera state.
59+
///
60+
/// - Parameter newPitch: The new pitch value.
61+
mutating func setPitch(_ newPitch: CameraPitch) {
62+
switch state {
63+
case let .centered(onCoordinate, zoom, pitch, direction):
64+
state = .centered(onCoordinate: onCoordinate,
65+
zoom: zoom,
66+
pitch: newPitch,
67+
direction: direction)
68+
case let .trackingUserLocation(zoom, _, direction):
69+
state = .trackingUserLocation(zoom: zoom, pitch: newPitch, direction: direction)
70+
case let .trackingUserLocationWithHeading(zoom, _):
71+
state = .trackingUserLocationWithHeading(zoom: zoom, pitch: newPitch)
72+
case let .trackingUserLocationWithCourse(zoom, _):
73+
state = .trackingUserLocationWithCourse(zoom: zoom, pitch: newPitch)
74+
case let .rect(boundingBox, edgePadding):
75+
return
76+
case let .showcase(shapeCollection):
77+
return
78+
}
79+
80+
lastReasonForChange = .programmatic
81+
}
82+
83+
// TODO: Add direction set
84+
}

Sources/MapLibreSwiftUI/MapViewCoordinator.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ public class MapViewCoordinator: NSObject {
6767
animated: animated)
6868
mapView.minimumPitch = pitch.rangeValue.lowerBound
6969
mapView.maximumPitch = pitch.rangeValue.upperBound
70-
case let .trackingUserLocation(zoom: zoom, pitch: pitch):
70+
case let .trackingUserLocation(zoom: zoom, pitch: pitch, direction: direction):
7171
mapView.userTrackingMode = .follow
7272
// Needs to be non-animated or else it messes up following
7373
mapView.setZoomLevel(zoom, animated: false)
74+
mapView.direction = direction
7475
mapView.minimumPitch = pitch.rangeValue.lowerBound
7576
mapView.maximumPitch = pitch.rangeValue.upperBound
7677
case let .trackingUserLocationWithHeading(zoom: zoom, pitch: pitch):

Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public enum CameraState: Hashable {
1515
///
1616
/// This feature uses the MLNMapView's userTrackingMode to .follow which automatically
1717
/// follows the user from within the MLNMapView.
18-
case trackingUserLocation(zoom: Double, pitch: CameraPitch)
18+
case trackingUserLocation(zoom: Double, pitch: CameraPitch, direction: CLLocationDirection)
1919

2020
/// Follow the user's location using the MapView's internal camera with the user's heading.
2121
///

Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ public struct MapViewCamera: Hashable {
6666
/// - pitch: Set the camera pitch method.
6767
/// - Returns: The MapViewCamera representing the scenario
6868
public static func trackUserLocation(zoom: Double = Defaults.zoom,
69-
pitch: CameraPitch = Defaults.pitch) -> MapViewCamera
69+
pitch: CameraPitch = Defaults.pitch,
70+
direction: CLLocationDirection = Defaults.direction) -> MapViewCamera
7071
{
7172
// Coordinate is ignored when tracking user location. However, pitch and zoom are valid.
72-
MapViewCamera(state: .trackingUserLocation(zoom: zoom, pitch: pitch),
73+
MapViewCamera(state: .trackingUserLocation(zoom: zoom, pitch: pitch, direction: direction),
7374
lastReasonForChange: .programmatic)
7475
}
7576

Tests/MapLibreSwiftUITests/Models/MapCamera/CameraStateTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ final class CameraStateTests: XCTestCase {
1313
}
1414

1515
func testTrackingUserLocation() {
16-
let state: CameraState = .trackingUserLocation(zoom: 4, pitch: .free)
17-
XCTAssertEqual(state, .trackingUserLocation(zoom: 4, pitch: .free))
16+
let state: CameraState = .trackingUserLocation(zoom: 4, pitch: .free, direction: 12)
17+
XCTAssertEqual(state, .trackingUserLocation(zoom: 4, pitch: .free, direction: 12))
1818
assertSnapshot(of: state, as: .description)
1919
}
2020

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CameraState.trackingUserLocation(zoom: (4.0, MapLibreSwiftUI.CameraPitch.free))
1+
CameraState.trackingUserLocation(zoom: (4.0, MapLibreSwiftUI.CameraPitch.free, 12.0))

Tests/MapLibreSwiftUITests/Models/MapCamera/__Snapshots__/MapViewCameraTests/testTrackingUserLocation.1.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
▿ lastReasonForChange: Optional<CameraChangeReason>
33
- some: CameraChangeReason.programmatic
44
▿ state: CameraState
5-
▿ trackingUserLocation: (2 elements)
5+
▿ trackingUserLocation: (3 elements)
66
- zoom: 10.0
77
▿ pitch: CameraPitch
88
▿ freeWithinRange: (2 elements)
99
- minimum: 12.0
1010
- maximum: 34.0
11+
- direction: 0.0

0 commit comments

Comments
 (0)