環境:
・Mac OS X Mountain Lion 10.8.5
・Xcode 5.0

Xcode を起動して、 Single View Application テンプレートでプロジェクトを作成。

[Create a new Xcode project]

[Choose a template for your new project]

[iOS]

[Application]

[Single View Application]

プロジェクトができたら、あれこれ設定をする。しかし、よくわからないので適当に。。。

Base SDK とか Deployment Target とかってなんだろ。

Base SDK バージョンとは?

 自分のアプリが使う一番高いSDK(又はOS)バージョン
 このバージョンより高いバージョンからある新らしいAPIは使えない。

Deployment Target バージョンとは?

 自分のアプリが使う一番低いSDK(又はOS)バージョン
 このバージョンより高いバージョンのSDKにあるAPIはWeak Linkされる。
 このバージョンと一緒又は低いバージョンのSDKにあるAPIはStrong Linkされる。

Base SDKとDeployment Targetの意味 at Altruistic Programmer's Blog (JP)
Base SDKはアプリケーションをコンパイルするときに使用されるSDKです。例えばiOS5から使用可能なAPIはBase SDKが5.0以上でないと使用できないということです。
(中略)
Deployment Targetはアプリケーションが動作する最小のOSバージョンです。AppStoreで表示される「iPhone、iPod touch および iPad 互換 iOS X.X 以降が必要」の部分ですね。

[Xcode4] Base SDKとDeployment Target | a.out

とりあえず Deployment Target に 6.0 を指定。

MapKit で地図を表示するための設定も必要。
Capabilities の項目で Map を ON にする。
(あるいは自分で Link Binary With Libraries に MapKit.framework を追加する)

プロジェクト作成時に自動生成された ほげほげ ViewController.m ファイルに、地図を表示するためのコードを追加する。

MapKit のヘッダファイルをインポート。


#import <MapKit/MapKit.h>

viewDidLoad 関数の中身を書き換える。


- (void)viewDidLoad
{
  [super viewDidLoad];
  // 画面いっぱいに地図を表示
  MKMapView* mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  mapView.mapType = MKMapTypeStandard;
  CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(35.178088, 136.891022);
  MKCoordinateSpan span = MKCoordinateSpanMake(0.02, 0.02);
  MKCoordinateRegion region = {coord,span};
  [mapView setRegion:region];
  [self.view addSubview:mapView];
}

ほげほげ ViewController.m ファイルの全貌。


#import "NilabViewController.h"
#import <MapKit/MapKit.h>
 
@interface NilabViewController ()
 
@end
 
@implementation NilabViewController
 
- (void)viewDidLoad
{
  [super viewDidLoad];
  // 画面いっぱいに地図を表示
  MKMapView* mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  mapView.mapType = MKMapTypeStandard;
  CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(35.178088, 136.891022);
  MKCoordinateSpan span = MKCoordinateSpanMake(0.02, 0.02);
  MKCoordinateRegion region = {coord,span};
  [mapView setRegion:region];
  [self.view addSubview:mapView];
}
 
- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
 
@end

ビルドしてシミュレータで動かしてみたときのスクリーンショット。

MapKit

とりあえず、地図を表示するだけはこれでできた(・∀・)/

2年前にも似たようなことをしているけど・・・
[ヅ] MapKit を使って iPhone に地図を表示するシンプルなサンプル (2011-09-15)

Ref.

tags: xcode mapkit iphone ios

Posted by NI-Lab. (@nilab)