位置座標の取得
Xcodeを使って新しいView-based Applicationプロジェクトを作成し、LBSと命名します。この新しいプロジェクトで、LBSViewController.xibファイルをダブルクリックして、Interface Builderで編集します。
Viewウィンドウに次のビューを配置します(図1を参照)。
- Label
- TextField
XcodeでFrameworksグループを右クリックし、[Add]→[Existing Frameworks...]を選択し、Framework/CoreLocation.frameworkを選択します。
次のコードの中で太字になっているステートメントをLBSViewController.hファイルに追加します。
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface LBSViewController : UIViewController
<CLLocationManagerDelegate> {
IBOutlet UITextField *latitudeTextField;
IBOutlet UITextField *longitudeTextField;
IBOutlet UITextField *accuracyTextField;
CLLocationManager *lm;
}
@property (retain, nonatomic) UITextField *latitudeTextField;
@property (retain, nonatomic) UITextField *longitudeTextField;
@property (retain, nonatomic) UITextField *accuracyTextField;
@end
CLLocationManagerクラスを使用するには、ビューコントローラクラスでCLLocationManagerDelegateプロトコルを実装する必要があります。また、Viewウィンドウの3つのTextFieldビューに接続する3つのアウトレットを作成する必要もあります。
Interface Builderに戻り、controlキーを押しながらFile's Ownerアイテムをクリックし、3つのTextFieldビューのおのおのにドラッグし、それぞれlatitudeTextField、longitudeTextField、accuracyTextFieldを選択します。
次のコードの中で太字になっているステートメントをLBSViewController.mファイルに挿入します。
#import "LBSViewController.h"
@implementation LBSViewController
@synthesize latitudeTextField, longitudeTextField, accuracyTextField;
- (void) viewDidLoad {
lm = [[CLLocationManager alloc] init];
if ([lm locationServicesEnabled]) {
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = 1000.0f;
[lm startUpdatingLocation];
}
}
- (void) locationManager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation{
NSString *lat = [[NSString alloc] initWithFormat:@"%g",
newLocation.coordinate.latitude];
latitudeTextField.text = lat;
NSString *lng = [[NSString alloc] initWithFormat:@"%g",
newLocation.coordinate.longitude];
longitudeTextField.text = lng;
NSString *acc = [[NSString alloc] initWithFormat:@"%g",
newLocation.horizontalAccuracy];
accuracyTextField.text = acc;
[acc release];
[lat release];
[lng release];
}
- (void) locationManager: (CLLocationManager *) manager
didFailWithError: (NSError *) error {
NSString *msg = [[NSString alloc]
initWithString:@"Error obtaining location"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:msg
delegate:nil
cancelButtonTitle: @"Done"
otherButtonTitles:nil];
[alert show];
[msg release];
[alert release];
}
- (void) dealloc{
[lm release];
[latitudeTextField release];
[longitudeTextField release];
[accuracyTextField release];
[super dealloc];
}
上記のコードにより、Viewのロード時にCLLocationManagerクラスのインスタンスが作成されます。このオブジェクトを使用する前に、ユーザーが機器上でロケーションサービスを有効にしているかどうか確認する必要があります。ロケーションサービスが有効になっていれば、desiredAccuracyプロパティを使って希望する精度を指定できます。希望する精度を指定するときは、次の定数を使用します。
- kCLLocationAccuracyBest
- kCLLocationAccuracyNearestTenMeters
- kCLLocationAccuracyHundredMeters
- kCLLocationAccuracyKilometer
- kCLLocationAccuracyThreeKilometers
なお、常に最高精度を指定してもかまいませんが、実際にその精度が保証されるわけではありません。また、必要以上の精度で位置を指定すると、機器のバッテリーと時間が浪費されます。
distanceFilterプロパティでは、位置更新を行う距離間隔(つまり、機器がその距離だけ横移動すると位置更新が行われる間隔)を指定できます。プロパティ値の単位はメートルで、直前の位置からの距離を指定します。わずかな移動でもそのたびに位置更新を行いたい場合は、kCLDistanceFilterNone定数を指定します。最後に、startUpdatingLocationメソッドを使ってロケーションマネージャを起動します。
位置情報を取得するには、次の2つのイベントを処理する必要があります。
- locationManager:didUpdateToLocation:fromLocation:
- locationManager:didFailWithError:
新しい位置値が使用可能なときは、locationManager:didUpdateToLocation:fromLocation:イベントが発生します。ロケーションマネージャが位置値を取得できないときは、locationManager:didFailWithError:イベントが発生します。
位置を取得できた場合は、その位置の緯度、経度、精度を表示します。これはCLLocationオブジェクトを使って行います。このオブジェクトのhorizontalAccuracyプロパティは、精度の範囲をメートル単位で指定します。
Command-rを押して、iPhoneシミュレータでアプリケーションをテストしてみましょう。図2は返された位置の緯度と経度をシミュレータに表示した様子です。また、結果の精度も表示しています。
シミュレータを使用した場合、報告される位置は常に固定のものです。この位置がどこを表しているのかは言うまでもないでしょう。


