首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将AVMetadataItem的GPS字符串转换为CLLocation

将AVMetadataItem的GPS字符串转换为CLLocation
EN

Stack Overflow用户
提问于 2016-11-10 02:05:32
回答 2查看 552关注 0票数 0

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

该项的值是以如下格式显示的字符串:

+39.9410-075.2040+007.371/

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-10 02:09:54

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

代码语言:javascript
复制
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];

以下是苹果的示例代码:AVLocationPlayer

此外,这里还有转换回的代码:

代码语言:javascript
复制
+ (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;
    }
}
票数 1
EN

Stack Overflow用户

发布于 2020-02-25 14:57:00

我处理相同的问题,并且在Swift中有相同的代码,而不使用substring

这里是locationString

+39.9410-075.2040+007.371/

代码语言:javascript
复制
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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40518926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档