How to find nearest location when using reverseGeocodeLocation Swift

725 views Asked by At

I am working with reverseGeocodeLocation and sometime with a given latitude and longitude a 'subThoroughfare' and 'thoroughfare' are not returned. If there a way to get the closest 'subThoroughfare' and 'thoroughfare' to that location?

Here is my code so far:

var nearAddressString = ""

        CLGeocoder().reverseGeocodeLocation(userCLLocation, completionHandler: { (placemarks, error) -> Void in

            if error != nil {
                print(error!)

            } else {

                if let p = placemarks?[0] {

                    let subThroughfare = p.subThoroughfare ?? ""

                    let thoroughfare = p.thoroughfare ?? ""

                    let subLocality = p.subLocality ?? ""

                    let subAdministrativeArea = p.subAdministrativeArea ?? ""

                    let postalCode = p.postalCode ?? ""

                    nearAddressString = "\(subThroughfare) \(thoroughfare) \n \(subLocality) \n \(subAdministrativeArea) \n \(postalCode)"

                    print(nearAddressString)

                } else {
                    print("Problem with the data received from geocoder")
                }

            }

        })
0

There are 0 answers