2012-10-07 81 views
1

我初始化的LocationManager這種方式:功能「didUpdateToLocation」被稱爲沒有變化

if (!self.locManager) 
{ 
    self.locManager = [[CLLocationManager alloc] init]; 
    self.locManager.delegate = self; 
    [locManager startMonitoringSignificantLocationChanges]; 
} 

我的設備是不動的,仍然「didUpdateToLocation」被稱爲每次。 有什麼可能是一個問題? 謝謝

回答

3

didUpdateToLocation回調可能由於許多原因而更新,處理這個問題的好策略是逐步過濾基於時間戳的結果,然後要求準確性。

蘋果提供的LocateMe sample app一個很好的例子:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    // test the age of the location measurement to determine if the measurement is cached 
    // in most cases you will not want to rely on cached measurements 
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 
    if (locationAge > 5.0) return; 

    // test that the horizontal accuracy does not indicate an invalid measurement 
    if (newLocation.horizontalAccuracy < 0) return; 

    // test the measurement to see if it is more accurate than the previous measurement 
    if (self.bestEffortAtLocation == nil || self.bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) 
    { 
     // store the location as the "best effort" 
     self.bestEffortAtLocation = newLocation; 

     // test the measurement to see if it meets the desired accuracy 
     // 
     // IMPORTANT!!! kCLLocationAccuracyBest should not be used for comparison with location coordinate or altitidue 
     // accuracy because it is a negative value. Instead, compare against some predetermined "real" measure of 
     // acceptable accuracy, or depend on the timeout to stop updating. This sample depends on the timeout. 
     // 
     if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) { 
      // we have a measurement that meets our requirements, so we can stop updating the location 
      // 
      // IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible. 
      // 
      [self stopUpdatingLocation:NSLocalizedString(@"Acquired Location", @"Acquired Location")]; 
     } 
    } 
} 
+0

太好了,謝謝! – user1553961

+0

謝謝,但如果我想讓位置服務保持打開狀態呢? stopUpdating可能會阻止位置管理器不行? – Dejell

+0

此代碼允許位置管理器根據需要進行更新以確定用戶所需的準確度。在哪一點上,電源管理停止更新是合乎情理的。如果您的應用程序需要在用戶更改位置時監視用戶,那麼您可以註冊重要更改:'[locationManager startMonitoringSignificantLocationChanges];'請參閱[docs](https://developer.apple.com/library/ios/documentation/ userexperience /概念性/ LocationAwarenessPG/CoreLocation/CoreLocation.html) – cleverbit

0

您是否檢查位置差異? CoreLocation還呼籲回調時,其他屬性,如精度,航向和速度變化

startMonitoringSignificantLocationChanges應該給你一個初步修復,之後你會得到「顯著的變化」(手機信號塔等的變化)

+1

對不起,我沒有完全理解。 – user1553961