如要設定應用程式使用 Places SDK for Android,請按照下列步驟操作。所有使用 Places SDK for Android 的應用程式都必須具備這些權限。
步驟 1:設定 Android Studio
本文件說明使用 Android Studio Hedgehog 和 Android Gradle 外掛程式 8.2 版的開發環境。
步驟 2:設定 SDK
您可透過 Google 的 Maven 存放區存取 Places SDK for Android 程式庫。如要將 SDK 加入應用程式,請按照下列步驟操作:
- 在頂層
settings.gradle.kts檔案的pluginManagement區塊下方,加入 Gradle 外掛程式入口網站、Google Maven 存放區,以及 Maven 中央存放區。pluginManagement區塊必須放在指令碼中的任何其他陳述式之前。pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } }
- 在頂層
settings.gradle.kts檔案的dependencyResolutionManagement區塊下方,加入 Google Maven 存放區和 Maven 中央存放區:dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) repositories { google() mavenCentral() } }
-
在模組層級
build.gradle.kts檔案的dependencies區段中,新增 Places SDK for Android 的依附元件:Groovy
dependencies { implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version")) implementation("com.google.android.libraries.places:places:3.5.0") }
Kotlin
dependencies { // Places and Maps SDKs implementation("com.google.android.libraries.places:places:6.0.2") }
- 在模組層級
build.gradle.kts檔案中,將compileSdk和minSdk設為下列值:Groovy
android { compileSdk 34 defaultConfig { minSdk 23 // ... } }
Kotlin
android { compileSdk = 34 defaultConfig { minSdk = 23 // ... } }
- 在模組層級
build.gradle檔案的buildFeatures部分,新增BuildConfig類別,該類別可用來存取此程序中稍後定義的中繼資料值:Groovy
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
步驟 3:將 API 金鑰加進專案
本節說明如何儲存 API 金鑰,讓應用程式以安全的方式參照金鑰。API 金鑰不應登錄在版本管控系統中,因此建議將金鑰儲存在專案根目錄的 secrets.properties 檔案內,並將該檔案排除在版本管控之外。如要進一步瞭解 Gradle 屬性檔案,請參閱「Gradle 屬性檔案」。
建議您使用 Secrets Gradle Plugin for Android 來簡化這項工作。
如要安裝 Secrets Gradle Plugin for Android 並儲存 API 金鑰,請按照下列步驟操作:
-
在 Android Studio 中開啟頂層的
build.gradle.kts或build.gradle檔案, 然後將下列程式碼加進plugins區塊。Kotlin
plugins { // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") version "2.0.1" apply false }
Groovy
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false }
-
開啟模組層級的
build.gradle.kts或build.gradle檔案,然後將下列程式碼加進plugins元素。Kotlin
plugins { id("com.android.application") // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
Groovy
plugins { id 'com.android.application' // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
- 如果您使用 Android Studio,請使用 Gradle 同步處理專案。
-
開啟頂層目錄中的
secrets.properties檔案,並加入下列程式碼,然後將YOUR_API_KEY替換成您的 API 金鑰。請務必將secrets.properties新增至.gitignore檔案 (或從版本管控系統中排除),以免將 API 金鑰登錄至版本管控系統。PLACES_API_KEY=YOUR_API_KEY
-
在頂層目錄 (與
secrets.properties檔案相同的資料夾) 中建立local.defaults.properties檔案,然後加入下列程式碼。PLACES_API_KEY=DEFAULT_API_KEY
如果找不到
secrets.properties檔案,這個檔案便可做為 API 金鑰的備份位置,以確保建置程序不會失敗。如果您從略過secrets.properties的版本管控系統複製應用程式,且尚未在本機建立secrets.properties檔案來提供 API 金鑰,就可能會發生這種情況。 -
在 Android Studio 中開啟模組層級的
build.gradle.kts或build.gradle檔案,然後編輯secrets屬性。如果沒有secrets屬性,請新增。編輯外掛程式的屬性,將
propertiesFileName設為"secrets.properties"、將defaultPropertiesFileName設為"local.defaults.properties",並設定其他屬性。Kotlin
secrets { // To add your Maps API key to this project: // 1. If the secrets.properties file does not exist, create it in the root directory (the same folder as the root local.properties file). // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY propertiesFileName = "secrets.properties" // A properties file containing default secret values. This file can be // checked in version control. defaultPropertiesFileName = "local.defaults.properties" }
Groovy
secrets { // To add your Maps API key to this project: // 1. If the secrets.properties file does not exist, create it in the root directory (the same folder as the root local.properties file). // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY propertiesFileName = "secrets.properties" // A properties file containing default secret values. This file can be // checked in version control. defaultPropertiesFileName = "local.defaults.properties" }
步驟 4:初始化 Places API 用戶端
如要在活動或片段中初始化 Places SDK for Android,請在呼叫 Places.initializeWithNewPlacesApiEnabled() 時傳遞 API 金鑰:
Kotlin
// Define a variable to hold the Places API key. val apiKey = BuildConfig.PLACES_API_KEY // Log an error if apiKey is not set. if (apiKey.isEmpty() || apiKey == "DEFAULT_API_KEY") { Log.e("Places test", "No api key") finish() return } // Initialize the SDK Places.initializeWithNewPlacesApiEnabled(applicationContext, apiKey) // Create a new PlacesClient instance val placesClient = Places.createClient(this)
Java
// Define a variable to hold the Places API key. String apiKey = BuildConfig.PLACES_API_KEY; // Log an error if apiKey is not set. if (TextUtils.isEmpty(apiKey) || apiKey.equals("DEFAULT_API_KEY")) { Log.e("Places test", "No api key"); finish(); return; } // Initialize the SDK Places.initializeWithNewPlacesApiEnabled(getApplicationContext(), apiKey); // Create a new PlacesClient instance PlacesClient placesClient = Places.createClient(this);
您現在可以開始使用 Places SDK for Android 了!
步驟 5:設定 Android 裝置
如要執行使用 Places SDK for Android 的應用程式,請務必將該應用程式部署至採用 Android 6.0 以上版本且包含 Google API 的 Android 裝置或 Android 模擬器。
- 如要使用 Android 裝置,請按照「在硬體裝置上執行應用程式」一文的說明操作。
- 如要使用 Android Emulator,您可以使用 Android Studio 隨附的裝置管理工具建立虛擬裝置並安裝模擬器。