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:
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;     }  
                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.
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.
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)               }         })      } 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With