Skip to content

Commit

Permalink
Added setters for camera parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Archdoog committed Apr 2, 2024
1 parent d330665 commit cc5fac1
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ protocol MLNMapViewCameraUpdating: AnyObject {
@MainActor var userTrackingMode: MLNUserTrackingMode { get set }
@MainActor var minimumPitch: CGFloat { get set }
@MainActor var maximumPitch: CGFloat { get set }
@MainActor var direction: CLLocationDirection { get set }
@MainActor func setCenter(_ coordinate: CLLocationCoordinate2D,
zoomLevel: Double,
direction: CLLocationDirection,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import Foundation

extension MapViewCamera {

// MARK: Zoom

/// Set a new zoom for the current camera state.
///
/// - Parameter newZoom: The new zoom value.
public mutating func setZoom(_ newZoom: Double) {
switch self.state {
case .centered(let onCoordinate, _, let pitch, let direction):
self.state = .centered(onCoordinate: onCoordinate,
zoom: newZoom,
pitch: pitch,
direction: direction)
case .trackingUserLocation(_, let pitch, let direction):
self.state = .trackingUserLocation(zoom: newZoom, pitch: pitch, direction: direction)
case .trackingUserLocationWithHeading(_, let pitch):
self.state = .trackingUserLocationWithHeading(zoom: newZoom, pitch: pitch)
case .trackingUserLocationWithCourse(_, let pitch):
self.state = .trackingUserLocationWithCourse(zoom: newZoom, pitch: pitch)
case .rect(let boundingBox, let edgePadding):

Check warning on line 23 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'boundingBox' was never used; consider replacing with '_' or removing it

Check warning on line 23 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'edgePadding' was never used; consider replacing with '_' or removing it
return
case .showcase(let shapeCollection):

Check warning on line 25 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'shapeCollection' was never used; consider replacing with '_' or removing it
return
}

self.lastReasonForChange = .programmatic
}

/// Increment the zoom of the current camera state.
///
/// - Parameter newZoom: The value to increment the zoom by. Negative decrements the value.
public mutating func incrementZoom(by increment: Double) {
switch self.state {
case .centered(let onCoordinate, let zoom, let pitch, let direction):
self.state = .centered(onCoordinate: onCoordinate,
zoom: zoom + increment,
pitch: pitch,
direction: direction)
case .trackingUserLocation(let zoom, let pitch, let direction):
self.state = .trackingUserLocation(zoom: zoom + increment, pitch: pitch, direction: direction)
case .trackingUserLocationWithHeading(let zoom, let pitch):
self.state = .trackingUserLocationWithHeading(zoom: zoom + increment, pitch: pitch)
case .trackingUserLocationWithCourse(let zoom, let pitch):
self.state = .trackingUserLocationWithCourse(zoom: zoom + increment, pitch: pitch)
case .rect(let boundingBox, let edgePadding):

Check warning on line 48 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'boundingBox' was never used; consider replacing with '_' or removing it

Check warning on line 48 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'edgePadding' was never used; consider replacing with '_' or removing it
return
case .showcase(let shapeCollection):

Check warning on line 50 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'shapeCollection' was never used; consider replacing with '_' or removing it
return
}

self.lastReasonForChange = .programmatic
}

// MARK: Pitch

/// Set a new pitch for the current camera state.
///
/// - Parameter newPitch: The new pitch value.
public mutating func setPitch(_ newPitch: CameraPitch) {
switch self.state {
case .centered(let onCoordinate, let zoom, let pitch, let direction):

Check warning on line 64 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'pitch' was never used; consider replacing with '_' or removing it
self.state = .centered(onCoordinate: onCoordinate,
zoom: zoom,
pitch: newPitch,
direction: direction)
case .trackingUserLocation(let zoom, _, let direction):
self.state = .trackingUserLocation(zoom: zoom, pitch: newPitch, direction: direction)
case .trackingUserLocationWithHeading(let zoom, _):
self.state = .trackingUserLocationWithHeading(zoom: zoom, pitch: newPitch)
case .trackingUserLocationWithCourse(let zoom, _):
self.state = .trackingUserLocationWithCourse(zoom: zoom, pitch: newPitch)
case .rect(let boundingBox, let edgePadding):

Check warning on line 75 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'boundingBox' was never used; consider replacing with '_' or removing it

Check warning on line 75 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'edgePadding' was never used; consider replacing with '_' or removing it
return
case .showcase(let shapeCollection):

Check warning on line 77 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'shapeCollection' was never used; consider replacing with '_' or removing it
return
}

self.lastReasonForChange = .programmatic
}

// TODO: Add direction set

}
3 changes: 2 additions & 1 deletion Sources/MapLibreSwiftUI/MapViewCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ public class MapViewCoordinator: NSObject {
animated: animated)
mapView.minimumPitch = pitch.rangeValue.lowerBound
mapView.maximumPitch = pitch.rangeValue.upperBound
case let .trackingUserLocation(zoom: zoom, pitch: pitch):
case let .trackingUserLocation(zoom: zoom, pitch: pitch, direction: direction):
mapView.userTrackingMode = .follow
// Needs to be non-animated or else it messes up following
mapView.setZoomLevel(zoom, animated: false)
mapView.direction = direction
mapView.minimumPitch = pitch.rangeValue.lowerBound
mapView.maximumPitch = pitch.rangeValue.upperBound
case let .trackingUserLocationWithHeading(zoom: zoom, pitch: pitch):
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum CameraState: Hashable {
///
/// This feature uses the MLNMapView's userTrackingMode to .follow which automatically
/// follows the user from within the MLNMapView.
case trackingUserLocation(zoom: Double, pitch: CameraPitch)
case trackingUserLocation(zoom: Double, pitch: CameraPitch, direction: CLLocationDirection)

/// Follow the user's location using the MapView's internal camera with the user's heading.
///
Expand Down
5 changes: 3 additions & 2 deletions Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ public struct MapViewCamera: Hashable {
/// - pitch: Set the camera pitch method.
/// - Returns: The MapViewCamera representing the scenario
public static func trackUserLocation(zoom: Double = Defaults.zoom,
pitch: CameraPitch = Defaults.pitch) -> MapViewCamera
pitch: CameraPitch = Defaults.pitch,
direction: CLLocationDirection = Defaults.direction) -> MapViewCamera
{
// Coordinate is ignored when tracking user location. However, pitch and zoom are valid.
MapViewCamera(state: .trackingUserLocation(zoom: zoom, pitch: pitch),
MapViewCamera(state: .trackingUserLocation(zoom: zoom, pitch: pitch, direction: direction),
lastReasonForChange: .programmatic)
}

Expand Down

0 comments on commit cc5fac1

Please sign in to comment.