課金を有効にして API キーを作成したら、アプリの開発に使用する Xcode プロジェクトをセットアップできます。
リリースノートは、各リリースごとに用意されています。
ステップ 1: 必要なソフトウェアをインストールする
Maps SDK for iOS を使ってプロジェクトを作成するには、以下をダウンロードしてインストールする必要があります。
ステップ 2: Xcode プロジェクトを作成し、Maps SDK for iOS をインストールする
新しいプロジェクトに API をインストールする手順は次のとおりです。
CocoaPods を使用
Maps SDK for iOS は CocoaPods ポッドとして提供されています。CocoaPods は、Swift と Objective-C の Cocoa プロジェクトで依存関係を管理するためのオープンソース ツールです。
CocoaPods ツールがない場合は、ターミナルから以下のコマンドを実行して、macOS 上に CocoaPods ツールをインストールします。詳しくは、CocoaPods スタートガイドをご覧ください。
sudo gem install cocoapods
Maps SDK for iOS の Podfile
を作成し、これを使用して API とその依存関係をインストールします。
- Xcode プロジェクトがまだない場合は、ここで作成してローカルマシンに保存します。iOS 開発を初めて行う場合:
- 新しいプロジェクトを作成します。
- [iOS] > [App] テンプレートを選択します。
- プロジェクト オプション画面で、次の操作を行います。
- プロジェクト名を入力します。
- [バンドル ID] フィールドの値を記録します。その値を使用して、以下で API キーを制限できます。
- プロジェクトの [Interface] を [Storyboard] に設定します。
- [Language] を Swift または Objective-C に設定します。
- プロジェクトのディレクトリに、
Podfile
という名前のファイルを作成します。このファイルでプロジェクトの依存関係を定義します。 Podfile
を編集して、依存関係とそのversionsを追加します。 以下に、Maps SDK for iOS に必要な依存関係を含む例を示します。source 'https://github.com/CocoaPods/Specs.git' platform :ios, '14.0' target 'YOUR_APPLICATION_TARGET_NAME_HERE' do pod 'GoogleMaps', '8.3.1' end
常に最新バージョンを利用できるよう、pod outdated
を定期的に実行して新しいバージョンが検出されるようにしてください。必要に応じて、最新バージョンにアップグレードします。Podfile
を保存します。ターミナルを開いて、
Podfile
を含むディレクトリに移動します。cd <path-to-project>
pod install
コマンドを実行します。Podfile
で指定した API とその依存関係がインストールされます。pod install
Xcode を終了し、プロジェクトの
.xcworkspace
ファイルを(ダブルクリックして)開いて、Xcode を起動します。これ以降、このプロジェクトを開くには.xcworkspace
ファイルを使用する必要があります。
既存のプロジェクトの API を更新する手順は次のとおりです。
- ターミナルを開いて、
Podfile
を含むプロジェクト ディレクトリに移動します。 pod update
コマンドを実行します。これにより、Podfile
で指定されたすべての API が最新バージョンに更新されます。
手動でのインストール
このガイドでは、Maps SDK for iOS を含む XCFrameworks をプロジェクトに手動で追加し、Xcode でビルド設定を構成する方法を説明します。XCFramework は、Apple シリコンを使用したマシンを含む、複数のプラットフォームで使用できるバイナリ パッケージです。- 次の SDK バイナリとリソース ファイルをダウンロードします。
- zip ファイルを解凍して、XCFrameworks とリソースにアクセスします。
- Xcode プロジェクトがまだない場合は、ここで作成してローカルマシンに保存します。iOS 開発を初めて行う場合:
- 新しいプロジェクトを作成します。
- [iOS] > [App] テンプレートを選択します。
- プロジェクト オプション画面で、次の操作を行います。
- プロジェクト名を入力します。
- [バンドル ID] フィールドの値を記録します。その値を使用して、以下で API キーを制限できます。
- プロジェクトの [Interface] を [Storyboard] に設定します。
- [Language] を Swift または Objective-C に設定します。
- プロジェクトの [Frameworks, Libraries, and Embedded Content] に、次の XCFrameworks をドラッグします。必ず、各 XCFramework で [Do Not Embed] を選択します。
GoogleMaps.xcframework
GoogleMapsBase.xcframework
GoogleMapsCore.xcframework
- (プレミアム プランのお客様のみ)
GoogleMapsM4B.xcframework
- ダウンロードした GoogleMapsResources から
GoogleMaps.bundle
を Xcode プロジェクトの最上位ディレクトリにドラッグします。プロンプトが表示されたら、[Copy items if needed] が選択されていることを確認します。 - プロジェクト ナビゲータでプロジェクトを選択し、アプリのターゲットを選択します。
- アプリケーションのターゲットの [Build Phases] タブを開き、[Link Binary with Libraries] 内で、次のフレームワークとライブラリを追加します。
Accelerate.framework
CoreData.framework
CoreGraphics.framework
CoreImage.framework
CoreLocation.framework
CoreTelephony.framework
CoreText.framework
GLKit.framework
ImageIO.framework
libc++.tbd
libz.tbd
Metal.framework
OpenGLES.framework
QuartzCore.framework
SystemConfiguration.framework
UIKit.framework
特定のターゲットではなくプロジェクトを選択し、[Build Settings] タブを開きます。[Other Linker Flags] セクションで
-ObjC
を追加します。これらの設定が表示されない場合は、[Build Settings] バーのフィルタを [Basic] から [All] に変更します。Places SDK for iOS XCFramework をインストールするには、Places SDK for iOS スタートガイドをご覧ください。
ステップ 3: API キーをプロジェクトに追加する
API キーを取得するでは、アプリの API キーを生成しました。次に、そのキーを Xcode プロジェクトに追加します。
以下の例で YOUR_API_KEY
とある箇所は、実際の API キーに置き替えてください。
をご覧ください。
Swift
次のように、API キーを AppDelegate.swift
に追加します。
- 次のインポート ステートメントを追加します。
import GoogleMaps
- API キーを使用して、
application(_:didFinishLaunchingWithOptions:)
メソッドに以下を追加します。GMSServices.provideAPIKey("YOUR_API_KEY")
- Places API を使用している場合は、次のようにキーを再度追加します。
GMSPlacesClient.provideAPIKey("YOUR_API_KEY")
Objective-C
次のように、API キーを AppDelegate.m
に追加します。
- 次のインポート ステートメントを追加します。
@import GoogleMaps;
- API キーを使用して、
application:didFinishLaunchingWithOptions:
メソッドに以下を追加します。[GMSServices provideAPIKey:@"YOUR_API_KEY"];
- Places API を使用している場合は、次のようにキーを再度追加します。
[GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];
ステップ 4: 地図を追加する
Swift
/* * Copyright 2020 Google Inc. All rights reserved. * * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this * file except in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF * ANY KIND, either express or implied. See the License for the specific language governing * permissions and limitations under the License. */ import UIKit import GoogleMaps class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. // Create a GMSCameraPosition that tells the map to display the // coordinate -33.86,151.20 at zoom level 6. let options = GMSMapViewOptions() options.camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0) options.frame = self.view.bounds let mapView = GMSMapView(options: options) self.view.addSubview(mapView) // Creates a marker in the center of the map. let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20) marker.title = "Sydney" marker.snippet = "Australia" marker.map = mapView } }
Objective-C
/* * Copyright 2020 Google Inc. All rights reserved. * * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this * file except in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF * ANY KIND, either express or implied. See the License for the specific language governing * permissions and limitations under the License. */ #import "ViewController.h" #import <GoogleMaps/GoogleMaps.h> @interface ViewController() @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // Create a GMSCameraPosition that tells the map to display the // coordinate -33.86,151.20 at zoom level 6. GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init]; options.camera = [GMSCameraPosition cameraWithLatitude:-33.8683 longitude:151.2086 zoom:6]; options.frame = self.view.bounds; GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options]; [self.view addSubview:mapView]; // Creates a marker in the center of the map. GMSMarker *marker = [[GMSMarker alloc] init]; marker.position = CLLocationCoordinate2DMake(-33.86, 151.20); marker.title = @"Sydney"; marker.snippet = @"Australia"; marker.map = mapView; } @end
ステップ 5(省略可): API で使用する URL スキームを宣言する
iOS 9 と Xcode 7 以降では、アプリの Info.plist
ファイルでスキームを指定することで、開く対象とする URL スキームを宣言できます。ユーザーが地図上の Google ロゴをクリックすると、Maps SDK for iOS によって Google マップ モバイルアプリが起動されるため、アプリで関連する URL スキームを宣言できます。
Maps SDK for iOS で使用する URL スキームを宣言するには、次の行を Info.plist
に追加します。
次のスクリーンショットは、Xcode でのユーザー インターフェースでの設定を示しています。
上記の宣言を行わないと、ユーザーが地図上の Google ロゴをタップしたときに次のエラーが発生する可能性があります。
-canOpenURL: failed for URL: "comgooglemaps://" - error: "This app is not allowed to query for scheme comgooglemaps" -canOpenURL: failed for URL: "googlechromes://" - error: "This app is not allowed for query for scheme googlechromes"
これらのエラーを防ぐには、Info.plist
に宣言を追加します。
次のステップ
これで API キーと Xcode プロジェクトが用意できたので、アプリを作成して実行できます。Maps SDK for iOS には、開発の開始に役立つチュートリアルやサンプルアプリが豊富に用意されています。詳しくは以下をご覧ください。