課金を有効にして API キーを作成したら、アプリの開発に使用する Xcode プロジェクトを設定できます。
各リリースのリリースノートをご覧ください。
ステップ 1: 必要なソフトウェアをインストールする
Places SDK for iOS を使用してプロジェクトを作成するには、以下が必要です。
- Xcode バージョン 14.0 以降
- Cocoapods
- Xcode プロジェクトをまだ作成していない場合は、ここで作成してローカルマシンに保存しますiOS 開発が初めての場合は、新しいプロジェクトを作成して、iOS アプリ テンプレートを選択します。
- プロジェクトのディレクトリに、
Podfile
という名前のファイルを作成します。このファイルでプロジェクトの依存関係を定義します。 Podfile
を編集して、依存関係とそのバージョンを追加します。以下に、アプリケーションのターゲット名とGooglePlaces
Pod の名前を指定する例を示します。source 'https://github.com/CocoaPods/Specs.git' platform :ios, '14.0' target 'YOUR_APPLICATION_TARGET_NAME_HERE' do pod 'GooglePlaces', '8.2.1' end
常に最新状態になるように、pod outdated
を定期的に実行して新しいバージョンを検出してください。Podfile
を保存します。ターミナルを開き、
Podfile
を含むディレクトリに移動します。cd <path-to-project>
pod install
コマンドを実行します。Podfile
で指定した API とその依存関係がインストールされます。pod install
Xcode を終了し、プロジェクトの
.xcworkspace
ファイルを(ダブルクリックして)開いて、Xcode を起動します。これ以降、このプロジェクトを開くには.xcworkspace
ファイルを使用する必要があります。- ターミナルを開き、
Podfile
を含むプロジェクト ディレクトリに移動します。 pod update
コマンドを実行します。これにより、Podfile
で指定されたすべての API が最新バージョンに更新されます。- SDK ソースファイル(GooglePlaces-8.2.1)をダウンロードします。
- ソースファイルを解凍します。[Frameworks] フォルダに移動して XCFramework にアクセスします。
- Xcode を起動し、既存のプロジェクトを開くか、新しいプロジェクトを作成します。iOS を初めて使用する場合は、新しいプロジェクトを作成して iOS アプリ テンプレートを選択します。
- 次の XCFramework を、プロジェクトの [フレームワーク、ライブラリ、埋め込みコンテンツ] の下にドラッグします。必ず [埋め込まない] を選択してください。
GooglePlaces-x.x.x/GooglePlaces.xcframework
- プロジェクトで
GooglePlaces.xcframework
を右クリックし、[Show In Finder] を選択します。 GooglePlaces.bundle
をios-arm64/GooglePlaces.framework/Resources
フォルダから Xcode プロジェクトの最上位ディレクトリにドラッグします。プロンプトが表示されたら、[Copy items into destination group's folder] が選択されていないことを確認します。- プロジェクト ナビゲータからプロジェクトを選択し、アプリのターゲットを選択します。
- [Build Phases] タブを開き、[Link Binary with Libraries] 内で、以下のフレームワークとライブラリを追加します。
CoreGraphics.framework
CoreLocation.framework
libc++.tbd
libz.tbd
QuartzCore.framework
UIKit.framework
特定のターゲットではなくプロジェクトを選択して、[Build Settings] タブを開きます。
- [Other Linker Flags] セクションで
-ObjC
を追加します。これらの設定が表示されない場合は、[Build Settings] バーのフィルタを [Basic] から [All] に変更します。 - SDK ソースファイルをダウンロードします。GooglePlaces-8.2.1
- ダウンロードしたソースファイルを解凍します。
- Xcode を起動し、既存のプロジェクトを開くか、新しいプロジェクトを作成します。iOS 開発が初めての場合は、新しいプロジェクトを作成して iOS アプリ テンプレートを選択します。
- 以前のリリースのマップ バンドルをプロジェクトから削除します。
- 次のバンドルをプロジェクトにドラッグします(プロンプトが表示されたら、[Copy items if needed] を選択します)。
GooglePlaces-x.x.x/Frameworks/GooglePlaces.xcframework
- プロジェクトで
GooglePlaces.xcframework
を右クリックし、[Show In Finder] を選択します。 GooglePlaces.bundle
をResources
フォルダからプロジェクトにドラッグします。プロンプトが表示されたら、[Copy items into destination group's folder] が選択されていないことを確認します。- プロジェクト ナビゲータで目的のプロジェクトを選択し、アプリのターゲットを選択します。
- [Build Phases] タブを開き、[Link Binary with Libraries] 内で、次のフレームワークとライブラリを追加します。
CoreGraphics.framework
CoreLocation.framework
libc++.tbd
libz.tbd
QuartzCore.framework
UIKit.framework
特定のターゲットではなくプロジェクトを選択して、[Build Settings] タブを開きます。
- [Other Linker Flags] セクションで
-ObjC
を追加します。これらの設定が表示されない場合は、[Build Settings] バーのフィルタを [Basic] から [All] に変更します。 - 次の import ステートメントを追加します。
import GooglePlaces
- 次のコードを
application(_:didFinishLaunchingWithOptions:)
メソッドに追加します。YOUR_API_KEY は実際の API キーに置き換えます。GMSPlacesClient.provideAPIKey("YOUR_API_KEY")
- 次の import ステートメントを追加します。
@import GooglePlaces;
- 次のコードを
application:didFinishLaunchingWithOptions:
メソッドに追加します。YOUR_API_KEY は実際の API キーに置き換えます。[GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];
ステップ 2: Xcode プロジェクトを作成し、Places SDK for iOS をインストールする
新しいプロジェクトに API をインストールする手順は次のとおりです。CocoaPods を使う
Places SDK for iOS は、すべてのプレイス機能を含む CocoaPod ポッド GooglePlaces として提供されています。
CocoaPods は、Swift と Objective-C の Cocoa プロジェクト向けのオープンソースの依存関係マネージャーです。CocoaPods ツールをまだお持ちでない場合は、ターミナルから次のコマンドを実行して macOS に CocoaPods ツールをインストールします。詳しくは、CocoaPods スタートガイドをご覧ください。
sudo gem install cocoapods
Places SDK for iOS の Podfile
を作成し、これを使用して SDK とその依存関係をインストールします。
既存のプロジェクトの API を更新する手順は次のとおりです。
XCFramework をインストールする
XCFramework は、Places SDK for iOS をインストールするために、M1 チップセットを使用するマシンを含む複数のプラットフォームで使用できるバイナリ パッケージです。このガイドでは、Places SDK for iOS を含む XCFramework をプロジェクトに追加し、Xcode でビルド設定を構成する方法について説明します。
手動で実装する
このガイドでは、Maps SDK for iOS をプロジェクトに手動で追加し、Xcode でビルド設定を行う方法について説明します。
カルタゴ付き
手動で実装する
このガイドでは、GooglePlaces フレームワークをプロジェクトに手動で追加し、Xcode でビルド設定を構成する方法について説明します。
ステップ 3: アプリに API キーを追加する
以下の例で YOUR_API_KEY
とある箇所は、実際の API キーに差し替えてください。
Swift
次のように、API キーを AppDelegate.swift
に追加します。
Objective-C
次のように、API キーを AppDelegate.m
に追加します。
ステップ 4: コードを記述する
次のコードサンプルは、現在の場所を取得する方法を示しています。
Swift
import GooglePlaces import UIKit class GetStartedViewController : UIViewController { // Add a pair of UILabels in Interface Builder, and connect the outlets to these variables. @IBOutlet private var nameLabel: UILabel! @IBOutlet private var addressLabel: UILabel! private var placesClient: GMSPlacesClient! override func viewDidLoad() { super.viewDidLoad() placesClient = GMSPlacesClient.shared() } // Add a UIButton in Interface Builder, and connect the action to this function. @IBAction func getCurrentPlace(_ sender: UIButton) { let placeFields: GMSPlaceField = [.name, .formattedAddress] placesClient.findPlaceLikelihoodsFromCurrentLocation(withPlaceFields: placeFields) { [weak self] (placeLikelihoods, error) in guard let strongSelf = self else { return } guard error == nil else { print("Current place error: \(error?.localizedDescription ?? "")") return } guard let place = placeLikelihoods?.first?.place else { strongSelf.nameLabel.text = "No current place" strongSelf.addressLabel.text = "" return } strongSelf.nameLabel.text = place.name strongSelf.addressLabel.text = place.formattedAddress } } }
Objective-C
#import "GetStartedViewController.h" @import GooglePlaces; @interface GetStartedViewController () // Add a pair of UILabels in Interface Builder and connect the outlets to these variables @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *addressLabel; @end @implementation GetStartedViewController { GMSPlacesClient *_placesClient; } - (void)viewDidLoad { [super viewDidLoad]; _placesClient = [GMSPlacesClient sharedClient]; } // Add a pair of UILabels in Interface Builder and connect the outlets to these variables. - (IBAction)getCurrentPlace:(UIButton *)sender { GMSPlaceField placeFields = (GMSPlaceFieldName | GMSPlaceFieldFormattedAddress); __weak typeof(self) weakSelf = self; [_placesClient findPlaceLikelihoodsFromCurrentLocationWithPlaceFields:placeFields callback:^(NSArray<GMSPlaceLikelihood *> * _Nullable likelihoods, NSError * _Nullable error) { __typeof__(self) strongSelf = weakSelf; if (strongSelf == nil) { return; } if (error != nil) { NSLog(@"An error occurred %@", [error localizedDescription]); return; } GMSPlace *place = likelihoods.firstObject.place; if (place == nil) { strongSelf.nameLabel.text = @"No current place"; strongSelf.addressLabel.text = @""; return; } strongSelf.nameLabel.text = place.name; strongSelf.addressLabel.text = place.formattedAddress; }]; } @end
次のステップ
プロジェクトの構成後、サンプルアプリを確認できます。Cocoapods v1.6.1 がインストールされている必要があります。