距离过滤器无法在标准位置服务中正常工作

时间:2018-07-02 08:15:48

标签: ios swift core-location cllocationmanager

我正在使用标准的位置服务来跟踪位置。

locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = DISTANCE_LIMIT //value set to 20
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()

在设置了过滤器距离的情况下,我假设仅当用户位置更改指定距离时才会接收位置更新。但是,收到的更新少于小于预设距离值的运动。

所以我将委托方法设置为:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    guard locations.count > 0 else {
        return
    }

    guard let location = locations.last else {
        return
    }

    let previousLocationEncoded = UserDefaults.standard.object(forKey: UserDefaultKey.previousVisitedLocation) as? Data

    if previousLocationEncoded == nil {

        let encodedLocation = NSKeyedArchiver.archivedData(withRootObject: location)

        UserDefaults.standard.set(encodedLocation, forKey: UserDefaultKey.previousVisitedLocation)

        // do some task

    } else {

        let previousLocationDecoded = NSKeyedUnarchiver.unarchiveObject(with: previousLocationEncoded!) as! CLLocation

        let distanceBetweenVisits = previousLocationDecoded.distance(from: location)

        if distanceBetweenVisits > DISTANCE_LIMIT {

            // do some other task 

            let encodedLocation = NSKeyedArchiver.archivedData(withRootObject: location)

            UserDefaults.standard.set(encodedLocation, forKey: UserDefaultKey.previousVisitedLocation)

        }
    }
}

目的是计算当前位置与先前保存的位置之间的距离,以确定该位置是否已被距离过滤器值更改。

但是,有时即使我不动也不动,条件 distanceBetweenVisits> DISTANCE_LIMIT 成立。这取决于iOS和GPS吗?这样的条件可以得到适当处理吗?

0 个答案:

没有答案
相关问题