Преобразовать строку GPS AVMetadataItem в CLLocation

AVAsset (или AVURLAsset) содержит элементы AVMetadataItems в массиве, один из которых может иметь общий ключ AVMetadataCommonKeyLocation.

Значением этого элемента является строка, которая отображается в следующем формате:

+ 39.9410-075.2040 + 007,371/

Как вы преобразуете эту строку в CLLocation?

1 ответ

Решение

Хорошо, я понял это после того, как обнаружил, что строка в формате 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)
}
Другие вопросы по тегам