diff --git a/Sources/MapLibreSwiftUI/Extensions/MapLibre/MLNMapViewCameraUpdating.swift b/Sources/MapLibreSwiftUI/Extensions/MapLibre/MLNMapViewCameraUpdating.swift index a2c3e2e..d46e238 100644 --- a/Sources/MapLibreSwiftUI/Extensions/MapLibre/MLNMapViewCameraUpdating.swift +++ b/Sources/MapLibreSwiftUI/Extensions/MapLibre/MLNMapViewCameraUpdating.swift @@ -13,7 +13,12 @@ protocol MLNMapViewCameraUpdating: AnyObject { direction: CLLocationDirection, animated: Bool) func setZoomLevel(_ zoomLevel: Double, animated: Bool) - func setVisibleCoordinateBounds(_ bounds: MLNCoordinateBounds, edgePadding: UIEdgeInsets, animated: Bool, completionHandler: (() -> Void)?) + func setVisibleCoordinateBounds( + _ bounds: MLNCoordinateBounds, + edgePadding: UIEdgeInsets, + animated: Bool, + completionHandler: (() -> Void)? + ) } extension MLNMapView: MLNMapViewCameraUpdating { diff --git a/Sources/MapLibreSwiftUI/MapViewCoordinator.swift b/Sources/MapLibreSwiftUI/MapViewCoordinator.swift index f35d241..a07963c 100644 --- a/Sources/MapLibreSwiftUI/MapViewCoordinator.swift +++ b/Sources/MapLibreSwiftUI/MapViewCoordinator.swift @@ -63,11 +63,11 @@ public class MapViewCoordinator: NSObject { case .trackingUserLocationWithCourse: mapView.userTrackingMode = .followWithCourse mapView.setZoomLevel(camera.zoom, animated: false) - case let .rect(boundingBox, padding): - mapView.setVisibleCoordinateBounds(boundingBox, - edgePadding: padding, - animated: animated, - completionHandler: nil) + case let .rect(boundingBox, padding): + mapView.setVisibleCoordinateBounds(boundingBox, + edgePadding: padding, + animated: animated, + completionHandler: nil) case .showcase: // TODO: Need a method these/or to finalize a goal here. break diff --git a/Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift b/Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift index d19929a..e600626 100644 --- a/Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift +++ b/Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift @@ -25,7 +25,10 @@ public enum CameraState: Hashable { case trackingUserLocationWithCourse /// Centered on a bounding box/rectangle. - case rect(boundingBox: MLNCoordinateBounds, edgePadding: UIEdgeInsets = .init(top: 20, left: 20, bottom: 20, right: 20)) + case rect( + boundingBox: MLNCoordinateBounds, + edgePadding: UIEdgeInsets = .init(top: 20, left: 20, bottom: 20, right: 20) + ) /// Showcasing GeoJSON, Polygons, etc. case showcase(shapeCollection: MLNShapeCollection) @@ -42,32 +45,30 @@ extension CameraState: CustomDebugStringConvertible { "CameraState.trackingUserLocationWithHeading" case .trackingUserLocationWithCourse: "CameraState.trackingUserLocationWithCourse" - case .rect(boundingBox: let boundingBox, _): - "CameraState.rect(northeast: \(boundingBox.ne), southwest: \(boundingBox.sw))" - case .showcase(shapeCollection: let shapeCollection): + case let .rect(boundingBox: boundingBox, _): + "CameraState.rect(northeast: \(boundingBox.ne), southwest: \(boundingBox.sw))" + case let .showcase(shapeCollection: shapeCollection): "CameraState.showcase(shapeCollection: \(shapeCollection))" } } } extension MLNCoordinateBounds: Equatable, Hashable { - - public func hash(into hasher: inout Hasher) { - hasher.combine(self.ne) - hasher.combine(self.sw) - } - - public static func == (lhs: MLNCoordinateBounds, rhs: MLNCoordinateBounds) -> Bool { - return lhs.ne == rhs.ne && lhs.sw == rhs.sw - } + public func hash(into hasher: inout Hasher) { + hasher.combine(ne) + hasher.combine(sw) + } + + public static func == (lhs: MLNCoordinateBounds, rhs: MLNCoordinateBounds) -> Bool { + lhs.ne == rhs.ne && lhs.sw == rhs.sw + } } extension UIEdgeInsets: Hashable { - - public func hash(into hasher: inout Hasher) { - hasher.combine(self.left) - hasher.combine(self.right) - hasher.combine(self.top) - hasher.combine(self.bottom) - } + public func hash(into hasher: inout Hasher) { + hasher.combine(left) + hasher.combine(right) + hasher.combine(top) + hasher.combine(bottom) + } } diff --git a/Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift b/Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift index ffd9729..fee9715 100644 --- a/Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift +++ b/Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift @@ -117,19 +117,22 @@ public struct MapViewCamera: Hashable { direction: Defaults.direction, lastReasonForChange: .programmatic) } - - /// Positions the camera to show a specific region in the MapView. - /// - /// - Parameters: - /// - box: Set the desired bounding box. This is a one time event and the user can manipulate by moving the map. - /// - edgePadding: Set the edge insets that should be applied before positioning the map. - /// - Returns: The MapViewCamera representing the scenario - public static func boundingBox(_ box: MLNCoordinateBounds, edgePadding: UIEdgeInsets = .init(top: 20, left: 20, bottom: 20, right: 20)) -> MapViewCamera { - // zoom, pitch & direction are ignored. - return MapViewCamera(state: .rect(boundingBox: box, edgePadding: edgePadding), - zoom: 1, - pitch: Defaults.pitch, - direction: Defaults.direction, - lastReasonForChange: .programmatic) - } + + /// Positions the camera to show a specific region in the MapView. + /// + /// - Parameters: + /// - box: Set the desired bounding box. This is a one time event and the user can manipulate by moving the map. + /// - edgePadding: Set the edge insets that should be applied before positioning the map. + /// - Returns: The MapViewCamera representing the scenario + public static func boundingBox( + _ box: MLNCoordinateBounds, + edgePadding: UIEdgeInsets = .init(top: 20, left: 20, bottom: 20, right: 20) + ) -> MapViewCamera { + // zoom, pitch & direction are ignored. + MapViewCamera(state: .rect(boundingBox: box, edgePadding: edgePadding), + zoom: 1, + pitch: Defaults.pitch, + direction: Defaults.direction, + lastReasonForChange: .programmatic) + } } diff --git a/Tests/MapLibreSwiftUITests/Models/MapCamera/CameraStateTests.swift b/Tests/MapLibreSwiftUITests/Models/MapCamera/CameraStateTests.swift index 099d87b..6dad5c9 100644 --- a/Tests/MapLibreSwiftUITests/Models/MapCamera/CameraStateTests.swift +++ b/Tests/MapLibreSwiftUITests/Models/MapCamera/CameraStateTests.swift @@ -34,9 +34,9 @@ final class CameraStateTests: XCTestCase { func testRect() { let northeast = CLLocationCoordinate2D(latitude: 12.3, longitude: 23.4) let southwest = CLLocationCoordinate2D(latitude: 34.5, longitude: 45.6) - - let state: CameraState = .rect(boundingBox: .init(sw: southwest, ne: northeast)) - XCTAssertEqual(state, .rect(boundingBox: .init(sw: southwest, ne: northeast))) + + let state: CameraState = .rect(boundingBox: .init(sw: southwest, ne: northeast)) + XCTAssertEqual(state, .rect(boundingBox: .init(sw: southwest, ne: northeast))) XCTAssertEqual( String(describing: state), diff --git a/Tests/MapLibreSwiftUITests/Models/MapCamera/MapViewCameraTests.swift b/Tests/MapLibreSwiftUITests/Models/MapCamera/MapViewCameraTests.swift index 056e73d..0b27f62 100644 --- a/Tests/MapLibreSwiftUITests/Models/MapCamera/MapViewCameraTests.swift +++ b/Tests/MapLibreSwiftUITests/Models/MapCamera/MapViewCameraTests.swift @@ -45,18 +45,21 @@ final class MapViewCameraTests: XCTestCase { XCTAssertEqual(camera.pitch, .free) XCTAssertEqual(camera.direction, 0) } - - func testBoundingBox() { - let southwest = CLLocationCoordinate2D(latitude: 24.6056011, longitude: 46.67369842529297) - let northeast = CLLocationCoordinate2D(latitude: 24.6993808, longitude: 46.7709285) - let bounds = MLNCoordinateBounds(sw: southwest, ne: northeast) - let camera = MapViewCamera.boundingBox(bounds) - - XCTAssertEqual(camera.state, .rect(boundingBox: bounds, edgePadding: .init(top: 20, left: 20, bottom: 20, right: 20))) - XCTAssertEqual(camera.zoom, 1) - XCTAssertEqual(camera.pitch, .free) - XCTAssertEqual(camera.direction, 0) - } + + func testBoundingBox() { + let southwest = CLLocationCoordinate2D(latitude: 24.6056011, longitude: 46.67369842529297) + let northeast = CLLocationCoordinate2D(latitude: 24.6993808, longitude: 46.7709285) + let bounds = MLNCoordinateBounds(sw: southwest, ne: northeast) + let camera = MapViewCamera.boundingBox(bounds) + + XCTAssertEqual( + camera.state, + .rect(boundingBox: bounds, edgePadding: .init(top: 20, left: 20, bottom: 20, right: 20)) + ) + XCTAssertEqual(camera.zoom, 1) + XCTAssertEqual(camera.pitch, .free) + XCTAssertEqual(camera.direction, 0) + } // TODO: Add additional camera tests once behaviors are added (e.g. rect) }