How to read the latitude and longitude from a taken photo?

Hello,

I'm trying to get the lat. and long. from a photo that was just taken.

My idea was to get it out of the metadata from that photo with, info[UIImagePickerController.InfoKey.mediaMetadata]

But when i look at that data is doens't show the lat. and long.

Who can help me with this?

Kind regards,

Robby

Replies

Could you post your present code ?


That's a long time I did this.

Some API may have changed a bit since.


But if that can help, here is some code extract.


            var phAsset : PHAsset?
            var stopSearch : Bool = false   // 24.9.2017 nom changé
           
            let optionsForFetch = PHFetchOptions()
            optionsForFetch.includeHiddenAssets = true

                let fetchResultForPhotostream = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.album, subtype: PHAssetCollectionSubtype.albumMyPhotoStream, options: nil)
                if fetchResultForPhotostream.count > 0 {
                    let photostream = fetchResultForPhotostream[0]
                    let fetchResultForPhotostreamAssets = PHAsset.fetchAssets(in: photostream, options: optionsForFetch)
                    if fetchResultForPhotostreamAssets.count >= 0 { 
                        stopSearch = false
                        for i in stride(from: 0, through: fetchResultForPhotostreamAssets.count-1, by: 1) where !stopSearch {   
                            let phAssetBeingCompared = fetchResultForPhotostreamAssets[i]
                            if phAssetBeingCompared.localIdentifier.range(of: localIDFragment) != nil { 
                                phAsset = phAssetBeingCompared
                                stopSearch = true
                            }
                            if stopSearch { // Found the image
                                let photoLocation = phAsset!.location     // This is the location

Thanks for your answer!

As you thought already, some API has changed unfortunatly.

For the moment i'm not getting a solution but I will look further in the PHAsset.

The code I was using was:


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let chosenImage = info[.originalImage] as? UIImage {
            topview.image = chosenImage //show it in my view
            let metadata = info[UIImagePickerController.InfoKey.mediaMetadata] as! String
            print(metadata)
        }

But this doesn't show a location/longitude or latitude.

I just want to take a photo and place it on a map.

Regards

Robby

Here you get only the image.


That's why I needed to use PHAssets to get access to metadata.


Here all the metadata

h ttps://www.howtogeek.com/354802/how-to-view-exif-metadata-for-photos-on-an-iphone-or-ipad/


and to access programmatically

h ttps://github.com/foundry/UIImageMetadata


Note that PHAssets is now replaced by ALAssets

Thanks!

I will give this a try.