अपने ऐप्लिकेशन को Places SDK for Android का इस्तेमाल करने के लिए कॉन्फ़िगर करने के लिए, यह तरीका अपनाएं. Places SDK for Android का इस्तेमाल करने वाले सभी ऐप्लिकेशन के लिए, इनकी ज़रूरत होती है.
पहला चरण: Android Studio सेट अप करना
इस दस्तावेज़ में, Android Studio Hedgehog और Android Gradle प्लगिन के वर्शन 8.2 का इस्तेमाल करके डेवलपमेंट एनवायरमेंट के बारे में बताया गया है.
दूसरा चरण. एसडीके टूल सेट अप करना
Places SDK for Android लाइब्रेरी, Google की मेवन रिपॉज़िटरी के ज़रिए उपलब्ध है. अपने ऐप्लिकेशन में 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:4.3.1") }
- मॉड्यूल-लेवल की
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 के लिए सीक्रेट ग्रेडल प्लग इन का इस्तेमाल करें.
अपने Google Maps प्रोजेक्ट में, Android के लिए Secrets Gradle Plugin इंस्टॉल करने के लिए:
-
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 (New). प्रॉडक्ट के वर्शन के बारे में ज़्यादा जानने के लिए, एसडीके का वर्शन चुनें लेख पढ़ें.
यहां दिए गए उदाहरण में, दोनों वर्शन के लिए एसडीके को शुरू करने का तरीका बताया गया है.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.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 for Android का इस्तेमाल शुरू किया जा सकता है!
पांचवां चरण: Android डिवाइस सेट अप करना
Places SDK for Android का इस्तेमाल करने वाले ऐप्लिकेशन को चलाने के लिए, आपको उसे Android डिवाइस या Android एम्युलेटर पर डिप्लॉय करना होगा. यह Android 5.0 या इसके बाद के वर्शन पर आधारित होना चाहिए और इसमें Google API शामिल होने चाहिए.
- Android डिवाइस का इस्तेमाल करने के लिए, हार्डवेयर डिवाइस पर ऐप्लिकेशन चलाना पर दिए गए निर्देशों का पालन करें.
- Android एम्युलेटर का इस्तेमाल करने के लिए, वर्चुअल डिवाइस बनाया जा सकता है. साथ ही, Android Studio के साथ आने वाले Android वर्चुअल डिवाइस (AVD) मैनेजर का इस्तेमाल करके, एम्युलेटर इंस्टॉल किया जा सकता है.