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: