Android के लिए Places SDK टूल का इस्तेमाल करने के लिए, अपने ऐप्लिकेशन को कॉन्फ़िगर करने का यह तरीका अपनाएं. Places SDK for Android का इस्तेमाल करने वाले सभी ऐप्लिकेशन के लिए, इनकी ज़रूरत होती है.
पहला चरण: Android Studio सेट अप करना
इस दस्तावेज़ में, Android Studio Hedgehog और Android Gradle प्लग इन के 8.2 वर्शन का इस्तेमाल करने वाले डेवलपमेंट एनवायरमेंट के बारे में बताया गया है.
दूसरा चरण. SDK टूल सेट अप करना
Places SDK for Android लाइब्रेरी, Google की Maven रिपॉज़िटरी से उपलब्ध है. अपने ऐप्लिकेशन में SDK टूल जोड़ने के लिए, यह तरीका अपनाएं:
- अपनी टॉप-लेवल
settings.gradle.kts
फ़ाइल में,pluginManagement
ब्लॉक में Gradle प्लग इन पोर्टल, Google Maven रिपॉज़िटरी, और Maven Central रिपॉज़िटरी को शामिल करें.pluginManagement
ब्लॉक, स्क्रिप्ट में किसी भी अन्य स्टेटमेंट से पहले दिखना चाहिए.pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } }
- अपनी टॉप-लेवल
settings.gradle.kts
फ़ाइल में,dependencyResolutionManagement
ब्लॉक में Google की Maven रिपॉज़िटरी और Maven Central रिपॉज़िटरी को शामिल करें:dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
-
मॉड्यूल-लेवल
build.gradle.kts
फ़ाइल केdependencies
सेक्शन में, Android के लिए Places SDK टूल की डिपेंडेंसी जोड़ें:ग्रूवी
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.1.0") }
- मॉड्यूल-लेवल की
build.gradle.kts
फ़ाइल में,compileSdk
औरminSdk
को इन वैल्यू पर सेट करें:ग्रूवी
android { compileSdk 34 defaultConfig { minSdk 23 // ... } }
Kotlin
android { compileSdk = 34 defaultConfig { minSdk = 23 // ... } }
- अपने मॉड्यूल-लेवल की
build.gradle
फ़ाइल केbuildFeatures
सेक्शन में,BuildConfig
क्लास जोड़ें. इस क्लास का इस्तेमाल, इस प्रोसेस में बाद में बताई गई मेटाडेटा वैल्यू को ऐक्सेस करने के लिए किया जाता है:ग्रूवी
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
तीसरा चरण: प्रोजेक्ट में अपनी एपीआई कुंजी जोड़ना
इस सेक्शन में, एपीआई पासकोड को सेव करने का तरीका बताया गया है, ताकि आपका ऐप्लिकेशन उसका सुरक्षित तरीके से रेफ़रंस दे सके. आपको अपने वर्शन कंट्रोल सिस्टम में एपीआई पासकोड की जांच नहीं करनी चाहिए. इसलिए, हमारा सुझाव है कि आप इसे secrets.properties
फ़ाइल में सेव करें. यह फ़ाइल, आपके प्रोजेक्ट की रूट डायरेक्ट्री में होती है. secrets.properties
फ़ाइल के बारे में ज़्यादा जानकारी के लिए, Gradle प्रॉपर्टी फ़ाइलें देखें.
हमारा सुझाव है कि इस काम को आसान बनाने के लिए, आप Android के लिए Secrets Gradle प्लग इन का इस्तेमाल करें.
अपने Google Maps प्रोजेक्ट में, Android के लिए Secrets Gradle प्लग इन इंस्टॉल करने के लिए:
-
Android Studio में, अपनी टॉप-लेवल
build.gradle.kts
याbuild.gradle
फ़ाइल खोलें औरbuildscript
के नीचे मौजूदdependencies
एलिमेंट में यह कोड जोड़ें.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
को अपनी एपीआई पासकोड से बदलें. अपनी कुंजी को इस फ़ाइल में सेव करें, क्योंकिsecrets.properties
को वर्शन कंट्रोल सिस्टम में शामिल नहीं किया जाता.PLACES_API_KEY=YOUR_API_KEY
-
अपनी टॉप-लेवल डायरेक्ट्री में
local.defaults.properties
फ़ाइल बनाएं. यह फ़ाइल,secrets.properties
फ़ाइल वाले फ़ोल्डर में होनी चाहिए. इसके बाद, नीचे दिया गया कोड जोड़ें.PLACES_API_KEY=DEFAULT_API_KEY
इस फ़ाइल का मकसद, एपीआई पासकोड का बैकअप सेव करना है. ऐसा इसलिए किया जाता है, ताकि अगर
secrets.properties
फ़ाइल न मिले, तो भी बिल्ड न रुके. ऐसा तब हो सकता है, जब आपने ऐप्लिकेशन को किसी ऐसे वर्शन कंट्रोल सिस्टम से क्लोन किया हो जिसमेंsecrets.properties
शामिल न हो और आपने एपीआई पासकोड देने के लिए, अब तक लोकल तौर परsecrets.properties
फ़ाइल न बनाई हो. -
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 for Android को शुरू करना. आपको पहले यह तय करना होगा कि SDK टूल के किस वर्शन का इस्तेमाल करना है: Places SDK for Android या Places SDK for Android (नया). प्रॉडक्ट के वर्शन के बारे में ज़्यादा जानकारी के लिए, अपना SDK टूल वर्शन चुनें लेख पढ़ें.
यहां दिए गए उदाहरण में, दोनों वर्शन के लिए SDK टूल को शुरू करने का तरीका बताया गया है.Places SDK for Android (नया)
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);
Android के लिए Places SDK टूल
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 for Android का इस्तेमाल शुरू करने का विकल्प है!
पांचवां चरण: Android डिवाइस सेट अप करना
Android के लिए Places SDK टूल का इस्तेमाल करने वाले ऐप्लिकेशन को चलाने के लिए, आपको उसे Android 5.0 या इसके बाद के वर्शन पर आधारित Android डिवाइस या Android एमुलेटर पर डिप्लॉय करना होगा. साथ ही, उसमें Google API टूल भी शामिल होने चाहिए.
- Android डिवाइस का इस्तेमाल करने के लिए, हार्डवेयर डिवाइस पर ऐप्लिकेशन चलाना लेख में दिए गए निर्देशों का पालन करें.
- Android एम्युलेटर का इस्तेमाल करने के लिए, वर्चुअल डिवाइस बनाएं और एम्युलेटर इंस्टॉल करें. इसके लिए, Android Studio में मौजूद Android वर्चुअल डिवाइस (AVD) मैनेजर का इस्तेमाल करें.