forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeocoding.swift
86 lines (79 loc) · 2.71 KB
/
Geocoding.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Foundation
import CoreLocation
import SwiftSignalKit
public func geocodeLocation(address: String) -> Signal<[CLPlacemark]?, NoError> {
return Signal { subscriber in
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, _) in
subscriber.putNext(placemarks)
subscriber.putCompletion()
}
return ActionDisposable {
geocoder.cancelGeocode()
}
}
}
public func geocodeLocation(dictionary: [String: String]) -> Signal<(Double, Double)?, NoError> {
return Signal { subscriber in
let geocoder = CLGeocoder()
geocoder.geocodeAddressDictionary(dictionary, completionHandler: { placemarks, _ in
if let location = placemarks?.first?.location {
subscriber.putNext((location.coordinate.latitude, location.coordinate.longitude))
} else {
subscriber.putNext(nil)
}
subscriber.putCompletion()
})
return ActionDisposable {
geocoder.cancelGeocode()
}
}
}
public struct ReverseGeocodedPlacemark {
public let street: String?
public let city: String?
public let country: String?
public var compactDisplayAddress: String? {
if let street = self.street {
return street
}
if let city = self.city {
return city
}
if let country = self.country {
return country
}
return nil
}
public var fullAddress: String {
var components: [String] = []
if let street = self.street {
components.append(street)
}
if let city = self.city {
components.append(city)
}
if let country = self.country {
components.append(country)
}
return components.joined(separator: ", ")
}
}
public func reverseGeocodeLocation(latitude: Double, longitude: Double) -> Signal<ReverseGeocodedPlacemark?, NoError> {
return Signal { subscriber in
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude), completionHandler: { placemarks, _ in
if let placemarks = placemarks, let placemark = placemarks.first {
let result = ReverseGeocodedPlacemark(street: placemark.thoroughfare, city: placemark.locality, country: placemark.country)
subscriber.putNext(result)
subscriber.putCompletion()
} else {
subscriber.putNext(nil)
subscriber.putCompletion()
}
})
return ActionDisposable {
geocoder.cancelGeocode()
}
}
}