LocationManager位置

时间:2016-08-29 18:00:20

标签: swift position locationmanager

我有一个填充了API数据的列表。所以基本上这个过程是这样的:

当用户打开应用程序时,我会这样做:

if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 20
        locationManager.startUpdatingLocation()
    }

然后在我的func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])中,我调用API来获取数据。

但问题是func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])有时可以调用5-6次,因此会有很多API调用,如果我只获得一个位置,那么我获得的位置很大远离用户。

有关如何解决此问题的任何想法?我基本上想要最好的位置,并尽可能少地进行API调用,最好是一个。

1 个答案:

答案 0 :(得分:0)

基本上,如果你想避免在func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])调用API,你需要考虑一些控件:

  • 创建收到位置之间的时间间隔
  • “改善”您的horizontalAccuracydistanceFilter

请记住,您始终需要避免:

  • 避免零地点
  • 无效的位置
  • 无效的位置
  • 无序位置
  • 缓存位置(启动locationManager之前)

代码示例:

protocol MyLocationManagerDelegate {
    func locationControllerDidUpdateLocation(location: CLLocation)
}

final class MyLocationManager: NSObject, CLLocationManagerDelegate {

    var oldLocation: CLLocation?
    let kTimeFilter = Double(10)                  //avoid locations for each kTimeFilter seconds
    let kValidDistanceToOldLocation = Double(100)  //avoid location less than kValidDistanceToOldLocation meters
    var startDate: NSDate?
    var delegate: MyLocationManagerDelegate?


    func isValidLocation(newLocation newLocation: CLLocation?, oldLocation: CLLocation?) -> Bool {

        // avoid nil locations
        if newLocation == nil {
            return false
        }

        //avoid invalid locations
        if newLocation!.coordinate.latitude == 0 || newLocation!.coordinate.longitude == 0 {
            return false
        }

        //avoid invalid locations
        if (newLocation!.horizontalAccuracy < 0){
            return false
        }

        if oldLocation != nil {

            let distance = newLocation!.distanceFromLocation(oldLocation!)

            if fabs(distance) < kValidDistanceToOldLocation {
                return false
            }

            //avoid out-of-order location.
            let secondsSinceLastPoint = newLocation!.timestamp.timeIntervalSinceDate(oldLocation!.timestamp)
            if secondsSinceLastPoint < 0 || secondsSinceLastPoint < kTimeFilter {
                return false
            }
        }

        //avoid cached locations (before you start the locationManager)
        let secondsSinceManagerStarted = newLocation!.timestamp.timeIntervalSinceDate(startDate!)
        if secondsSinceManagerStarted < 0 {
            return false
        }

        return true
    }

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

        let newLocation = locations.last

        if isValidLocation(newLocation: newLocation, oldLocation:oldLocation) || oldLocation == nil {
            self.delegate?.locationControllerDidUpdateLocation(newLocation!)
            oldLocation = newLocation
        }

    }

}

class ViewController: UIViewController, MyLocationManagerDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()

        let location = MyLocationManager()
        location.delegate = self

    }

    func locationControllerDidUpdateLocation(location: CLLocation) {
        //Api Call
    }

}