Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Generate an Address Format from Reverse Geocoding

I am trying to generate a Formatted Full address using CLGeocoder in Swift 3. I referred to this SO thread to get the code given below.

However, sometimes the app crashes with a 'nil' error at the line:

//Address dictionary print(placeMark.addressDictionary ?? "") 

Questions:

  1. How can I concatenate these values retrieved from the GeoCoder to form a full address? (Street + City + etc)
  2. How do I handle the nil error I get when the func is unable to find an address?

Full code:

func getAddress() -> String {         var address: String = ""          let geoCoder = CLGeocoder()         let location = CLLocation(latitude: selectedLat, longitude: selectedLon)         //selectedLat and selectedLon are double values set by the app in a previous process          geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in              // Place details             var placeMark: CLPlacemark!             placeMark = placemarks?[0]              // Address dictionary             //print(placeMark.addressDictionary ?? "")              // Location name             if let locationName = placeMark.addressDictionary!["Name"] as? NSString {                 //print(locationName)             }              // Street address             if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString {                 //print(street)             }              // City             if let city = placeMark.addressDictionary!["City"] as? NSString {                 //print(city)             }              // Zip code             if let zip = placeMark.addressDictionary!["ZIP"] as? NSString {                 //print(zip)             }              // Country             if let country = placeMark.addressDictionary!["Country"] as? NSString {                 //print(country)             }          })          return address;     }  
like image 231
Dinuka Jay Avatar asked Dec 28 '16 08:12

Dinuka Jay


People also ask

How do you reverse geocoding in Swift?

Reverse Geocoding With Swift. Reverse geocoding is a simple concept. We hand the CLGeocoder class a set of coordinates, latitude and longitude, and ask it for the corresponding address, a physical location that has meaning to the user.

What is the difference between geocoding and reverse geocoding?

Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address.


1 Answers

func getAddressFromLatLon(pdblLatitude: String, withLongitude pdblLongitude: String) {         var center : CLLocationCoordinate2D = CLLocationCoordinate2D()         let lat: Double = Double("\(pdblLatitude)")!         //21.228124         let lon: Double = Double("\(pdblLongitude)")!         //72.833770         let ceo: CLGeocoder = CLGeocoder()         center.latitude = lat         center.longitude = lon          let loc: CLLocation = CLLocation(latitude:center.latitude, longitude: center.longitude)           ceo.reverseGeocodeLocation(loc, completionHandler:             {(placemarks, error) in                 if (error != nil)                 {                     print("reverse geodcode fail: \(error!.localizedDescription)")                 }                 let pm = placemarks! as [CLPlacemark]                  if pm.count > 0 {                     let pm = placemarks![0]                     print(pm.country)                     print(pm.locality)                     print(pm.subLocality)                     print(pm.thoroughfare)                     print(pm.postalCode)                     print(pm.subThoroughfare)                     var addressString : String = ""                     if pm.subLocality != nil {                         addressString = addressString + pm.subLocality! + ", "                     }                     if pm.thoroughfare != nil {                         addressString = addressString + pm.thoroughfare! + ", "                     }                     if pm.locality != nil {                         addressString = addressString + pm.locality! + ", "                     }                     if pm.country != nil {                         addressString = addressString + pm.country! + ", "                     }                     if pm.postalCode != nil {                         addressString = addressString + pm.postalCode! + " "                     }                       print(addressString)               }         })      } 
like image 58
Himanshu Moradiya Avatar answered Sep 28 '22 08:09

Himanshu Moradiya