หากต้องการกำหนดค่าแอปให้ใช้ Places SDK for Android ให้ทำตามขั้นตอนต่อไปนี้ ซึ่งจำเป็นสำหรับแอปทั้งหมดที่ใช้ Places SDK for Android
ขั้นตอนที่ 1: ตั้งค่า Android Studio
เอกสารนี้อธิบายสภาพแวดล้อมในการพัฒนาซอฟต์แวร์โดยใช้ Android Studio Hedgehog และ ปลั๊กอิน Android Gradle เวอร์ชัน 8.2
ขั้นตอนที่ 2 ตั้งค่า SDK
ไลบรารี Places SDK for Android พร้อมให้บริการผ่าน ที่เก็บ Maven ของ Google หากต้องการเพิ่ม SDK ลงในแอป ให้ทำดังนี้
- ในไฟล์
settings.gradle.ktsระดับบนสุด ให้ใส่ พอร์ทัลปลั๊กอิน Gradle, ที่เก็บ Maven ของ Google, และ ที่เก็บส่วนกลาง Maven ไว้ในบล็อกpluginManagementบล็อกpluginManagementต้องปรากฏก่อนคำสั่งอื่นๆ ในสคริปต์pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } }
- ในไฟล์
settings.gradle.ktsระดับบนสุด ให้ใส่ ที่เก็บ Maven ของ Google และ ที่เก็บส่วนกลาง Maven ไว้ในบล็อกdependencyResolutionManagementดังนี้dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) repositories { google() mavenCentral() } }
-
ในส่วน
dependenciesของไฟล์ระดับโมดูลbuild.gradle.ktsให้เพิ่มทรัพยากร Dependency ลงใน 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 // ... } }
- ในส่วน
buildFeaturesของไฟล์build.gradleระดับโมดูล ให้เพิ่มคลาสBuildConfigซึ่งคุณใช้เพื่อเข้าถึงค่าข้อมูลเมตาที่กำหนดไว้ในภายหลัง ในขั้นตอนนี้Groovy
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
ขั้นตอนที่ 3: เพิ่มคีย์ API ลงในโปรเจ็กต์
ส่วนนี้อธิบายวิธีจัดเก็บคีย์ API เพื่อให้แอปอ้างอิงได้อย่างปลอดภัย คุณไม่ควรเช็คอินคีย์ API ลงในระบบควบคุมเวอร์ชัน ดังนั้นเราขอแนะนำให้จัดเก็บคีย์ API ไว้ในไฟล์ secrets.properties ในไดเรกทอรีรากของโปรเจ็กต์ และยกเว้นไฟล์ดังกล่าวจากระบบควบคุมเวอร์ชัน ดูข้อมูลเพิ่มเติมเกี่ยวกับไฟล์พร็อพเพอร์ตี้ Gradle
ได้ที่
ไฟล์พร็อพเพอร์ตี้ Gradle
เราขอแนะนำให้ใช้ ปลั๊กอินข้อมูลลับ Gradle สำหรับ Android เพื่อลดความซับซ้อนของงานนี้
วิธีติดตั้งปลั๊กอินข้อมูลลับ Gradle สำหรับ Android และจัดเก็บคีย์ API
-
ใน Android Studio ให้เปิดไฟล์
build.gradle.ktsหรือbuild.gradleระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ลงในบล็อกpluginsKotlin
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ระดับโมดูล แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบpluginsKotlin
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
-
สร้างไฟล์
local.defaults.propertiesในไดเรกทอรีระดับบนสุด ซึ่งเป็นโฟลเดอร์เดียวกับไฟล์secrets.propertiesแล้วเพิ่มโค้ดต่อไปนี้PLACES_API_KEY=DEFAULT_API_KEY
วัตถุประสงค์ของไฟล์นี้คือการระบุตำแหน่งสำรองสำหรับคีย์ API ในกรณีที่ไม่พบไฟล์
secrets.propertiesเพื่อไม่ให้การสร้าง ล้มเหลว ซึ่งอาจเกิดขึ้นได้หากคุณโคลนแอปจากระบบควบคุมเวอร์ชันที่ละเว้น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 ภายในกิจกรรมหรือ Fragment ให้ส่งคีย์ API เมื่อเรียก
Places.initializeWithNewPlacesApiEnabled() ดังนี้
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 หรือโปรแกรมจำลองของ Android ที่ใช้ Android 6.0 ขึ้นไปและมี Google API
- หากต้องการใช้อุปกรณ์ Android ให้ทำตามวิธีการที่ หัวข้อเรียกใช้แอปในอุปกรณ์ฮาร์ดแวร์
- หากต้องการใช้โปรแกรมจำลองของ Android คุณสามารถสร้างอุปกรณ์เสมือนจริงและติดตั้งโปรแกรมจำลองได้โดยใช้โปรแกรมจัดการอุปกรณ์ที่มาพร้อมกับ Android Studio