【问题标题】:Convert AVMetadataItem's GPS string into a CLLocation将 AVMetadataItem 的 GPS 字符串转换为 CLLocation
【发布时间】:2017-03-23 23:15:30
【问题描述】:

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

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

+39.9410-075.2040+007.371/

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

【问题讨论】:

    标签: video cllocation avasset avurlasset avmetadataitem


    【解决方案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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      我在处理同样的问题,并且在 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)
      }
      

      【讨论】:

        猜你喜欢
        • 2017-07-21
        • 1970-01-01
        • 2012-03-07
        • 2019-07-30
        • 2019-10-10
        • 2019-01-05
        • 2015-01-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多