Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Address {
- let shortAddress: String
- let placeName: String
- let completeAddress: String
- let headerAddress: String
- }
- extension CLLocationCoordinate2D {
- func getLocationName(callback: @escaping (_ address: Address) -> Void) {
- // Add below code to get address for touch coordinates.
- let geoCoder = CLGeocoder()
- let location = CLLocation(latitude: latitude, longitude: longitude)
- var shortAddress: String = "" // (PIN) Street name (thoroughfare) number (sub thoroughfare), locality
- var placeName: String = "" //Name
- var completeAddress: String = "" //thoroughfare subthoroughfare, postal code, locality, country
- var headerAddress: String = "" //thoroughfare subthoroughfare, postal code, locality, country
- geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, _) -> Void in
- // Place details
- var placeMark: CLPlacemark!
- guard let placemarks = placemarks else { return }
- if placemarks.count == 0 {
- return
- }
- placeMark = placemarks[0]
- // Short Address
- shortAddress = placeMark.name ?? ""
- var shortAddressComponents: [String] = []
- var streetComponents: [String] = []
- if let thoroughfare = placeMark.thoroughfare, !thoroughfare.isEmpty {
- streetComponents.append(thoroughfare)
- }
- if let subThoroughfare = placeMark.subThoroughfare, !subThoroughfare.isEmpty {
- streetComponents.append(subThoroughfare)
- }
- let streetComponentsJoined = streetComponents.joined(separator: " ")
- if !streetComponentsJoined.isEmpty {
- shortAddressComponents.append(streetComponentsJoined)
- }
- if let locality = placeMark.locality, !locality.isEmpty {
- shortAddressComponents.append(locality)
- }
- let shortAddressJoined = shortAddressComponents.joined(separator: ", ")
- if !shortAddressJoined.isEmpty {
- shortAddress = shortAddressJoined
- }
- // Place Name
- placeName = placeMark.name ?? shortAddress
- // Complete Address
- var addressComponentsList: [String] = []
- var addressComponentsListWOCountry: String = ""
- //// Street
- let streetAndNumber = streetComponents.joined(separator: " ")
- if !streetAndNumber.isEmpty {
- addressComponentsList.append(streetAndNumber)
- }
- //// Postal Code and Locality
- var postalCodeAndLocalityList: [String] = []
- var postalCodeAndLocality: String = ""
- if let postalCode = placeMark.postalCode, !postalCode.isEmpty {
- postalCodeAndLocalityList.append(postalCode)
- }
- if let locality = placeMark.locality, !locality.isEmpty {
- postalCodeAndLocalityList.append(locality)
- }
- postalCodeAndLocality = postalCodeAndLocalityList.joined(separator: " ")
- if !postalCodeAndLocality.isEmpty {
- addressComponentsList.append(postalCodeAndLocality)
- }
- //// Country
- addressComponentsListWOCountry = addressComponentsList.joined(separator: ", ") //Used for the header title
- if let country = placeMark.country, !country.isEmpty {
- addressComponentsList.append(country)
- }
- completeAddress = addressComponentsList.joined(separator: ", ")
- // Header Address
- if placeName.isContentEqual(to: streetAndNumber) {
- headerAddress = postalCodeAndLocality
- } else if streetAndNumber.isEmpty {
- if postalCodeAndLocality.isEmpty {
- if let country = placeMark.country, !country.isEmpty {
- headerAddress = "\(placeName), \(country)"
- } else {
- headerAddress = placeName
- }
- } else {
- headerAddress = "\(placeName), \(postalCodeAndLocality)"
- }
- } else {
- headerAddress = addressComponentsListWOCountry
- }
- callback(Address(shortAddress: shortAddress, placeName: placeName, completeAddress: completeAddress, headerAddress: headerAddress))
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement