หากต้องการกำหนดค่าแอปให้ใช้ 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
ระดับบนสุด ให้รวมพอร์ทัลปลั๊กอิน Gradle, ที่เก็บ Google Maven และที่เก็บส่วนกลางของ Maven ภายใต้การบล็อกpluginManagement
บล็อกpluginManagement
ต้องปรากฏก่อนคำสั่งอื่นๆ ในสคริปต์pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } }
- ในไฟล์
settings.gradle
ระดับบนสุด ให้รวมที่เก็บ Maven ของ Google และที่เก็บส่วนกลางของ Maven ใต้บล็อกdependencyResolutionManagement
ดังนี้dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
-
ในส่วน
dependencies
ของไฟล์build.gradle
ระดับโมดูล ให้เพิ่มทรัพยากร Dependency ให้กับ Places SDK สำหรับ Android ดังนี้ดึงดูด
dependencies { // If updating kotlin-bom version number above, also edit project-level build.gradle definition of $kotlin_version variable implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version")) implementation 'com.google.android.libraries.places:places:3.3.0' }
Kotlin
dependencies { implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version")) implementation("com.google.android.libraries.places:places:3.5.0") }
- ในไฟล์
build.gradle
ระดับโมดูล ให้ตั้งค่าcompileSdk
และminSdk
เป็นค่าต่อไปนี้ดึงดูด
android { compileSdk 34 defaultConfig { minSdk 21 // ... } }
Kotlin
android { compileSdk = 34 defaultConfig { minSdk = 21 // ... } }
- ในส่วน
buildFeatures
ของไฟล์build.gradle
ระดับโมดูล ให้เพิ่มคลาสBuildConfig
ที่คุณใช้เพื่อเข้าถึงค่าข้อมูลเมตาที่ระบุภายหลังในกระบวนการนี้ดึงดูด
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
ขั้นตอนที่ 3: เพิ่มคีย์ API ลงในโปรเจ็กต์
ส่วนนี้จะอธิบายวิธีจัดเก็บคีย์ API เพื่อให้แอปใช้อ้างอิงได้อย่างปลอดภัย คุณไม่ควรตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน เราจึงขอแนะนำให้เก็บคีย์ดังกล่าวไว้ในไฟล์ secrets.properties
ซึ่งอยู่ในไดเรกทอรีรากของโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับไฟล์ secrets.properties
ได้ที่ไฟล์พร็อพเพอร์ตี้ Gradle
เราขอแนะนำให้ใช้ปลั๊กอิน Secrets Gradle สำหรับ Android เพื่อปรับปรุงการทำงานนี้ให้มีประสิทธิภาพยิ่งขึ้น
วิธีติดตั้งปลั๊กอิน Secrets Gradle สำหรับ Android ในโปรเจ็กต์ Google Maps
-
ใน Android Studio ให้เปิดไฟล์
build.gradle
หรือbuild.gradle.kts
ระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบdependencies
ในส่วนbuildscript
ดึงดูด
buildscript { dependencies { classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1" } }
Kotlin
buildscript { dependencies { classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1") } }
-
เปิดไฟล์
build.gradle
ระดับโมดูลและเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบplugins
ดึงดูด
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
Kotlin
plugins { id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
- ในไฟล์
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
หรือbuild.gradle.kts
ระดับโมดูล แล้วแก้ไขพร็อพเพอร์ตี้secrets
หากไม่มีพร็อพเพอร์ตี้secrets
ให้เพิ่มพร็อพเพอร์ตี้แก้ไขคุณสมบัติของปลั๊กอินเพื่อตั้งค่า
propertiesFileName
เป็นsecrets.properties
ตั้งค่าdefaultPropertiesFileName
เป็นlocal.defaults.properties
และตั้งค่าพร็อพเพอร์ตี้อื่นๆดึงดูด
secrets { // Optionally specify a different file name containing your secrets. // The plugin defaults to "local.properties" propertiesFileName = "secrets.properties" // A properties file containing default secret values. This file can be // checked in version control. defaultPropertiesFileName = "local.defaults.properties" // Configure which keys should be ignored by the plugin by providing regular expressions. // "sdk.dir" is ignored by default. ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore" ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*" }
Kotlin
secrets { // Optionally specify a different file name containing your secrets. // The plugin defaults to "local.properties" propertiesFileName = "secrets.properties" // A properties file containing default secret values. This file can be // checked in version control. defaultPropertiesFileName = "local.defaults.properties" // Configure which keys should be ignored by the plugin by providing regular expressions. // "sdk.dir" is ignored by default. ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore" ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*" }
ขั้นตอนที่ 4 เริ่มต้นไคลเอ็นต์ของ Places API
เริ่มต้น Places SDK สำหรับ Android ภายในกิจกรรมหรือส่วนย่อย ก่อนอื่นคุณต้องตัดสินใจเลือกเวอร์ชัน 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.initialize()
โดยทำดังนี้
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 API รวมอยู่ด้วย
- หากต้องการใช้อุปกรณ์ Android ให้ทำตามวิธีการเรียกใช้แอปในอุปกรณ์ฮาร์ดแวร์
- หากต้องการใช้โปรแกรมจำลองของ Android คุณสร้างอุปกรณ์เสมือนและติดตั้งโปรแกรมจำลองได้โดยใช้โปรแกรมจัดการอุปกรณ์เสมือน (AVD) ของ Android ที่มาพร้อมกับ Android Studio