Location timestamp accuracy in ios

最后都变了- 提交于 2019-12-22 17:51:27

问题


After analysing location services in iOS 10, found that some inconsistency is in the caching behaviour.

Fetching locations in a periodic time (in my case every 20 secs) returns locations but their timestamps are not in chronologically ordered. This indicates that the caching locations might have issues. So if you are checking accuracy through location-timestamp better to save the previous timestamps also. So that you could decide that the location fetched can be used or not.

Below image is taken from my console log. Here I used the format "Lat Long : latitude_longitude | location_timestamp | Now : current_timestamp"


回答1:


Yes some time in best accuracy ios take the location from the cache so you need to avoid that location here is the code for accurate locationtion.

Update : "Because it can take several seconds to return an initial location, the location manager typically delivers the previously cached location data immediately and then delivers more up-to-date location data as it becomes available. Therefore it is always a good idea to check the timestamp of any location object before taking any actions."

Reference :

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

Note: you can vary the accuracy for the device like ipod and 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.....");

            }
        }
    }



回答2:


Yes like @chirag shah commented we definitely need to do that check. My suggestion is we should know about that the caching technique has been modified. And also it is not enough only checking the timestamp, we have to focus on the failure case. Here is the objective C code

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



回答3:


The problem behind this is that sometimes the timestamps do not match to the location! E.g. while traveling you suddenly records speeds > 300km/h and best accuracy.

I would sort the locations and only would take the last one if not too old:

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


来源:https://stackoverflow.com/questions/41606592/location-timestamp-accuracy-in-ios

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