Accuracy of Core Location

后端 未结 4 889
南笙
南笙 2020-12-23 15:16

I\'m currently working on a location tracking app and I have difficulties with inaccurate location updates from my CLLocationManager. This causes my app to track di

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 16:05

    I've used this method to retrive the desired accuracy of the location (In SWIFT)

    let TIMEOUT_INTERVAL = 3.0
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        let newLocation = locations.last as! CLLocation
        println("didupdateLastLocation \(newLocation)")
        //The last location must not be capured more then 3 seconds ago
        if newLocation.timestamp.timeIntervalSinceNow > -3 &&
            newLocation.horizontalAccuracy > 0 {
                var distance = CLLocationDistance(DBL_MAX)
                if let location = self.lastLocation {
                    distance = newLocation.distanceFromLocation(location)
                }
                if self.lastLocation == nil ||
                    self.lastLocation!.horizontalAccuracy > newLocation.horizontalAccuracy {
                        self.lastLocation = newLocation
                        if newLocation.horizontalAccuracy <= self.locationManager.desiredAccuracy {
                            //Desired location Found
                            println("LOCATION FOUND")
                            self.stopLocationManager()
                        }
                } else if distance < 1 {
                    let timerInterval = newLocation.timestamp.timeIntervalSinceDate(self.lastLocation!.timestamp)
                    if timerInterval >= TIMEOUT_INTERVAL {
                        //Force Stop
                        stopLocationManager()
                    }
                }
        }
    

    Where:

    if newLocation.timestamp.timeIntervalSinceNow > -3 &&
            newLocation.horizontalAccuracy > 0 {
    

    The last location retrieved must not be captured more then 3 seconds ago and the last location must have a valid horizontal accuracy (if less then 1 means that it's not a valid location).

    Then we're going to set a distance with a default value:

                var distance = CLLocationDistance(DBL_MAX)
    

    Calculate the distance from the last location retrieved to the new location:

    if let location = self.lastLocation {
                    distance = newLocation.distanceFromLocation(location)
                }
    

    If our local last location hasn't been setted yet or if the new location horizontally accuracy it's better then the actual one, then we are going to set our local location to the new location:

    if self.lastLocation == nil ||
            self.lastLocation!.horizontalAccuracy > newLocation.horizontalAccuracy {
                self.lastLocation = newLocation
    

    The next step it's to check whether the accuracy from the location retrieved it's good enough. To do that we check if the horizontalDistance of the location retrieved it lest then the desiredAccurancy. If this is case we can stop our manager:

    if newLocation.horizontalAccuracy <= self.locationManager.desiredAccuracy {
                            //Desired location Found
                            self.stopLocationManager()
                        }
    

    With the last if we're going to check if the distance from the last location retrieved and the new location it's less the one (that means that the 2 locations are very close). If this it's the case then we're going to get the time interval from the last location retrieved and the new location retrieved, and check if the interval it's more then 3 seconds. If this is the case, this mean that it's more then 3 seconds that we're not receiving a location which is more accurate of the our local location, and so we can stop the location services:

    else if distance < 1 {
                    let timerInterval = newLocation.timestamp.timeIntervalSinceDate(self.lastLocation!.timestamp)
                    if timerInterval >= TIMEOUT_INTERVAL {
                        //Force Stop
                        println("Stop location timeout")
                        stopLocationManager()
                    }
                }
    

提交回复
热议问题