获取与iOS的

问题描述:

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中的代码。我怎样才能做到这一点?

发生这种情况是因为位置管理器多次触发其更新,逐步获得更准确的位置结果。制止这种

一种方法就是使用一个布尔

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 
}