My issue is this block of code. Sometimes it retrieves the value based on the coordinates and sometimes it hangs and returns the error output below. No matter what it has coordinates every time and a valid user request to use location manager.
What im not understanding is why sometimes it works and sometimes it doesnt.
guard let placemark = placemark else {
print("Error:", error ?? "nil")
return
}
Code:
let long = locationManager.location?.coordinate.longitude
let lat = locationManager.location?.coordinate.latitude
var userLocation = ""
let location = CLLocation(latitude: lat ?? 0.0, longitude: long ?? 0.0)
location.placemark { placemark, error in
guard let placemark = placemark else {
print("Error:", error ?? "nil")
return
}
print(placemark.postalAddressFormatted ?? "")
let line1 = placemark.streetName ?? ""
let city = placemark.city ?? ""
let state = placemark.state ?? ""
let country = "US"
let postal_code = placemark.zipCode ?? ""
Output:
Error: Error Domain=kCLErrorDomain Code=2 "(null)"
You are probably requesting it too many consecutive times.
reverseGeocodeLocation(_:completionHandler:)
From the docs:
After initiating a reverse-geocoding request, do not attempt to initiate another reverse- or forward-geocoding request. 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
CLError.Code.network
to your completion handler.