Convert AVMetadataItem's GPS string into a CLLocation

704 Views Asked by At

An AVAsset (or AVURLAsset) contains AVMetadataItems in an array, of which one may be of the common key AVMetadataCommonKeyLocation.

The value of that item is a string which appears in a format like:

+39.9410-075.2040+007.371/

How do you convert that string into a CLLocation?

2

There are 2 best solutions below

0
On BEST ANSWER

Okay I figured it out after finding that the string is in the ISO 6709 format, and then finding some relevant Apple sample code.

NSString* locationDescription = [item stringValue];

NSString *latitude  = [locationDescription substringToIndex:8];
NSString *longitude = [locationDescription substringWithRange:NSMakeRange(8, 9)];

CLLocation* location = [[CLLocation alloc] initWithLatitude:latitude.doubleValue 
                                                  longitude:longitude.doubleValue];

Here's the Apple sample code: AVLocationPlayer

Also, here's code to convert back:

+ (NSString*)iso6709StringFromCLLocation:(CLLocation*)location
{
    //Comes in like
    //+39.9410-075.2040+007.371/
    //Goes out like
    //+39.9410-075.2040/
    if (location) {
        return [NSString stringWithFormat:@"%+08.4f%+09.4f/",
            location.coordinate.latitude,
            location.coordinate.longitude];
    } else {
        return nil;
    }
}
0
On

I work on the same question and I have the same code in Swift without using substring :

Here the locationString is

+39.9410-075.2040+007.371/

let indexLat = locationString.index(locationString.startIndex, offsetBy: 8)
let indexLong = locationString.index(indexLat, offsetBy: 9)

let lat = String(locationString[locationString.startIndex..<indexLat])
let long = String(locationString[indexLat..<indexLong])

if let lattitude = Double(lat), let longitude = Double(long) {
      let location = CLLocation(latitude: lattitude, longitude: longitude)
}