CLLocationManager忽略stopUpdatingLocation

时间:2015-05-19 16:07:42

标签: ios objective-c swift core-location cllocationmanager

我在startUpdatingLocation()上呼叫CLLocationManager,而在didUpdateLocations方法中,当精确度小于100米时,我正在呼叫stopUpdatingLocation()

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    var lastLocation: CLLocation = locations.last! as! CLLocation
    var accuracy = lastLocation.horizontalAccuracy

    if (accuracy < 100) {
        locationCoordinate = lastLocation.coordinate
        locationManager.stopUpdatingLocation()
        getData()
    }
}

但是,除非我将didUpdateLocations设置为stopUpdatingLocations(),否则在调用locationManager后,nil方法仍会被调用一到两倍。显然,这也会多次调用我的方法getData(),这是我不想要的。如何locationManager停止而不将其设置为nil

2 个答案:

答案 0 :(得分:5)

我也面临类似的问题。我通过将CLLocationManagerDelegate设置为nil

解决了这个问题

答案 1 :(得分:0)

正如H先生所说,只是使用一个博尔。

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    var lastLocation: CLLocation = locations.last! as! CLLocation
    var accuracy = lastLocation.horizontalAccuracy

    static bool flag = NO; //Or however this is done in Swift)

    if (accuracy < 100 && !flag) { //Check the flag in the condition

        locationCoordinate = lastLocation.coordinate
        locationManager.stopUpdatingLocation()
        getData()

        flag = YES; //Set flag to yes here

    }
}