หน้านี้จะอธิบายวิธีกำหนดค่าโปรเจ็กต์ Android Studio ให้ใช้ Maps SDK สำหรับ Android โดยไม่ต้องใช้เทมเพลต Google Maps ซึ่งมีรายละเอียดอยู่ในคู่มือเริ่มใช้งานฉบับย่อ
เทมเพลต Google Maps จะกำหนดค่าและเพิ่มแผนที่พื้นฐานลงในโปรเจ็กต์ Android Studio ใหม่โดยอัตโนมัติ อย่างไรก็ตาม คุณสามารถเพิ่มแผนที่ในโครงการ Android ที่ใช้เทมเพลต Android Studio อื่นได้ด้วย ในการดำเนินการดังกล่าว คุณต้องกำหนดค่า โปรเจ็กต์ด้วยตนเอง แล้วเพิ่มแผนที่
ขั้นตอนที่ 1: ตั้งค่า Android Studio
- ต้องระบุ Android Studio Arctic Fox ขึ้นไป ดาวน์โหลดและติดตั้ง หากยังไม่ได้ดำเนินการ
- ตรวจสอบว่าคุณใช้ ปลั๊กอิน Android Gradle เวอร์ชัน 7.0 ขึ้นไปใน Android Studio
ขั้นตอนที่ 2 ตั้งค่า SDK
คลัง Maps SDK สำหรับ Android มีให้บริการผ่าน ที่เก็บ Maven ของ Google หากต้องการเพิ่ม SDK ลงในแอป ให้ทำดังนี้
- ในไฟล์
settings.gradle
ระดับบนสุด ให้ใส่พอร์ทัลปลั๊กอิน Gradle, ที่เก็บ Google Maven และที่เก็บส่วนกลาง Maven ไว้ในบล็อกpluginManagement
การบล็อกpluginManagement
ต้องปรากฏก่อนคำสั่งอื่นๆ ในสคริปต์pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } }
- ในไฟล์
settings.gradle
ระดับบนสุด ให้ใส่ที่เก็บ Maven ของ Google และที่เก็บ Maven Central ไว้ในบล็อกdependencyResolutionManagement
ดังนี้dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
- ในไฟล์
build.gradle
ระดับโมดูล ให้เพิ่มทรัพยากร Dependency ของบริการ Google Play สำหรับ Maps SDK สำหรับ Androiddependencies { // Maps SDK for Android implementation 'com.google.android.gms:play-services-maps:18.2.0' }
- ในไฟล์
build.gradle
ระดับโมดูล ให้ตั้งค่าcompileSdk
และminSdk
เป็นค่าต่อไปนี้android { compileSdk 31 defaultConfig { minSdk 19 // ... }
ขั้นตอนที่ 3: เพิ่มคีย์ API ลงในโปรเจ็กต์
ส่วนนี้อธิบายวิธีจัดเก็บคีย์ API เพื่อให้แอปอ้างอิงได้อย่างปลอดภัย คุณไม่ควรตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน เราจึงขอแนะนำให้เก็บคีย์นี้ไว้ในไฟล์ secrets.properties
ซึ่งอยู่ในไดเรกทอรีรากของโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับไฟล์ secrets.properties
ได้ที่ไฟล์คุณสมบัติ Gradle
เราขอแนะนำให้ใช้ปลั๊กอินข้อมูลลับ Gradle สำหรับ Android เพื่อปรับปรุงงานนี้ให้มีประสิทธิภาพยิ่งขึ้น
วิธีติดตั้งปลั๊กอิน Secrets Gradle สำหรับ Android ในโปรเจ็กต์ Google Maps มีดังนี้
-
ใน Android Studio ให้เปิดไฟล์
build.gradle
ระดับโปรเจ็กต์และเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบ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") } }
-
Open your module-level
build.gradle
file and add the following code to theplugins
element.Groovy
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
Kotlin
plugins { id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
- Save the file and sync your project with Gradle.
-
Open the
secrets.properties
in your project level directory, and then add the following code. ReplaceYOUR_API_KEY
with your API key.MAPS_API_KEY=YOUR_API_KEY