Advertisement
luismachado

Untitled

Feb 20th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.51 KB | None | 0 0
  1. struct Address {
  2.     let shortAddress: String
  3.     let placeName: String
  4.     let completeAddress: String
  5.     let headerAddress: String
  6. }
  7.  
  8. extension CLLocationCoordinate2D {
  9.  
  10.     func getLocationName(callback: @escaping (_ address: Address) -> Void) {
  11.         // Add below code to get address for touch coordinates.
  12.  
  13.         let geoCoder = CLGeocoder()
  14.         let location = CLLocation(latitude: latitude, longitude: longitude)
  15.  
  16.         var shortAddress: String = "" // (PIN) Street name (thoroughfare) number (sub thoroughfare), locality
  17.         var placeName: String = "" //Name
  18.         var completeAddress: String = "" //thoroughfare subthoroughfare, postal code, locality, country
  19.         var headerAddress: String = "" //thoroughfare subthoroughfare, postal code, locality, country
  20.  
  21.         geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, _) -> Void in
  22.  
  23.             // Place details
  24.             var placeMark: CLPlacemark!
  25.  
  26.             guard let placemarks = placemarks else { return }
  27.  
  28.             if placemarks.count == 0 {
  29.                 return
  30.             }
  31.  
  32.             placeMark = placemarks[0]
  33.  
  34.             // Short Address
  35.             shortAddress = placeMark.name ?? ""
  36.             var shortAddressComponents: [String] = []
  37.             var streetComponents: [String] = []
  38.             if let thoroughfare = placeMark.thoroughfare, !thoroughfare.isEmpty {
  39.                 streetComponents.append(thoroughfare)
  40.             }
  41.             if let subThoroughfare = placeMark.subThoroughfare, !subThoroughfare.isEmpty {
  42.                 streetComponents.append(subThoroughfare)
  43.             }
  44.  
  45.             let streetComponentsJoined = streetComponents.joined(separator: " ")
  46.             if !streetComponentsJoined.isEmpty {
  47.                 shortAddressComponents.append(streetComponentsJoined)
  48.             }
  49.  
  50.             if let locality = placeMark.locality, !locality.isEmpty {
  51.                 shortAddressComponents.append(locality)
  52.             }
  53.  
  54.             let shortAddressJoined = shortAddressComponents.joined(separator: ", ")
  55.             if !shortAddressJoined.isEmpty {
  56.                 shortAddress = shortAddressJoined
  57.             }
  58.  
  59.             // Place Name
  60.             placeName = placeMark.name ?? shortAddress
  61.  
  62.             // Complete Address
  63.             var addressComponentsList: [String] = []
  64.             var addressComponentsListWOCountry: String = ""
  65.             //// Street
  66.             let streetAndNumber = streetComponents.joined(separator: " ")
  67.             if !streetAndNumber.isEmpty {
  68.                 addressComponentsList.append(streetAndNumber)
  69.             }
  70.             //// Postal Code and Locality
  71.             var postalCodeAndLocalityList: [String] = []
  72.             var postalCodeAndLocality: String = ""
  73.  
  74.             if let postalCode = placeMark.postalCode, !postalCode.isEmpty {
  75.                 postalCodeAndLocalityList.append(postalCode)
  76.             }
  77.             if let locality = placeMark.locality, !locality.isEmpty {
  78.                 postalCodeAndLocalityList.append(locality)
  79.             }
  80.  
  81.             postalCodeAndLocality = postalCodeAndLocalityList.joined(separator: " ")
  82.  
  83.             if !postalCodeAndLocality.isEmpty {
  84.                 addressComponentsList.append(postalCodeAndLocality)
  85.             }
  86.             //// Country
  87.             addressComponentsListWOCountry = addressComponentsList.joined(separator: ", ") //Used for the header title
  88.             if let country = placeMark.country, !country.isEmpty {
  89.                 addressComponentsList.append(country)
  90.             }
  91.             completeAddress = addressComponentsList.joined(separator: ", ")
  92.  
  93.             // Header Address
  94.             if placeName.isContentEqual(to: streetAndNumber) {
  95.                 headerAddress = postalCodeAndLocality
  96.             } else if streetAndNumber.isEmpty {
  97.                 if postalCodeAndLocality.isEmpty {
  98.                     if let country = placeMark.country, !country.isEmpty {
  99.                         headerAddress = "\(placeName), \(country)"
  100.                     } else {
  101.                         headerAddress = placeName
  102.                     }
  103.                 } else {
  104.                     headerAddress = "\(placeName), \(postalCodeAndLocality)"
  105.                 }
  106.             } else {
  107.                 headerAddress = addressComponentsListWOCountry
  108.             }
  109.  
  110.             callback(Address(shortAddress: shortAddress, placeName: placeName, completeAddress: completeAddress, headerAddress: headerAddress))
  111.         })
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement