/
CLLocation.swift
36 lines (32 loc) 路 1.24 KB
/
CLLocation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import CoreLocation
import Combine
public extension CLLocation {
/// `CLLocation` from `CLLocationCoordinate2D`
/// - Parameter coordinate: `CLLocationCoordinate2D`
convenience init(from coordinate: CLLocationCoordinate2D) {
self.init(latitude: coordinate.latitude, longitude: coordinate.longitude)
}
/// Geocode location `Error`
enum GeocodeError: Error {
case invalid(String)
case empty(String)
}
/// Reverse geocode a `CLLocation`
/// - Parameter location: `CLLocation`
/// - Returns: Future with Result<[`CLPlacemark`], `GeocodeError`>
func reverseGeocode() -> Deferred<Future<[CLPlacemark], GeocodeError>> {
Deferred {
Future { promise in
CLGeocoder().reverseGeocodeLocation(self) { placemarks, error -> Void in
if let err = error {
return promise(.failure(.invalid("\(String(describing: err))")))
}
guard let marks = placemarks, !marks.isEmpty else {
return promise(.failure(.empty("\(String(describing: placemarks))")))
}
return promise(.success(marks))
}
}
}
}
}