diff --git a/Sources/GeodesySpherical/CoreLocationSupport.swift b/Sources/GeodesySpherical/CoreLocationSupport.swift new file mode 100644 index 0000000..eb75205 --- /dev/null +++ b/Sources/GeodesySpherical/CoreLocationSupport.swift @@ -0,0 +1,30 @@ +// +// CoreLocationSupport.swift +// swift-geodesy +// +// Created by Florian Reinhart on 19/06/2024. +// + +#if canImport(CoreLocation) + +import CoreLocation + +public extension Coordinate { + init(_ coordinate: CLLocationCoordinate2D) { + self.init(coordinate.latitude, coordinate.longitude) + } + + init(_ location: CLLocation) { + self.init(location.coordinate) + } + + var clLocationCoordinate2D: CLLocationCoordinate2D { + CLLocationCoordinate2D(latitude: latitude, longitude: longitude) + } + + var clLocation: CLLocation { + CLLocation(latitude: latitude, longitude: longitude) + } +} + +#endif diff --git a/Tests/GeodesySphericalTests/GeodesySphericalTest.swift b/Tests/GeodesySphericalTests/GeodesySphericalTest.swift index 242a9cd..bcda46e 100644 --- a/Tests/GeodesySphericalTests/GeodesySphericalTest.swift +++ b/Tests/GeodesySphericalTests/GeodesySphericalTest.swift @@ -417,4 +417,16 @@ final class GeodesySphericalTest: XCTestCase { XCTAssertNil(distance) } } + +#if canImport(CoreLocation) + func testCoreLocation() { + for coordinate in [cambridge, paris, greenwich, bradwell, dov, cal] { + XCTAssertEqual(coordinate, Coordinate(coordinate.clLocationCoordinate2D)) + XCTAssertEqual(coordinate, Coordinate(coordinate.clLocation)) + XCTAssertEqual(coordinate.clLocationCoordinate2D.latitude, coordinate.clLocation.coordinate.latitude) + XCTAssertEqual(coordinate.clLocationCoordinate2D.longitude, coordinate.clLocation.coordinate.longitude) + } + } +#endif + }