2011-07-30 14 views
0

ロケーションサービスから緯度、経度、高度、正確度、スピードを取得しています。ロケーションサービス、緯度、時間(30秒)後に経度を更新する方法は?

この情報を30秒ごとに更新したいと思います。私はviewDidLoad方法に置かれている

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

:この目的のために私はNSTimerを使用しました。これは、出力初めてを示しているが、タイマーがこれを呼び出す二回目は、それがこのエラーを示しています

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を受けていません。 あなたはオブジェクトを送信するのUserInfoを使用する必要があります。

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

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

解決策では、問題はロケーションマネージャ(location location manager)からのロケーションオブジェクトがないことです。コード – Azhar

+0

でわかるように、CoreLoactionControllerのCLLocationにグローバル変数を使用する必要があります。 –

2

タイマーを使用してより良いソリューションがあります。 CoreLocationには遅延アップデートの機能が組み込まれており、通常のCoreLocationと組み合わせたタイマーは電話機のバッテリーを破壊します。 allowDeferredLocationUpdatesUntilDistanceTraveledをお試しください:タイムアウト:

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instm/CLLocationManager/allowDeferredLocationUpdatesUntilTraveled:timeout

0

タイマーベースの位置情報の更新が恐ろしいバッテリーを食べる人です。

CoreLocationは、場所が変更されたときに通知します。ポーリングの必要はありません。私が歩いていても30秒は非常に遅いですし、私の電話が何時間も移動しないでベッドの近くに横たわっていると、バッテリーが不必要に破壊されてしまいます。

関連する問題