Places SDK for Android का इस्तेमाल करने के लिए, अपने ऐप्लिकेशन को कॉन्फ़िगर करने के लिए, यह तरीका अपनाएं. Places SDK for Android का इस्तेमाल करने वाले सभी ऐप्लिकेशन के लिए, इनकी ज़रूरत होती है.
पहला चरण: Android Studio सेट अप करना
इस दस्तावेज़ में, Android Studio Hedgehog और Android Gradle प्लगिन के वर्शन 8.2 का इस्तेमाल करके डेवलपमेंट एनवायरमेंट के बारे में बताया गया है.
दूसरा चरण. एसडीके टूल सेट अप करना
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सेक्शन में, Places SDK for 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:5.0.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 के लिए सीक्रेट ग्रेडल प्लग इन का इस्तेमाल करें.
Android के लिए Secrets Gradle Plugin इंस्टॉल करने और अपनी एपीआई कुंजी सेव करने के लिए:
-
Android Studio में, अपनी रूट-लेवल की
build.gradleफ़ाइल खोलें. इसके बाद,buildscriptमें मौजूदdependenciesएलिमेंट में यह कोड जोड़ें.ग्रूवी
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.android.application' // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
Kotlin
plugins { id("com.android.application") // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
- अगर Android Studio का इस्तेमाल किया जाता है, तो अपने प्रोजेक्ट को Gradle के साथ सिंक करें.
-
अपने प्रोजेक्ट लेवल की डायरेक्ट्री में मौजूद
local.propertiesखोलें. इसके बाद, यह कोड जोड़ें.YOUR_API_KEYको अपने एपीआई पासकोड से बदलें.PLACES_API_KEY=YOUR_API_KEY
चरण 4. Places API क्लाइंट की प्रोसेस शुरू करना
किसी गतिविधि या फ़्रैगमेंट में 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);
अब Places SDK for Android का इस्तेमाल शुरू किया जा सकता है!
पांचवां चरण: Android डिवाइस सेट अप करना
Places SDK for Android का इस्तेमाल करने वाले ऐप्लिकेशन को चलाने के लिए, आपको इसे Android डिवाइस या Android एम्युलेटर पर डिप्लॉय करना होगा. यह Android 5.0 या इसके बाद के वर्शन पर आधारित होना चाहिए और इसमें Google API शामिल होने चाहिए.
- Android डिवाइस का इस्तेमाल करने के लिए, हार्डवेयर डिवाइस पर ऐप्लिकेशन चलाना पर दिए गए निर्देशों का पालन करें.
- Android एम्युलेटर का इस्तेमाल करने के लिए, वर्चुअल डिवाइस बनाया जा सकता है. साथ ही, Android Studio के साथ आने वाले Android वर्चुअल डिवाइस (AVD) मैनेजर का इस्तेमाल करके, एम्युलेटर इंस्टॉल किया जा सकता है.