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)
}
}
You may be hitting the limit of geocoder.
Excerpt from the docs: