0

I want to get address using Lat long, But always getting nil. Please help me, How to manage that type of function so that in return will get address. i want to get address while calling, but it give me always nil.

func getAddressFromLatLon(pdblLatitude: String, pdblLongitude: String) -> String  {
    var addressString : 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("ADDRESS \(pm.country)---\(pm.locality)---\(pm.subLocality)---\(pm.thoroughfare)---\(pm.postalCode)---\(pm.subThoroughfare)")

                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! + " "
                }


            }
    })
    return addressString
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ahtazaz
  • 903
  • 8
  • 21

1 Answers1

1

At the beginning of your function you set addressString to "", and you return this value before the closure of ceo.reverseGeocodeLocation is called.

Instead of returning a value from this function directly you need to use a closure parameter. Your function should look more like this (I added some improvements ):

func getAddressFromLatLon(pdblLatitude: String, pdblLongitude: String, completion: @escaping (String) -> Void)  {
    var addressString : 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
        guard error != nil else {
            print("reverse geodcode fail: \(error!.localizedDescription)")
            return
        }

        let pm = placemarks![0]
        print("ADDRESS \(pm.country)---\(pm.locality)---\(pm.subLocality)---\(pm.thoroughfare)---\(pm.postalCode)---\(pm.subThoroughfare)")

        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! + " "
        }
        completion(addressString)
    })
}

The documentation states that, in the completion block of reverseGeocodeLocation, the placemarks will be non-empty (if they're not nil), so no need for that extra check.

Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
  • Great thanks, now i can get value using getAddressFromLatLon(pdblLatitude: String(position.target.latitude), pdblLongitude: String(position.target.longitude), completion: nil) . I'm i right? – Ahtazaz Jun 03 '18 at 11:25
  • No, don't pass `nil` for the `completion` parameter, give it a closure where you use the result of the reverse location request. – Guy Kogus Jun 03 '18 at 11:44