Android Studio प्रोजेक्ट सेट अप करना

अपने ऐप्लिकेशन को Android के लिए Places SDK टूल का इस्तेमाल करने के लिए कॉन्फ़िगर करें. इसके लिए, इन चरण पूरे करें. ये Android के लिए, Places SDK टूल का इस्तेमाल करने वाले सभी ऐप्लिकेशन के लिए ज़रूरी हैं.

पहला चरण: Android Studio सेट अप करना

इस दस्तावेज़ में, डेवलपमेंट एनवायरमेंट के बारे में बताया गया है. इसके लिए, इसका इस्तेमाल किया जा रहा है Android Studio Hedgehog और Android Gradle प्लग इन वर्शन 8.2.

दूसरा चरण. SDK टूल सेट अप करें

Android लाइब्रेरी के लिए Places SDK टूल इनके ज़रिए उपलब्ध है Google की Maven रिपॉज़िटरी. यहां की यात्रा पर हूं अपने ऐप्लिकेशन में SDK टूल जोड़ें और ये काम करें:

  1. अपनी टॉप-लेवल settings.gradle फ़ाइल में, इसे शामिल करें Gradle प्लग इन पोर्टल, Google Maven रिपॉज़िटरी, और Maven सेंट्रल रिपॉज़िटरी pluginManagement ब्लॉक के तहत. pluginManagement ब्लॉक स्क्रिप्ट में किसी भी अन्य स्टेटमेंट से पहले दिखना चाहिए.
    pluginManagement {
        repositories {
            gradlePluginPortal()
            google()
            mavenCentral()
        }
    } 
  2. अपनी टॉप-लेवल settings.gradle फ़ाइल में, इसे शामिल करें Google की Maven रिपॉज़िटरी और Maven सेंट्रल रिपॉज़िटरी dependencyResolutionManagement ब्लॉक के तहत:
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    } 
  3. dependencies में अपने मॉड्यूल-लेवल की build.gradle फ़ाइल के सेक्शन में कोई डिपेंडेंसी जोड़ें Android के लिए स् थान SDK में:

    ग्रूवी

    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")
    }
  4. अपनी मॉड्यूल-लेवल build.gradle फ़ाइल में, compileSdk सेट करें और minSdk को नीचे दी गई वैल्यू में बदलें:

    ग्रूवी

    android {
        compileSdk 34
    
        defaultConfig {
            minSdk 21
            // ...
        }
    }

    Kotlin

    android {
        compileSdk = 34
    
        defaultConfig {
            minSdk = 21
            // ...
        }
    }
  5. मॉड्यूल-लेवल की build.gradle फ़ाइल के buildFeatures सेक्शन में, BuildConfig क्लास जोड़ें, जिसका इस्तेमाल बाद में तय किए गए मेटाडेटा वैल्यू को ऐक्सेस करने के लिए किया जा सकता है इस प्रक्रिया में:

    ग्रूवी

    android {
      // ...
      buildFeatures {
        buildConfig true
        // ...
      }
    }

    Kotlin

    android {
      // ...
      buildFeatures {
        buildConfig = true
        // ...
      }
    }

तीसरा चरण: प्रोजेक्ट में एपीआई पासकोड जोड़ना

इस सेक्शन में, एपीआई पासकोड को सेव करने का तरीका बताया गया है, ताकि इसका इस्तेमाल आपका ऐप्लिकेशन. आपको वर्शन कंट्रोल सिस्टम में एपीआई पासकोड की जांच नहीं करनी चाहिए. इसलिए, हमारा सुझाव है कि इसे secrets.properties फ़ाइल में सेव करना, जो आपकी रूट डायरेक्ट्री में मौजूद होती है प्रोजेक्ट. secrets.properties फ़ाइल के बारे में ज़्यादा जानकारी के लिए, देखें Gradle प्रॉपर्टी फ़ाइलें.

इस टास्क को आसान बनाने के लिए, हमारा सुझाव है कि आप Android के लिए सीक्रेट ग्रेडल प्लग इन.

अपने Google Maps प्रोजेक्ट में Android के लिए Secrets Gradle प्लग इन इंस्टॉल करने के लिए:

  1. 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")
        }
    }
    
  2. अपनी मॉड्यूल-लेवल build.gradle फ़ाइल खोलें और नीचे दिए गए कोड को plugins एलिमेंट.

    ग्रूवी

    plugins {
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    }

    Kotlin

    plugins {
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. अपनी मॉड्यूल-लेवल build.gradle फ़ाइल में, पक्का करें कि targetSdk और compileSdk को 34 पर सेट किया गया.
  4. फ़ाइल सेव करें और Gredle के साथ प्रोजेक्ट सिंक करते हैं.
  5. अपनी शीर्ष-स्तरीय डायरेक्ट्री में secrets.properties फ़ाइल खोलें, और फिर जोड़ें . YOUR_API_KEY को अपनी एपीआई पासकोड से बदलें. अपनी कुंजी इस फ़ाइल में सेव करें ऐसा इसलिए हुआ, क्योंकि वर्शन कंट्रोल में secrets.properties की जांच नहीं की जा सकी सिस्टम.
    PLACES_API_KEY=YOUR_API_KEY
  6. फ़ाइल सेव करें.
  7. अपनी टॉप-लेवल डायरेक्ट्री में local.defaults.properties फ़ाइल बनाएं. फ़ोल्डर को secrets.properties फ़ाइल के रूप में चुनकर यह कोड जोड़ें.

    PLACES_API_KEY=DEFAULT_API_KEY

    इस फ़ाइल का मकसद, एपीआई पासकोड के लिए बैकअप जगह की जानकारी देना है, अगर secrets.properties फ़ाइल नहीं मिली है, ताकि बिल्ड विफल न हो. ऐसा तब हो सकता है, जब आपने वर्शन कंट्रोल सिस्टम से ऐप्लिकेशन का क्लोन बनाया है. इसमें secrets.properties और आपने अभी तक स्थानीय भाषा में secrets.properties फ़ाइल नहीं बनाई है. एपीआई पासकोड.

  8. फ़ाइल सेव करें.
  9. Android Studio में, अपना मॉड्यूल-लेवल build.gradle खोलें या build.gradle.kts फ़ाइल में बदलाव करें और secrets प्रॉपर्टी में बदलाव करें. अगर secrets प्रॉपर्टी मौजूद नहीं है, इसे जोड़ें.

    propertiesFileName को सेट करने के लिए, प्लग इन की प्रॉपर्टी में बदलाव करें secrets.properties, 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"
    
        // 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 {
        // 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"
    
        // 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 क्लाइंट का इस्तेमाल शुरू करें

किसी गतिविधि या फ़्रैगमेंट में, Android के लिए Places SDK टूल शुरू करें. पहले आपको तय करें कि SDK के किस वर्शन का इस्तेमाल करना है: Android के लिए Places SDK टूल या Android के लिए Places SDK टूल (नया). प्रॉडक्ट के वर्शन के बारे में ज़्यादा जानकारी पाने के लिए, अपने एसडीके का वर्शन चुनें लेख देखें.

यहां दिए गए उदाहरण में, दोनों वर्शन के लिए SDK टूल शुरू करने का तरीका बताया गया है.

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.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);
    
अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है

अब आप Android के लिए Places SDK का इस्तेमाल शुरू करने के लिए तैयार हैं!

पांचवां चरण: Android डिवाइस सेट अप करना

Android के लिए Places SDK टूल का इस्तेमाल करने वाले ऐप्लिकेशन को चलाने के लिए, आपको उसे Android डिवाइस या Android पर डिप्लॉय करना होगा एम्युलेटर, जो Android 5.0 या इसके बाद वाले वर्शन पर आधारित हो और जिसमें तो Google API के ज़रिए ऐसा किया जा सकता है.

  • Android डिवाइस का इस्तेमाल करने के लिए, यहां दिए गए निर्देशों का पालन करें: हार्डवेयर डिवाइस पर ऐप्लिकेशन चलाएं.
  • Android एम्युलेटर का इस्तेमाल करने के लिए, वर्चुअल डिवाइस बनाएं. इसके बाद, Android वर्चुअल डिवाइस (एवीडी) मैनेजर यह सुविधा Android Studio में पहले से मौजूद है.

अगले चरण

अपना प्रोजेक्ट कॉन्फ़िगर करने के बाद, ऐप्लिकेशन के नमूने.