iOS中的位置时间戳精度

3
分析了iOS 10中的位置服务后,发现缓存行为存在一些不一致性。每隔一段时间(在我的情况下是每20秒)获取位置会返回位置,但它们的时间戳不是按照时间顺序排列的。这表明缓存位置可能存在问题。因此,如果您通过位置时间戳检查准确性,最好也保存之前的时间戳。这样您就可以决定是否可以使用获取到的位置。
下面的图片来自我的控制台日志。我使用的格式是“纬度经度:纬度_经度 | 位置时间戳 | 现在:当前时间戳
高亮行具有较早的时间戳
3个回答

3

有时候iOS会从缓存中获取位置信息,导致精度不够准确,因此需要避免这种情况。以下是获取准确位置的代码:

更新: “由于返回初始位置可能需要几秒钟时间,定位管理器通常会立即提供先前缓存的位置数据,然后在可用时提供更实时的位置数据。因此,在执行任何操作之前,检查任何位置对象的时间戳始终是一个好主意。”

参考资料:

https://developer.apple.com/reference/corelocation/cllocationmanager

注意:您可以根据设备(如iPod和iPad)的需要调整精度。

//MARK: Location delgates
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        if locations.count > 0
        {
            let location = locations[locations.count-1]

            let maxAge:TimeInterval = 60;
            let requiredAccuracy:CLLocationAccuracy = 100;

            let locationIsValid:Bool = Date().timeIntervalSince(location.timestamp) < maxAge && location.horizontalAccuracy <= requiredAccuracy;
            if locationIsValid
            {
                NSLog(",,, location : %@",location);
                NSLog("valid locations.....");

            }
        }
    }

1
为了回答这个问题,您可以从https://developer.apple.com/reference/corelocation/cllocationmanager中添加以下引用:“因为返回初始位置可能需要几秒钟的时间,所以位置管理器通常会立即提供先前缓存的位置数据,然后在可用时提供更实时的位置数据。因此,在执行任何操作之前,检查任何位置对象的时间戳始终是一个好主意。” - JoakimE
为什么要这样做?让 _ = locations.first - netshark1000
您还应检查位置精度是否为负。负值表示无效位置。 - RawMean

1
这个问题的原因是有时时间戳与位置不匹配!例如,当您旅行时,突然记录下速度> 300公里/小时和最佳准确性。

我会对位置进行排序,并仅在最后一个位置不太旧时才采用它:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let sortedLocations = locations.sorted { (l1, l2) -> Bool in
            return l1.timestamp.compare(l2.timestamp) != .orderedDescending
        }

        if let newestLocation = sortedLocations.last{

            if Date().timeIntervalSince(newestLocation.timestamp) < 60{
                //TODO: Use the location
            }
        }
    }

0

是的,就像@chirag shah评论的那样,我们绝对需要进行检查。我的建议是我们应该知道缓存技术已经被修改了。而且仅仅检查时间戳是不够的,我们还必须关注失败的情况。这里是Objective C代码

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    CLLocation* location = [locations lastObject];
    NSDate* locationTimestamp = location.timestamp;
    NSTimeInterval cachedBefore = [locationTimestamp timeIntervalSinceNow];
    if (fabs(cachedBefore) < 60.0) {
        // Implement your code here
    }else{
        // Try again or wait for fetching location
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接