CLLocationManager从后台状态返回时冻结我的应用程序?

时间:2012-03-28 14:13:01

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

我在我的应用程序的不同视图中使用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];

    }

1 个答案:

答案 0 :(得分:0)

当应用程序进入后台并变为活动状态时,使用这些通知打开(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];