【iOS】iPhoneで位置情報を使う方法
こんにちは ビンゴ中西です。
iPhoneで位置情報を使う方法を勉強します。
iOS5以降を想定しています。
それより前のバージョンに付いては、参考にさせていただいているURL先のものを参照してみてください。
現在地を取得
参考:
http://www.bright-sys.co.jp/blog/iphone-corelocation-1/
きみが思い出になる前に - CLGeocoderを使って住所から緯度経度を取得しピンを立ててみた
ViewController.h
なるほど、めちゃめちゃ参考になりました。
#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController <CLLocationManagerDelegate> // ロケーションマネージャー @property(nonatomic, strong) CLLocationManager *locationManager; @end
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize locationManager; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; // ロケーションマネージャーを作成 self.locationManager = [[CLLocationManager alloc] init]; if ([CLLocationManager locationServicesEnabled]) { self.locationManager.delegate = self; // 位置情報取得開始 [self.locationManager startUpdatingLocation]; }else{ NSLog(@"位置情報使えないよ><"); } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"現在のlongitude:%f", newLocation.coordinate.longitude); NSLog(@"現在のlatitude:%f", newLocation.coordinate.latitude); [self.locationManager stopUpdatingLocation]; // 何回もこのメソッドが呼ばれたくないならこのメソッドを追加する } // 位置情報が取得失敗した場合にコールされる。 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if (error) { NSString* message = nil; switch ([error code]) { // アプリでの位置情報サービスが許可されていない場合 case kCLErrorDenied: // 位置情報取得停止 [self.locationManager stopUpdatingLocation]; message = [NSString stringWithFormat:@"このアプリは位置情報サービスが許可されていません。"]; break; default: message = [NSString stringWithFormat:@"位置情報の取得に失敗しました。"]; break; } if (message) { // アラートを表示 UIAlertView* alert= [[UIAlertView alloc] initWithTitle:@"" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } } } @end
距離計算
参考:
Objective-Cで経緯・緯度から2点間の距離を出す « Code? – Design?
現在地が取得できたので、東大と現在地との距離を測ってみます。
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocationメソッドを以下のように書き換えました。
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"現在のlongitude:%f", newLocation.coordinate.longitude); NSLog(@"現在のlatitude:%f", newLocation.coordinate.latitude); [self.locationManager stopUpdatingLocation]; // 何回もこのメソッドが呼ばれたくないならこのメソッドを追加する // 距離計算 double latTouDai = 35.713398; double lngTouDai = 139.762436; CLLocation *toDaiLocation = [[CLLocation alloc] initWithLatitude:latTouDai longitude:lngTouDai]; CLLocationDistance distance = [newLocation distanceFromLocation:toDaiLocation]; NSLog(@"現在地と東大の距離:%fメートル", distance); }
アプリ制作実習〜データベース作成〜
まだ、あんまりまとまったわけではないですが、
写真を管理するデータベースを制作中なので自分のメモ代わりに書いていきます。
このアプリが持っているべきテーブルは
- 写真
- 写真ID
- タイトル
- 画像
- アルバムID
- アルバム
- アルバムID
- タイトル
の2テーブル。
そして、このアプリでやりたいことは
1.写真のアルバム形式でのグループ化
SQLにすると…
select 写真*, アルバムID
from 写真, アルバム
where 写真.アルバムID=アルバム.アルバムID
2.写真(アルバム)を追加すること
SQL➡
insert
into 写真(ID, タイトル, 画像, アルバムID)
values ('03' '記念' 'image.jpg' '01')
3.写真(アルバム)を削除すること
SQL➡
delete
from アルバム
where アルバムID='01'
4.写真(アルバム)のタイトル(所属アルバムID)編集
SQL➡
update 写真
set タイトル='3周年'
where 写真ID='15'
これを、android式に書いていけばよいということになりますが、
それは苦戦中で、まだまだかかりそうです💦