Google Maps SDK for iOS を試してみました
2013/4/25 修正
ver.1.2.0からMarkerなどのOverlayの作成方法が変更になりましたので、記事を一部修正しました
2013/3/27 修正
Ver.1.1.2でLinkするライブラリが変更になりましたので、記事を一部修正しました
API Keyもやっとこさ作れるようになりましたので、さらに新しくなったGoogle Maps for iOSを試してみました。
> so we’d like to invite you to be amongst some of the first developers to gain access.
ということなので、Keyが作れるのは最初の頃に申し込んだ一部のエンジニアのみみたいです。
API Keyを作る
Bundle Identifier単位でアクセスを制限されるっぽいので、新しいプロジェクトに設定したIDをAPI Consoleで入力して iOS用のAPI Keyを発行。
frameworkをprojectに追加する
SDKのzipを解凍して、中のGoogleMaps.frameworkを、プロジェクトのFrameworksにdrag。copy items にチェックを入れてOK.
bundleをprojectに追加する
上で追加したGoogleMaps.frameworkを右クリックして、Show in Finderで開き、Resourcesの中のGoogleMaps.bundleを XcodeのプロジェクトのFrmeworksにdrag。これもcopy itemsにチェックをいれてOK。
Build Phases設定
アプリケーションターゲットをクリックして、Build Phasesタブを選び、
- AVFoundation.framework
- CoreData.framework
- CoreLocation.framework
- CoreText.framework
- GLKit.framework
- ImageIO.framework
- libc++.dylib
- libicucore.dylib
- libz.dylib
- OpenGLES.framework
- QuartzCore.framework
- SystemConfiguration.framework
をLink Binaryのところで追加
Build Settings設定
- Valid Architecturesを armv7に変更
- Linkingの Other Linker Flags に -ObjCを追加
AppDelegateでServiceを初期化
application:didFinishLaunchingWithOptions:で
[GMSServices provideAPIKey:@"YOUR_API_KEY"];
を呼んでサービスを初期化。 APIキーは上で発行しておいたもの。
ViewControllerに MapViewを追加
あとはViewControllerでMapViewを表示するだけ
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
longitude:151.2086
zoom:6];
_mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
_mapView.myLocationEnabled = YES;
self.view = _mapView;
GMSMarker* marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = @"Sydney";
marker.snippet = @"Australia";
marker.map = _mapView;
これで地図が表示されるはずなんだけど、 ログには
ClientParametersRequest failed, 3 attempts remaining (0 vs 5). Error Domain=com.google.Maps.GMSDASHConnection Code=100 "The operation couldn’t be completed.
とか出力されて、地図が表示されない。MarkerとUserLocationは出てるから、API Keyが違うっぽい。APIキーの説明のところでは40文字って書いてあるけど、実際に発行されるAPI Keyは39文字。はたしてどちらが正しいのやら?
とりあえず今日の調査はここまで。これって年末にやったこととほとんど同じだ…