หากต้องการกำหนดค่าแอปให้ใช้ Places SDK สำหรับ Android ให้ทำตาม ขั้นตอนต่อไปนี้ โดยแอปทั้งหมดที่ใช้ Places SDK สำหรับ Android ต้องมีคีย์เหล่านี้
ขั้นตอนที่ 1: ตั้งค่า Android Studio
เอกสารนี้อธิบายสภาพแวดล้อมการพัฒนาโดยใช้ Android Studio Hedgehog และปลั๊กอิน Android Gradle เวอร์ชัน 8.2
ขั้นตอนที่ 2 ตั้งค่า SDK
ไลบรารี Places SDK สำหรับ Android พร้อมใช้งานผ่านที่เก็บ Maven ของ Google หากต้องการ เพิ่ม SDK ลงในแอป ให้ทำดังนี้
- ในไฟล์
settings.gradle.kts
ระดับบนสุด ให้รวม พอร์ทัลปลั๊กอิน Gradle ที่เก็บ Maven ของ Google และที่เก็บส่วนกลางของ Maven ไว้ในบล็อกpluginManagement
pluginManagement
บล็อก ต้องปรากฏก่อนคำสั่งอื่นๆ ในสคริปต์pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } }
- ในไฟล์
settings.gradle.kts
ระดับบนสุด ให้ใส่ ที่เก็บ Maven ของ Google และที่เก็บ Maven Central ภายใต้บล็อกdependencyResolutionManagement
ดังนี้dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
-
ในส่วน
dependencies
ของไฟล์build.gradle.kts
ระดับโมดูล ให้เพิ่มทรัพยากร Dependency ลงใน Places SDK สำหรับ Android ดังนี้ดึงดูด
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:4.3.1") }
- ในไฟล์
build.gradle.kts
ระดับโมดูล ให้ตั้งค่าcompileSdk
และminSdk
เป็นค่าต่อไปนี้ดึงดูด
android { compileSdk 34 defaultConfig { minSdk 23 // ... } }
Kotlin
android { compileSdk = 34 defaultConfig { minSdk = 23 // ... } }
- ในส่วน
buildFeatures
ของไฟล์build.gradle
ระดับโมดูล ให้ เพิ่มคลาสBuildConfig
ซึ่งคุณใช้เพื่อเข้าถึงค่าข้อมูลเมตาที่กำหนดไว้ในภายหลัง ในขั้นตอนนี้ดึงดูด
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
ขั้นตอนที่ 3: เพิ่มคีย์ API ลงในโปรเจ็กต์
ส่วนนี้อธิบายวิธีจัดเก็บคีย์ API เพื่อให้แอปอ้างอิงได้อย่างปลอดภัย คุณไม่ควรเช็คอินคีย์ API ในระบบควบคุมเวอร์ชัน ดังนั้นเราขอแนะนำให้จัดเก็บไว้ในไฟล์ secrets.properties
ซึ่งอยู่ในไดเรกทอรีรากของโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับไฟล์ secrets.properties
ได้ที่
ไฟล์พร็อพเพอร์ตี้ Gradle
เราขอแนะนำให้คุณใช้ปลั๊กอินข้อมูลลับ Gradle สำหรับ Android เพื่อให้งานนี้ง่ายขึ้น
วิธีติดตั้งปลั๊กอินข้อมูลลับ Gradle สำหรับ Android ในโปรเจ็กต์ Google Maps
-
ใน Android Studio ให้เปิดไฟล์
build.gradle.kts
หรือbuild.gradle
ระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบdependencies
ภายในbuildscript
Kotlin
buildscript { dependencies { classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1") } }
ดึงดูด
buildscript { dependencies { classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1" } }
-
เปิดไฟล์
build.gradle.kts
หรือbuild.gradle
ระดับโมดูล แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบplugins
Kotlin
plugins { // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
ดึงดูด
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
- ในไฟล์
build.gradle.kts
หรือbuild.gradle
ระดับโมดูล ให้ตรวจสอบ ว่าได้ตั้งค่าtargetSdk
และcompileSdk
เป็น 34 - ซิงค์โปรเจ็กต์กับ Gradle
-
เปิดไฟล์
secrets.properties
ในไดเรกทอรีระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ แทนที่YOUR_API_KEY
ด้วยคีย์ API ของคุณ โปรดจัดเก็บคีย์ไว้ในไฟล์นี้ เนื่องจากระบบจะไม่ตรวจสอบsecrets.properties
ในระบบควบคุมเวอร์ชัน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 same folder as the 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" }
ดึงดูด
secrets { // To add your Maps API key to this project: // 1. If the secrets.properties file does not exist, create it in the same folder as the 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 สำหรับ Android ภายในกิจกรรมหรือ Fragment คุณต้องตัดสินใจก่อนว่าจะใช้ SDK เวอร์ชันใด ได้แก่ Places SDK สำหรับ Android หรือ Places SDK สำหรับ Android (ใหม่) ดูข้อมูลเพิ่มเติมเกี่ยวกับเวอร์ชันผลิตภัณฑ์ได้ที่เลือกเวอร์ชัน SDK
ตัวอย่างต่อไปนี้แสดงวิธีเริ่มต้น SDK สำหรับทั้ง 2 เวอร์ชันPlaces SDK สำหรับ Android (ใหม่)
ส่งคีย์ 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 สำหรับ Android
ส่งคีย์ 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.initialize(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.initialize(getApplicationContext(), apiKey); // Create a new PlacesClient instance PlacesClient placesClient = Places.createClient(this);
ตอนนี้คุณพร้อมที่จะเริ่มใช้ Places SDK สำหรับ Android แล้ว
ขั้นตอนที่ 5: ตั้งค่าอุปกรณ์ Android
หากต้องการเรียกใช้แอปที่ใช้ Places SDK สำหรับ Android คุณต้องติดตั้งใช้งานแอปในอุปกรณ์ Android หรือโปรแกรมจำลอง Android ที่ใช้ Android 5.0 ขึ้นไปและมี Google APIs
- หากต้องการใช้อุปกรณ์ Android ให้ทำตามวิธีการที่ เรียกใช้แอปในอุปกรณ์ฮาร์ดแวร์
- หากต้องการใช้โปรแกรมจำลอง Android คุณสามารถสร้างอุปกรณ์เสมือนและติดตั้งโปรแกรมจำลองได้โดยใช้ Android Virtual Device (AVD) Manager ที่มาพร้อมกับ Android Studio