2012-05-11 19 views
2

CLLocationManagerを使用して現在の位置を取得しています。現在の位置の緯度と経度の値を取得しています。私は私のアプリでMapviewを使用していない。だから、現在の場所が変更されると、その時点で私は現在の場所をサーバーに更新したいと思う。そして、5分ごとにWebサービスを呼び出し、現在の場所をサーバーに更新する必要があります。バックグラウンドモードとフォアグラウンドモードです。iPhoneのバックグラウンドモードで現在の場所を取得するにはどうすればよいですか?

-(void) getCurrentLocation 
{ 
    // To get the current location of the place 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; //100 m 
    //Start the location manager, then only updates the current location 
    [locationManager startUpdatingLocation]; 
} 

/In Location manager delegate method, if the location is updated it will call and get the values from the newLocation using CLLocation 
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    CLLocation *location = [locationManager location]; 

    // Configure the new event with information from the location 
    CLLocationCoordinate2D coordinate = [location coordinate]; 

    NSString *lat = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
    NSString *lon = [NSString stringWithFormat:@"%f", coordinate.longitude]; 
    NSLog(@"dLatitude : %@", lat); 
    NSLog(@"dLongitude : %@",lon); 

    self.reverseGeocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate] autorelease]; 
    reverseGeocoder.delegate = self; 
    [reverseGeocoder start]; 


    //Stop the location manager 
    [locationManager stopUpdatingLocation]; 

} 

場所が変更されるたびに、特定の場所の緯度と経度を取得する必要があります。

ありがとうございます!

答えて

-1

まず、フォアグラウンドとバックグラウンドでプログラムを実行する必要がある場合は、プロジェクトのinfo.plistに必要なバックグラウンドモードをロケーションサービスとして追加します。場所をNSStringとして取得し、同期型または非同期型を使用してURLをサーバーに送信してみます。

0

UIBackgroundModesキーを使用すると(バックグラウンドでも)ユーザーに継続的なロケーション更新を提供するために標準ロケーションサービスを使用する必要がある場合は、値)をアプリのInfo.plistファイルに追加します。

0

バックグラウンドタスクとフォアグラウンドタスクに次のコードを使用します。

UIApplication *app = [UIApplication sharedApplication]; 

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
}]; 

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

-(void)updateLocation{ 

    // write the code to update location here. this will execute even if in background or foreground 
} 
関連する問題