
MacBookが手に入ったので早速マップ表示するiPhoneアプリを作ってみることにした。
とりあえず現在地表示。
やりかた
1.Xcodeプロジェクト作成
何はともあれ iPhoneアプリを作るのでXcode起動してプロジェクト作成。

どんなアプリケーションにするか聞いてくるので、今回はSingle View Applicationを選択。

プロジェクト名は適当にMapKitSampleとかにした。
「Use Storyboards」にチェック(画面レイアウトするのに使うっぽい)
「Use Automatic Reference Counting」にチェック(コンパイラがオブジェクトの管理してくれるっぽい)

プロジェクトフォルダ作る場所を適当に選んでCreate

プロジェクトできた。わーい。

とりあえず、SimulatorがiPadになってるからiPhoneに変更

2.必要なFrameworkの追加
画面中央の「Summary」のところを下にスクロールしていくと「Linked Frameworks and Libraries」があるので、「+」ボタンで追加。
今回必要なのはMapKitとCoreLocationってやつなので、順に追加する。

両方追加するとこんな感じ。

3.画面にMapViewを貼り付ける
MainStoryboard_iPhone.storyboardを選択して画面レイアウトを表示する。
右下の〜Viewとかがあるところを下にスクロールしていくとMapViewがあるのでドラッグドロップで貼り付ける。
これでもうマップ表示できるみたい。あら簡単。

4.コード書く
今回は現在地を表示するって決めたので、ちょっとだけコード書く。ViewController.hを選択してソースを開く。
Frameworkを読み込むために下記コードを追加する。(なんかソースの表示がおかしくなっちゃう。)
#import

良い感じに書けたら、その下のViewController.mを選択してソースを開く。
下記コードを追加する。
MKMapView *_mapView; - (void)viewDidLoad { [super viewDidLoad]; // 地図の表示 _mapView = [[MKMapView alloc] init]; _mapView.frame = self.view.bounds; _mapView.mapType = MKMapTypeStandard; _mapView.showsUserLocation = YES; [self.view addSubview:_mapView]; [_mapView.userLocation addObserver:self forKeyPath:@"location" options:0 context:NULL]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { // 地図の中心座標に現在地を設定 _mapView.centerCoordinate = _mapView.userLocation.location.coordinate; // 表示倍率の設定 MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01); MKCoordinateRegion region = MKCoordinateRegionMake(_mapView.userLocation.coordinate, span); [_mapView setRegion:region animated:YES]; }

これも良い感じに書けたら完成。
左上の再生ボタンを押すとiPhoneのSimulatorが起動して実行される。

とってもいい感じに出来ました。
Objective-Cは全然わかんないけどaideal.jpさんのお陰で出来ました。
http://aidealjp.blog.fc2.com/blog-entry-23.html
調べてるうちにわかったけど、MapKitとGoogleMapは違うんだね。
次はGoogle Maps SDK for iOSを使ってやってみようかな。