【问题标题】:CLLocationManager freeze my app when returning from background state?CLLocationManager 从后台状态返回时冻结我的应用程序?
【发布时间】:2012-03-28 14:13:01
【问题描述】:

我在我的应用程序的不同视图中使用 CLLocationManager,当从后台状态返回时,某种冻结大约 2 到 3 秒,没有响应用户交互。

在我的 AppDelegate 中,我正在使用以下代码:

   _locationManager = [[CLLocationManager alloc] init]; 
   // Get the location if the user
    if ([CLLocationManager locationServicesEnabled]) 
    {
       [self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
       [self.locationManager setDistanceFilter:50];
       [self.locationManager startUpdatingLocation];
    }

在其他视图中我正在使用此代码:

    _locationManager = [[CLLocationManager alloc] init];
    if ([CLLocationManager locationServicesEnabled]) 
    {
       [self.locationManager setDelegate:self];
       [self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
       [self.locationManager setDistanceFilter:50];
       [self.locationManager startUpdatingLocation];
    }

      // Delegate
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
   {   
         // Set the map view to the current location
         MKCoordinateSpan span;
         span.latitudeDelta = 0.01;
         span.longitudeDelta = 0.02;

         MKCoordinateRegion region;
         region.span = span;
         region.center = newLocation.coordinate;

         [_mapView setRegion:region animated:YES];

    }

【问题讨论】:

  • 您在后台时需要位置吗?那你能不能停止更新位置?
  • 这个问题缺乏细节。我使用 CLLocationManager 并没有发现问题。您是否隔离了导致延迟的代码行?如果不打开 locationmanger 会怎样?
  • 如果我不打开位置管理器,我没有任何问题

标签: iphone cocoa-touch ipad core-location cllocationmanager


【解决方案1】:

当应用进入后台并变为活动状态时,使用这些通知打开 (locationManager startUpdatingLocation) 和关闭 (locationManager stopUpdatingLocation):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(start) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stop) name:UIApplicationDidEnterBackgroundNotification object:nil];

【讨论】:

  • 我想在处理你的 didUpdateLocation 的任何类中。
  • 这个改变有什么好运气吗?
  • 不,我没有,我还有同样的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
相关资源
最近更新 更多