質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Google マップ

Google Mapは、Google社がオンラインで提供している地図・ローカル検索サービスです。GIS(Geographic Information System:地理情報システム)の中の「WebGIS」に該当します。地図・航空写真・地形の表示方式があり、それぞれユーザーが縮尺を調整して表示させることができます。地域の情報サービスを検索する機能やルート検索の機能も搭載されています。

Q&A

解決済

1回答

948閲覧

Swift GooglePlacesAPIのオートコンプリート機能を使おうとするとエラーになる

kazuma-shino

総合スコア1

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Google マップ

Google Mapは、Google社がオンラインで提供している地図・ローカル検索サービスです。GIS(Geographic Information System:地理情報システム)の中の「WebGIS」に該当します。地図・航空写真・地形の表示方式があり、それぞれユーザーが縮尺を調整して表示させることができます。地域の情報サービスを検索する機能やルート検索の機能も搭載されています。

0グッド

0クリップ

投稿2021/02/06 10:27

エラー内容

PlacesAPIを使用し、オートコンプリートを機能を実装したところ、以下のerrorメッセージとともにautocompleteControllerが強制終了してしまいます

Error: Error Domain=com.google.places.ErrorDomain Code=-4 "Operation failed due to an invalid (malformed or missing) API key. See the developer's guide (https://developers.google.com/places/ios-sdk/start) for information on creating and using an API key." UserInfo={NSLocalizedFailureReason=Operation failed due to an invalid (malformed or missing) API key. See the developer's guide (https://developers.google.com/places/ios-sdk/start) for information on creating and using an API key., NSUnderlyingError=0x281f85410 {Error Domain=com.google.places.api.server.ErrorDomain Code=-2 "REQUEST_DENIED" UserInfo={NSLocalizedFailureReason=You must enable Billing on the Google Cloud Project at https://console.cloud.google.com/project/_/billing/enable Learn more at https://developers.google.com/maps/gmp-get-started, NSLocalizedDescription=REQUEST_DENIED}}}

試したこと

GoogleCloudPlatformに請求先アカウントを追加
APIキーの制限として、iosアプリのみ、バンドルIDを設定済み

↓このように有効にはなっています
イメージ説明

AppDelegate.swift

import UIKit import GoogleMaps import GooglePlaces @main class AppDelegate: UIResponder, UIApplicationDelegate { let cGoogleMapsAPIKey = "***" func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. GMSPlacesClient.provideAPIKey(cGoogleMapsAPIKey) GMSServices.provideAPIKey(cGoogleMapsAPIKey) return true } // MARK: UISceneSession Lifecycle func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) } func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { // Called when the user discards a scene session. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } }

実装中のコード

import UIKit import GooglePlaces class AddTicketViewController: UIViewController { @IBOutlet weak var addressTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() } @IBAction func addressTap(_ sender: Any) { let autocompleteController = GMSAutocompleteViewController() autocompleteController.delegate = self // オートコンプリート用のViewの表示 present(autocompleteController, animated: true, completion: nil) } func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) } } //PlaceAPI extension AddTicketViewController: GMSAutocompleteViewControllerDelegate { // オートコンプリートで場所が選択した時に呼ばれる関数 func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) { // 名前をoutletに設定 addressTextField.text = place.formattedAddress print(place) print("Place name: (place.name)") print("Place address: (place.formattedAddress)") print("Place attributions: (place.attributions)") dismiss(animated: true, completion: nil) } func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) { // TODO: handle the error. print("Error: (error)") dismiss(animated: true, completion: nil) } // User cancelled the operation. func wasCancelled(_ viewController: GMSAutocompleteViewController) { print("Autocomplete was cancelled.") dismiss(animated: true, completion: nil) } }

こちらの記事を参考に実装しました
https://qiita.com/ron214_ron/items/5149ce7c5578dc0d72ce

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

特に変更することなく解決しました。
おそらく設定の反映に時間がかかっていただけだと思います。

投稿2021/02/06 12:16

kazuma-shino

総合スコア1

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問