videocllocationavassetavurlassetavmetadataitem

Convert AVMetadataItem's GPS string into a CLLocation


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?


Solution

  • 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;
        }
    }