[iOS]AR(位置情報) | Cocoa練習帳

[iOS]AR(位置情報)

現在地情報を取得してみる。


プロジェクトに、CoreLocation.frameworkを追加する。


CLLocationManagerDelegateプロトコルを採用する。


#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
 
@interface ViewController : UIViewController <CLLocationManagerDelegate>
 
@property (weak, nonatomic) IBOutlet UIView *augmentedRealityView;
 
@end

CLLocationManagerクラスのインスタンスを生成して、現在地の更新を開始する。


    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];

現在地の更新を受け取る。


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation  *newLocation = [locations lastObject];
    CLLocationCoordinate2D  newCoordinate = newLocation.coordinate;
    DBGMSG(@"new latitude=%f, longitude=%f", newCoordinate.latitude, newCoordinate.longitude);
}

デバイスの情報も取得してみよう。


電子コンパスが仕様可能な場合は、測定を開始する。


    if ([CLLocationManager headingAvailable]) {
        [self.locationManager startUpdatingHeading];
    }

方位の更新を受け取る。


- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    DBGMSG(@"trueHeading %f, magneticHeading %f", newHeading.trueHeading, newHeading.magneticHeading);
}

ソースコード
GitHubからどうぞ。

関連情報

【Cocoa練習帳】
http://ameblo.jp/bitz/(ミラー・サイト)
Ios Sensor Programming: Iphone and Ipad Apps Wi.../Oreilly & Associates Inc
¥4,163
Amazon.co.jp