iPhoneによる位置情報関係のまとめ
- 平成22年1月27日(水) 21時12分55秒
- 区分
- objective-c
- 報告者:
- masuda
こんにちは。
先日komagataさんにgit svnの使い方を教えていただいたおかげで、少しは使えるようになってきたmasudaです。
今回も前回のGeohashに引き続き、位置情報に関する内容です。
ググれば出てくる内容なので、新しいことはなんにもないのが申し訳ないところです。
位置情報に関するライブラリは
- CoreLocation
- MapKit
GPSで現在位置を取得するにはCoreLocationを使用し、地図を表示したり現在地の大まかな住所を取得するためにはMapKitを使用します。
CoreLocationではCLLocationManagerDelegateを実装し
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([locationManager locationServicesEnabled])
{
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", newLocation);
}
このようにすればLogに現在地の情報が取得できます。
地図を表示するには
CGRect screen = [[UIScreen mainScreen] bounds]; MKMapView *mapView = [[MKMapView alloc] initWithFrame:screen]; [[self view] addSubview:mapView]; [mapView release]; MKCoordinateRegion region; region.center = [location coordinate]; region.span.latitudeDelta = 0.005; region.span.longitudeDelta = 0.005; [mapView setRegion:region animated:YES];
とすれば、locationで指定した座標に移動します。
現在地を取得するにはMKReverseGeocoderDelegateを実装し
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:[location coordinate]];
[reverseGeocoder setDelegate:self];
[reverseGeocoder start];
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
NSLog(@"%@", [placemark title]);
}
とすることで取得することができます。
すべてのAPIが非常にシンプルに作られているので、使用するのは非常に簡単です。