Guest User

Untitled

a guest
Dec 14th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. func geocode(latitude: Double, longitude: Double, completion: @escaping (CLPlacemark?, Error?) -> ()) {
  2. CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude)) { completion($0?.first, $1) }
  3. }
  4.  
  5. geocode(latitude: -22.963451, longitude: -43.198242) { placemark, error in
  6. guard let placemark = placemark, error == nil else { return }
  7. // you should always update your UI in the main thread
  8. DispatchQueue.main.async {
  9. // update UI here
  10. print("address1:", placemark.thoroughfare ?? "")
  11. print("address2:", placemark.subThoroughfare ?? "")
  12. print("city:", placemark.locality ?? "")
  13. print("state:", placemark.administrativeArea ?? "")
  14. print("zip code:", placemark.postalCode ?? "")
  15. print("country:", placemark.country ?? "")
  16. }
  17. }
  18.  
  19. # Gets latitude, longitude and a completion block
  20. func geocode(latitude: Double, longitude: Double, completion: @escaping (CLPlacemark?, Error?) -> ())
  21. {
  22. # Calls system function to resolve the coordinates
  23. CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude))
  24. {
  25. # $0 is the placemarks array so this returns the first value of the placeholder array
  26. # $1 is the error
  27. completion($0?.first, $1)
  28.  
  29. }
  30. }
  31.  
  32. typealias CLGeocodeCompletionHandler = ([CLPlacemark]?, Error?) -> Void
Add Comment
Please, Sign In to add comment