2011-07-30 66 views
0

我從位置服務獲取緯度,經度,高度,準確度,速度。位置服務,如何在一段時間(30秒)後更新緯度,經度?

我想每30秒更新一次所有這些信息。爲了這個目的,我已經使用了的NSTimer:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(locationUpdate:) userInfo:nil repeats:YES]; 

,我已經把在viewDidLoad方法。它示出了一個輸出的第一時間,但在第二時間,當計時器調用此,它顯示了這樣的錯誤:

2011-08-02 13:17:00.141 CoreLocationAssign[1120:207] This is location <__NSCFTimer: 0x4b200a0> 
2011-08-02 13:17:00.142 CoreLocationAssign[1120:207] -[__NSCFTimer coordinate]: unrecognized selector sent to instance 0x4b200a0 
2011-08-02 13:17:00.144 CoreLocationAssign[1120:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer coordinate]: unrecognized selector sent to instance 0x4b200a0' 
*** Call stack at first throw: 

的代碼如下:

MyCLController.h 

@implementation MyCLController 
@synthesize locationManager; 
@synthesize delegate; 

- (id) init{ 
    if (self!=nil) { 
     self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
     self.locationManager.delegate = self; 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     self.locationManager.distanceFilter = kCLDistanceFilterNone; 
     [self.locationManager startUpdatingLocation]; 
    } 
    return self; 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    //NSLog(@"Location: %@", [newLocation description]); 
    [self.delegate locationUpdate:newLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error 
{ 
// NSLog(@"Error: %@", [error description]); 
    [self.delegate locationError:error]; 
} 

- (void)dealloc { 
    [self.locationManager release]; 
    [super dealloc]; 
} 

CoreLoactionController.h 
- (void)viewDidLoad { 
    locationController = [[MyCLController alloc] init]; 
    locationController.delegate = self; 
    [locationController.locationManager startUpdatingLocation]; 
    [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(locationUpdate:) userInfo:nil repeats:YES]; 
    [super viewDidLoad]; 
} 


- (void)locationUpdate:(CLLocation *)location { 
    locationLable.text = [location description]; 
    CLLocationCoordinate2D coordinate = [location coordinate]; 
    NSLog(@"This is location %@",[location description]); 
    NSLog(@"Lattitude   = %@",[NSString stringWithFormat:@"%f", coordinate.latitude]); 
    NSLog(@"Langitude   = %@",[NSString stringWithFormat:@"%f", coordinate.longitude]); 
    NSLog(@"Altitude   = %@",[NSString stringWithFormat:@"%gm", location.altitude]); 
    NSLog(@"horizontalAccuracy = %@",[NSString stringWithFormat:@"%gm", location.horizontalAccuracy]); 
    NSLog(@"verticalAccuracy = %@",[NSString stringWithFormat:@"%gm", location.verticalAccuracy]); 
    NSLog(@"Speed = %@",[NSString stringWithFormat:@"%gm", location.speed]); 

} 

如何CCAN我解決了這個問題?我想每30秒更新一次地點。

+2

我似乎無法在這裏找到實際問題?你似乎在你的問題的正文中回答了題目問題。 –

回答

0

其實在這裏:

- (void)locationUpdate:(CLLocation *)location 

你沒有收到CLLocation *而是一個的NSTimer。 您應該使用用戶信息發送對象:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(locationUpdate:) userInfo:location repeats:YES]; 

-(void)locationUpdate:(NSTimer*) timer{ 
    //probably 
    CLLocation *location = (CLLocation*)[timer userInfo]; 
    //... 
} 
+0

在你的解決方案中,問題是我們沒有位置管理器(void)locationManager的位置對象:你可以在代碼 – Azhar

+0

中看到我認爲你應該在CoreLoactionController中爲你的CLLocation使用一個全局變量。 –

0

基於定時器的位置更新是可怕的電池食客。

CoreLocation可以告訴你何時位置發生變化,不需要輪詢。如果我甚至走路,30秒太慢了,如果我的手機躺在我的牀邊幾個小時不動,它會不必要地破壞電池。

相關問題