I have written the following function to find out the placemark of an image. This also works, because if a url of an image with the appropriate metadata is given the correct placemark is returned.
func GetLocationFromFoto(url: URL, completion: @escaping ([CLPlacemark]?) -> Void) {
guard let con = getImageMetadata(imageURL: url)?["{GPS}"] as? NSDictionary else {
completion(nil)
return
}
if let lat = con["Latitude"] as? CLLocationDegrees, let lon = con["Longitude"] as? CLLocationDegrees {
let loc = CLLocation(latitude: lat, longitude: lon)
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(loc) { placemarks, error in
if let error = error {
print("Geocoding error: \(error.localizedDescription)")
completion(nil)
return
}
let country = placemarks
completion(country)
}
} else {
completion(nil)
}
}
Since the function geocoder.reverseGeocodeLocation(loc)
runs async, I used a completion. But now it disturbs the further flow of the program. So is there a possibility that the main function waits for this async function and then has a simple return value? The head of the function should look like this: func GetLocationFromFoto(url: URL) -> [CLPlacemark]? {
Thank you very much in advance!