后台线程中的CLLocationManager

时间:2012-12-17 06:47:44

标签: iphone ios mapkit

我正在做一个应用程序。我正在使用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];
 }

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

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

请你试试这个。

 dispatch_async(newThread, ^(void) {

       [self fetch];
   }); 
希望你能解决问题。

答案 2 :(得分:-2)

<。>文件中的

#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

//Set Delegate  
    CLLocationManagerDelegate
// Declare
    CLLocationManager *locationManager;
<。>文件中的

-(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];

     }

答案 3 :(得分:-2)

<。>文件中的

MainView *ctl;
NSMutableDictionary *dictSubPoses;

- (id)initWithCtl:(MainView*)_ctl;
<。>文件中的

- (id)initWithCtl:(MainView*)_ctl
{
   if(self = [super init]) 
   {
      ctl = _ctl; //[_ctl retain];
   }

   return self;
}

- (void)main 
{
   [ctl performSelector:@selector(yourMethod) withObject:dictSubPoses];
}