【问题标题】:Location timestamp accuracy in iosios中的位置时间戳准确性
【发布时间】:2017-05-27 04:07:07
【问题描述】:

在分析 iOS 10 中的定位服务后,发现缓存行为存在一些不一致。

定期(在我的情况下每 20 秒)获取位置会返回位置,但它们的时间戳不是按时间顺序排列的。这表明缓存位置可能存在问题。因此,如果您通过位置时间戳检查准确性,最好也保存以前的时间戳。这样您就可以决定是否可以使用获取的位置。

下图取自我的控制台日志。这里我使用了“Lat Long : latitude_longitude | location_timestamp | Now : current_timestamp”的格式

【问题讨论】:

    标签: ios cllocationmanager ios10 cllocation


    【解决方案1】:

    是的,有一段时间,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.....");
    
                }
            }
        }
    

    【讨论】:

    • 要完成这个答案,您可以添加来自developer.apple.com/reference/corelocation/cllocationmanager 的引用“因为返回初始位置可能需要几秒钟,位置管理器通常会立即提供先前缓存的位置数据,然后再提供更多最新的位置数据,因为它变得可用。因此,在采取任何行动之前检查任何位置对象的时间戳总是一个好主意。“
    • 你为什么要这样做?让_=locations.first
    • 您还应该检查以确保位置准确性不是负数。负值表示位置无效。
    【解决方案2】:

    这背后的问题是有时时间戳与位置不匹配!例如。旅行时,您突然记录到 > 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
                }
            }
        }
    

    【讨论】:

      【解决方案3】:

      是的,就像@chirag shah 评论的那样,我们绝对需要进行检查。我的建议是我们应该知道缓存技术已被修改。而且仅仅检查时间戳是不够的,我们必须关注失败的情况。这是目标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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-12-18
        • 2016-12-22
        • 2015-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-16
        • 1970-01-01
        相关资源
        最近更新 更多