我在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
?
答案 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
}
}