将 AVMetadataItem 的 GPS 字符串转换为 CLLocation

Convert AVMetadataItem's GPS string into a CLLocation

一个AVAsset(或AVURLAsset)包含一个数组中的AVMetadataItems,其中一个可能是公共键AVMetadataCommonKeyLocation。

该项目的值是一个字符串,其格式如下:

+39.9410-075.2040+007.371/

如何将该字符串转换为 CLLocation?

好吧,我发现字符串是 ISO 6709 格式,然后找到一些相关的 Apple 示例代码,我想通了。

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];

这是 Apple 示例代码:AVLocationPlayer

此外,这里是转换回来的代码:

+ (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;
    }
}

我在处理同一个问题,我在 Swift 中有相同的代码,但没有使用 substring :

这里的locationString

+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)
}