后台线程中的CLLocationManager

我正在做一个应用程序。我正在使用CLLocationManager类来获取更新的位置纬度和经度详细信息。但我需要在分离线程中使用此CLLocationManager。我编写了如下代码。

- (void)viewDidLoad { [NSThread detachNewThreadSelector:@selector(fetch) toTarget:self withObject:nil]; } -(void)fetch { manager=[[CLLocationManager alloc]init]; manager.delegate=self; manager.distanceFilter=kCLDistanceFilterNone; manager.desiredAccuracy = kCLLocationAccuracyBest; [manager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"%f",newLocation.coordinate.latitude); lbl.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude]; } 

。但是,当我运行此代码时,不会触发此委托方法。所以请指导我如何在单独的线程中获取位置更新。

委托对象的方法是从启动相应位置服务的线程中调用的。 该线程本身必须有一个活动的运行循环,就像在应用程序的主线程中找到的那样。 – 来自苹果文件

你可以试试这个吗?

  dispatch_async(newThread, ^(void) { [self fetch]; }); 

希望你能解决问题。

在.h文件中

 MainView *ctl; NSMutableDictionary *dictSubPoses; - (id)initWithCtl:(MainView*)_ctl; 

在.m文件中

 - (id)initWithCtl:(MainView*)_ctl { if(self = [super init]) { ctl = _ctl; //[_ctl retain]; } return self; } - (void)main { [ctl performSelector:@selector(yourMethod) withObject:dictSubPoses]; } 

在.h文件中

 #import  #import  //Set Delegate CLLocationManagerDelegate // Declare CLLocationManager *locationManager; 

在.m文件中

 -(void)ViewDidLoad { locationupadate=YES; locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.distanceFilter = 100.0f; [locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { if(locationupadate) { NSLog(@"%f",newLocation.coordinate.latitude); lbl.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude]; }