Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func geocode(latitude: Double, longitude: Double, completion: @escaping (CLPlacemark?, Error?) -> ()) {
- CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude)) { completion($0?.first, $1) }
- }
- geocode(latitude: -22.963451, longitude: -43.198242) { placemark, error in
- guard let placemark = placemark, error == nil else { return }
- // you should always update your UI in the main thread
- DispatchQueue.main.async {
- // update UI here
- print("address1:", placemark.thoroughfare ?? "")
- print("address2:", placemark.subThoroughfare ?? "")
- print("city:", placemark.locality ?? "")
- print("state:", placemark.administrativeArea ?? "")
- print("zip code:", placemark.postalCode ?? "")
- print("country:", placemark.country ?? "")
- }
- }
- # Gets latitude, longitude and a completion block
- func geocode(latitude: Double, longitude: Double, completion: @escaping (CLPlacemark?, Error?) -> ())
- {
- # Calls system function to resolve the coordinates
- CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude))
- {
- # $0 is the placemarks array so this returns the first value of the placeholder array
- # $1 is the error
- completion($0?.first, $1)
- }
- }
- typealias CLGeocodeCompletionHandler = ([CLPlacemark]?, Error?) -> Void
Add Comment
Please, Sign In to add comment