I need to get the GPS coordinates of an image taken with the iOS device\'s camera. I do not care about the Camera Roll images, just the image taken with UIImagePickerControl
Swift answer:
import AssetsLibrary
import CoreLocation
// MARK: - UIImagePickerControllerDelegate
extension ViewController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
defer {
dismiss(animated: true, completion: nil)
}
guard picker.sourceType == .photoLibrary else {
return
}
guard let url = info[UIImagePickerControllerReferenceURL] as? URL else {
return
}
let library = ALAssetsLibrary()
library.asset(for: url, resultBlock: { (asset) in
guard let coordinate = asset?.value(forProperty: ALAssetPropertyLocation) as? CLLocation else {
return
}
print("\(coordinate)")
// Getting human-readable address.
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(coordinate, completionHandler: { (placemarks, error) in
guard let placemark = placemarks?.first else {
return
}
print("\(placemark.addressDictionary)")
})
}, failureBlock: { (error: Error?) in
print("Unable to read metadata: \(error)")
})
}
}