2012-10-07 78 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