スキップしてメイン コンテンツに移動

GPS for high accuracy

#import "SampleViewController.h"

#define GPS_MEASURING_TIME 3.0
#define GPS_DESIRED_ACCURACY 100.0
#define GPS_UNADOPTABLE_ACCURACY 2000.0


// GPS測定開始処理。ここではView表示時に測定開始
- (void)viewWillAppear:(BOOL)animated {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    bestEffortAtLocation = nil;
    locationManager.desiredAccuracy = GPS_DESIRED_ACCURACY;
    // GPS測定開始
    [locationManager startUpdatingLocation];
    // GPS_MEASURING_TIME 秒後に 測定終了(stopUpdatingLocation:)メソッドを実行
    [self performSelector:@selector(stopUpdatingLocation:)
                     withObject:nil
                     afterDelay:GPS_MEASURING_TIME];
     NSLog(@"locationManager startUpdatingLocation");

    statusLable.text = @"GPS 取得中...";
    [activityIndicator startAnimating];
    [super viewWillAppear:animated];
}

// GPS情報の処理。OSからGPS情報取得時に呼び出される
- (void)locationManager:(CLLocationManager *)manager 
                 didUpdateToLocation:(CLLocation *)newLocation  
                 fromLocation:(CLLocation *)oldLocation
{
    // newLocation.descriptionで位置情報の全てが文字列で取得できる
    NSLog(@"newLoc: %@", newLocation.description);

    // 不要なデータは無視
    if (-[newLocation.timestamp timeIntervalSinceNow] > 5.0) return;
    if (newLocation.horizontalAccuracy > GPS_UNADOPTABLE_ACCURACY) return;

    // 一番精度の高い情報を記憶
    if (bestEffortAtLocation == nil ||
         newLocation.horizontalAccuracy < bestEffortAtLocation.horizontalAccuracy) {
         [bestEffortAtLocation autorelease];
         bestEffortAtLocation = [newLocation retain];
     }
}

// GPSエラー処理
- (void)locationManager:(CLLocationManager *)manager  didFailWithError:(NSError *)error
{
    [activityIndicator stopAnimating];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GPS Error"
                                message:@"Error" 
                                delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

// GPS測定終了処理
- (void)stopUpdatingLocation:(NSObject *)args {
    [locationManager stopUpdatingLocation];
    [activityIndicator stopAnimating];

    if (bestEffortAtLocation == nil) {
         statusLabel.text = @"GPS 取得失敗";
         return;
    }
    statusLabel.text = @"GPS 取得完了";


    float longitude = bestEffortAtLocation.coordinate.longitude;
    float latitude = bestEffortAtLocation.coordinate.latitude;

    // 位置情報を使った処理
        ....
        ....
}

コメント

このブログの人気の投稿

日本に来た 今三週間

四月六日に日本に来た、今三週間になった。 嬉しくなかった、悲しくなかった。 日本の携帯電話のカメラはとてもいいよ、私は今撮影が好きだ、いつも寮に帰る途中で花とか魚とか写真を撮る。これも生活の趣味だ。 滴は綺麗だ 桜が凋んで落ちる 天気がいいとき、自転車に乗って、食材を買った。 自分で料理をつくる、おいしそうでしょう。 昨日清水寺に行った、天気はとても熱いね。日に焼けすぎたので、腕は今赤い。 側に花があるので、赤い清水寺はもっと美しそうだ。 木の色は艶やかだ。