2012-12-16 76 views
0

我想獲取一個位置,然後停止從CLLocationManager獲取通知。CLLocationManager問題

我這個做:

-(id)initWithDelegate:(id <GPSLocationDelegate>)aDelegate{ 
self = [super init]; 

if(self != nil) { 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    delegate = aDelegate; 

} 
return self; 
} 

-(void)startUpdating{ 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
    [locationManager startUpdatingLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    [locationManager stopUpdatingLocation]; 
    [delegate locationUpdate:newLocation]; 
} 

的問題是,即使我在- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:

[locationManager stopUpdatingLocation];

我仍然得到通知,任何想法,爲什麼會發生?

+0

MTA讓我們知道你是否修復它.. – Stas

回答

-1

我認爲你的問題的原因是距離過濾器。正如文檔所說:

Use the value kCLDistanceFilterNone to be notified of all movements. The default value of this property is kCLDistanceFilterNone. 所以你只是有你所設定的 - 不斷更新。

1

也許試試我的解決方案。我構建了兩個用於處理LocationManger對象的函數。 第一個函數是startUpdates,用於處理開始更新位置。代碼如下所示:

- (void)startUpdate 
{ 
    if ([self locationManager]) 
    { 
     [[self locationManager] stopUpdatingLocation]; 
    } 
    else 
    { 
     self.locationManager = [[CLLocationManager alloc] init]; 
     [[self locationManager] setDelegate:self]; 
     [[self locationManager] setDesiredAccuracy:kCLLocationAccuracyBestForNavigation]; 
     [[self locationManager] setDistanceFilter:10.0]; 
    } 

    [[self locationManager] startUpdatingLocation]; 
} 

第二個函數是stopUpdate,用於處理CLLocationDelegate以停止更新位置。代碼如下所示:

- (void)stopUpdate 
{ 
    if ([self locationManager]) 
    { 
     [[self locationManager] stopUpdatingLocation]; 
    } 
} 

所以,CLLocationManagerDelegate應該是這個樣子:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{  
    NSDate* eventDate = newLocation.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
    self.attempts++; 

    if(firstPosition == NO) 
    { 
     if((howRecent < -2.0 || newLocation.horizontalAccuracy > 50.0) && ([self attempts] < 5)) 
     { 
      // force an update, value is not good enough for starting Point    
      [self startUpdates]; 
      return; 
     } 
     else 
     { 
      firstPosition = YES; 
      isReadyForReload = YES; 
      tempNewLocation = newLocation; 
      NSLog(@"## Latitude : %f", tempNewLocation.coordinate.latitude); 
      NSLog(@"## Longitude : %f", tempNewLocation.coordinate.longitude); 
      [self stopUpdate]; 
     } 
    } 
} 

內高於這個功能,我是正確的只爲更新位置的最佳位置。我希望我的回答會有幫助,乾杯。

相關問題