How to get nearby address through reverseGeocodeLocation if location address is nil?

306 Views Asked by At

I am trying to get address of location through reverseGeocodeLocation but sometimes it returns nil address, what I want is get nearby address if reverseGeocodeLocation returns nil address for any particular location.

public class func getAddressFrom(location: CLLocation, completion:@escaping ((String?) -> Void)) {
        let geocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
            if let placemark = placemarks?.first,
                let subThoroughfare = placemark.subThoroughfare,
                let thoroughfare = placemark.thoroughfare,
                let locality = placemark.locality,
                let administrativeArea = placemark.administrativeArea {
                let address = subThoroughfare + " " + thoroughfare + ", " + locality + " " + administrativeArea

                //placemark.addressDictionary

                return completion(address)

            }
            completion(nil)
        }
    }
1

There are 1 best solutions below

1
On

You may be hitting the limit of geocoder.

Excerpt from the docs:

Geocoding requests are rate-limited for each app, so making too many requests in a short period of time may cause some of the requests to fail. When the maximum rate is exceeded, the geocoder passes an error object with the value network to your completion handler.