iOS CoreLocation checking CLLocation timestamp to use it

回眸只為那壹抹淺笑 提交于 2019-12-21 05:51:46

问题


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?

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

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.

Thanks!


回答1:


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
        }
    }

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



来源:https://stackoverflow.com/questions/14119303/ios-corelocation-checking-cllocation-timestamp-to-use-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!