iOS CoreLocation检查CLLocation时间戳以使用它

问题描述:

如何检查CLLocation对象并决定是否要使用它或丢弃结果并获取新的位置更新?

How do you check a CLLocation object and decide whether you want to use it or discard the result and get a new location update instead?

我看到了timestamp属性关于CLLocation,但我不确定如何将其与当前时间进行比较。

I saw the timestamp property on CLLocation, but I'm not sure how to compare that to the current time.

此外,在我比较时间并找出差异的秒数之后,我应该使用该CLLocation对象的差异是什么值?什么是好的门槛。

Also, after I do compare the time and find the difference in seconds, what value should the difference be under for me to use that CLLocation object? What's a good threshold.

谢谢!

非常重要检查时间戳,因为iOS通常会缓存该位置,并且作为第一个度量返回最后一个缓存,所以这里是我在Core Location Manager的委托方法中的方式:

Is really important to check the timestamp, because iOS usually caches the location and as a first measure returns the last cached, so here is how I do inside the delegate method of Core Location Manager:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation * newLocation = [locations lastObject];
        //check if coordinates are consistent
        if (newLocation.horizontalAccuracy < 0) {
            return;
        }
        NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];
        //check against absolute value of the interval
        if (abs(interval)<30) {
            //DO STUFF
        }
    }

我使用30秒。
如你所见,我也检查结果的一致性,询问水平精度。

希望这有帮助,
Andrea

I use 30 seconds. As you can see I also check the consistency of the result asking the horizontal accuracy.
Hope this helps,
Andrea