29 lines
894 B
Swift
29 lines
894 B
Swift
import MapKit
|
|
|
|
@MainActor
|
|
protocol CustomerLocationService {
|
|
func coordinate(for address: String) async throws -> CLLocationCoordinate2D
|
|
}
|
|
|
|
enum CustomerLocationError: Error {
|
|
case notFound
|
|
}
|
|
|
|
@MainActor
|
|
struct AppleMapsCustomerLocationService: CustomerLocationService {
|
|
func coordinate(for address: String) async throws -> CLLocationCoordinate2D {
|
|
guard let request = MKGeocodingRequest(addressString: address) else {
|
|
throw CustomerLocationError.notFound
|
|
}
|
|
let items: [MKMapItem]
|
|
do {
|
|
items = try await request.mapItems
|
|
} catch let error as MKError where error.code == .placemarkNotFound {
|
|
throw CustomerLocationError.notFound
|
|
}
|
|
try Task.checkCancellation()
|
|
guard let item = items.first else { throw CustomerLocationError.notFound }
|
|
return item.location.coordinate
|
|
}
|
|
}
|