How to Get GPS Location Information from a Photo or Movie with Swift and UIImagePickerController
This code assumes you're using the UIImagePickerController. If not you will need to get an image url another way.
But, If you are using UIImagePickerControllerDelegate then it will supply the asset's url in the info dictionary . From the asset's url you can fetch the asset with PHAsset.fetchAssets method.
Anyway, once you get the array your asset will contain the location.
class ViewController: UIViewController, UIImagePickerControllerDelegate {
// *** omitted details for implentation of UIImagePickerController *** //
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let url = info[UIImagePickerControllerReferenceURL] as? URL {
if let asset: PHAsset = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)[0] {
print(asset.location ?? "None")
}
}
dismiss(animated: true, completion:nil) // hide picker when done w/it.
}
}
The title of this post is about GPS, but UIImagePickerControllerReferenceURL has nothing to do with GPS.