· 1 min read

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.
    }
}

2 comments

  • Davidn

    The title of this post is about GPS, but UIImagePickerControllerReferenceURL has nothing to do with GPS.

    • Sean

      It is about getting the location data from an asset using UIImagePickerControllerReferenceURL. Read the code in the example and you will see asset.location which holds the data.

Leave a comment