アプリ内で Google Maps を使用するための SDK、"Google Maps SDK for iOS" の導入手順です。
公式情報は Google Developers にあります。
1. API Keyを取得
- Google APIs Console の Services タブで、Google Maps SDK for iOS を ON にする。
- API Access タブで、"Create new iOS key" ボタンをクリック。ポップアップで出てくるウィンドウに、Google Maps SDK for iOSを使用するアプリのBundle identifierを入力する。
2. SDK をプロジェクトに追加
GoogleMaps SDK for iOS をダウンロード
Getting the Google Maps SDK for iOSGoogleMaps.framework をプロジェクトに追加する
Build Phasesタブの、Copy Bundle Resourcesに、GoogleMaps.framework フォルダ配下にある GoogleMaps.bundle を追加する
3. 必要なフレームワークをプロジェクトに追加
- AVFoundation.framework
- CoreData.framework
- CoreLocation.framework
- CoreText.framework
- GLKit.framework
- ImageIO.framework
- libicucore.dylib
- libstdc++.dylib
- libz.dylib
- OpenGLES.framework
- QuartzCore.framework
- SystemConfiguration.framework
4. ビルド設定
- Architectures を
armv7
にする(もしくは Valid Architectures からarmv7s
を外す) - Other Linker Flags に
-ObjC
を追加
5. Info.plistの編集
Bundle identifier欄に、手順1でAPI Key取得時に入力したBundle identifierを入力する。
6. AppDelegate で初期化
- ヘッダをインポート
#import <GoogleMaps/GoogleMaps.h>
-
application:didFinishLaunchingWithOptions:
で、provideAPIKey:
メソッドをコール(引数には手順1で取得したAPI Keyを渡す)
[GMSServices provideAPIKey:@"YOUR_API_KEY"];
7. 地図を表示する ViewController の実装
- ヘッダをインポート
#import <GoogleMaps/GoogleMaps.h>
- プロパティ追加
@property (nonatomic, weak) GMSMapView *mapView;
- GMSMapViewの初期化と諸々の設定
- (void)loadView {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
longitude:151.2086
zoom:6];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.myLocationEnabled = YES;
self.view = self.mapView;
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = CLLocationCoordinate2DMake(-33.8683, 151.2086);
options.title = @"Sydney";
options.snippet = @"Australia";
[self.mapView addMarkerWithOptions:options];
}
- 地図の描画を開始する
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.mapView startRendering];
}
- 地図の描画を停止する
- (void)viewWillDisappear:(BOOL)animated {
[self.mapView stopRendering];
[super viewWillDisappear:animated];
}