2013-02-24 65 views
1

CLLocation經理協調,一旦我使用這個代碼來獲取座標:獲取與iOS的

_locationManager = [[CLLocationManager alloc] init]; 
_locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
_locationManager.delegate = self; 
[_locationManager startUpdatingLocation]; 

,然後我用

- (void)locationManager:(CLLocationManager *)manager 
didUpdateLocations:(NSArray *)locations 
{ 

_currentLocation = [locations lastObject]; 


//Doing some stuff 

//I am using stopUpdatingLocation but this delegate is called a lot of times 
[_locationManager stopUpdatingLocation]; 



} 

其實我想一旦座標,因爲我希望避免多次執行didUpdateLocation中的代碼。我怎樣才能做到這一點?

回答

0

發生這種情況是因爲位置管理器多次觸發其更新,逐步獲得更準確的位置結果。制止這種

一種方法就是使用一個布爾

BOOL _hasUpdatedUserLocation = NO; 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    if (!_hasUpdatedUserLocation) { 
     _hasUpdatedUserLocation = YES; 
     ... // other code 
    } 
} 

或者,你可以殺死委託:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    [locationManager setDelegate:nil]; 
    ... // other code 
}