मार्कर के साथ मैप जोड़ना

इस ट्यूटोरियल में, Android ऐप्लिकेशन में Google Maps जोड़ने का तरीका बताया गया है. इस मैप में एक मार्कर होता है, जिसे पिन भी कहा जाता है. यह किसी खास जगह को दिखाता है.

Android के लिए Maps SDK टूल का इस्तेमाल करके Android ऐप्लिकेशन बनाने के लिए, ट्यूटोरियल देखें. सुझाया गया डेवलपमेंट एनवायरमेंट Android Studio है.

कोड पाएं

GitHub से Google Maps के Android API v2 सैंपल का डेटा स्टोर करने की जगह का क्लोन बनाएं या उसे डाउनलोड करें.

गतिविधि का Java वर्शन देखें:

    // Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.example.mapwithmarker;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * An activity that displays a Google map with a marker (pin) to indicate a particular location.
 */
public class MapsMarkerActivity extends AppCompatActivity
        implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve the content view that renders the map.
        setContentView(R.layout.activity_maps);

        // Get the SupportMapFragment and request notification when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Manipulates the map when it's available.
     * The API invokes this callback when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user receives a prompt to install
     * Play services inside the SupportMapFragment. The API invokes this method after the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        // Add a marker in Sydney, Australia,
        // and move the map's camera to the same location.
        LatLng sydney = new LatLng(-33.852, 151.211);
        googleMap.addMarker(new MarkerOptions()
            .position(sydney)
            .title("Marker in Sydney"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

    

गतिविधि का Kotlin वर्शन देखें:

    // Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.example.mapwithmarker

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions

/**
 * An activity that displays a Google map with a marker (pin) to indicate a particular location.
 */
class MapsMarkerActivity : AppCompatActivity(), OnMapReadyCallback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Retrieve the content view that renders the map.
        setContentView(R.layout.activity_maps)

        // Get the SupportMapFragment and request notification when the map is ready to be used.
        val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
        mapFragment?.getMapAsync(this)
    }

    override fun onMapReady(googleMap: GoogleMap) {
      val sydney = LatLng(-33.852, 151.211)
      googleMap.addMarker(
        MarkerOptions()
          .position(sydney)
          .title("Marker in Sydney")
      )
      googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
    }
}

    

अपना डेवलपमेंट प्रोजेक्ट सेट अप करें

Android Studio में ट्यूटोरियल प्रोजेक्ट बनाने के लिए, यह तरीका अपनाएं.

  1. Android Studio को डाउनलोड करें और इंस्टॉल करें.
  2. Android Studio में Google Play services का पैकेज जोड़ें.
  3. Google Maps के Android API v2 सैंपल डेटा स्टोर करने की जगह का क्लोन बनाएं या डाउनलोड करें अगर इस ट्यूटोरियल को पढ़ना शुरू करते समय आपने ऐसा नहीं किया था, तो.
  4. ट्यूटोरियल प्रोजेक्ट इंपोर्ट करें:

    • Android Studio में, फ़ाइल > नया > प्रोजेक्ट इंपोर्ट करें चुनें.
    • उस जगह पर जाएं जहां आपने Google Maps Android API v2 सैंपल डेटा डाउनलोड करने के बाद डेटा स्टोर करने की जगह को सेव किया था.
    • इस जगह पर MapWithMarker प्रोजेक्ट ढूंढें:
      PATH-TO-SAVED-REPO/android-samples/tutorials/java/MapWithMarker (Java) या
      PATH-TO-SAVED-REPO/android-samples/tutorials/kotlin/MapWithMarker (Kotlin)
    • प्रोजेक्ट डायरेक्ट्री चुनें और खोलें पर क्लिक करें. Android Studio अब Gradle बिल्ड टूल इस्तेमाल करके आपका प्रोजेक्ट बनाता है.

ज़रूरी एपीआई चालू करें और एपीआई पासकोड पाएं

इस ट्यूटोरियल को पूरा करने के लिए, आपके पास ऐसा Google Cloud प्रोजेक्ट होना चाहिए जिसमें ज़रूरी एपीआई चालू हों. साथ ही, एक एपीआई पासकोड ऐसा होना चाहिए जिसे Android के लिए Maps SDK टूल का इस्तेमाल करने की अनुमति हो. ज़्यादा जानकारी के लिए, देखें:

अपने ऐप्लिकेशन में एपीआई पासकोड जोड़ना

  1. अपने प्रोजेक्ट की local.properties फ़ाइल खोलें.
  2. नीचे दी गई स्ट्रिंग जोड़ें और फिर YOUR_API_KEY को अपनी एपीआई कुंजी की वैल्यू से बदलें:

    MAPS_API_KEY=YOUR_API_KEY
    

    जब ऐप्लिकेशन बनाया जाएगा, तो Android के लिए Secrets Gradle प्लग इन एपीआई पासकोड को कॉपी कर देगा और उसे Android मेनिफ़ेस्ट में बिल्ड वैरिएबल के तौर पर उपलब्ध करा देगा, जैसा कि नीचे बताया गया है.

अपना ऐप्लिकेशन बनाना और चलाना

ऐप्लिकेशन बनाने और चलाने के लिए:

  1. अपने कंप्यूटर से कोई Android डिवाइस कनेक्ट करें. अपने Android डिवाइस पर डेवलपर के लिए सेटिंग और टूल को चालू करने के लिए, instructions का पालन करें. साथ ही, डिवाइस का पता लगाने के लिए अपने सिस्टम को कॉन्फ़िगर करें.

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

  2. Android Studio में, Run मेन्यू के विकल्प या 'चलाएं' बटन के आइकॉन पर क्लिक करें. निर्देश के हिसाब से डिवाइस चुनें.

Android Studio ऐप्लिकेशन बनाने के लिए Gradle को शुरू करता है और इसके बाद ऐप्लिकेशन को डिवाइस पर या एम्युलेटर पर चलाता है. आपको एक मैप दिखेगा, जिसमें एक मार्कर होगा, जो ऑस्ट्रेलिया के पूर्वी तट पर सिडनी की ओर इशारा करता है. यह तस्वीर, इस पेज पर दिखाई गई इमेज जैसी है.

समस्या का हल:

  • अगर आपको कोई मैप नहीं दिखता है, तो देखें कि आपने एपीआई पासकोड लिया है या नहीं और उसे ऐप्लिकेशन में जोड़ा है या नहीं, जैसा कि ऊपर बताया गया है. एपीआई पासकोड से जुड़ी गड़बड़ी के मैसेज के लिए, Android Studio के Android Monitor का लॉग देखें.
  • लॉग देखने और ऐप्लिकेशन को डीबग करने के लिए, Android Studio डीबग करने वाले टूल का इस्तेमाल करें.

कोड को समझना

ट्यूटोरियल के इस हिस्से में MapWithMarker ऐप्लिकेशन के अहम हिस्सों के बारे में जानकारी मिलती है. इससे आपको मिलता-जुलता ऐप्लिकेशन बनाने का तरीका समझने में मदद मिलती है.

अपना Android मेनिफ़ेस्ट देखें

अपने ऐप्लिकेशन की AndroidManifest.xml फ़ाइल में इन एलिमेंट को नोट करें:

  • Google Play सेवाओं के जिस वर्शन के साथ ऐप्लिकेशन को कंपाइल किया गया था उसे एम्बेड करने के लिए, meta-data एलिमेंट जोड़ें.

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    
  • अपनी एपीआई कुंजी के बारे में बताने वाला meta-data एलिमेंट जोड़ें. इस ट्यूटोरियल के साथ दिया गया सैंपल, एपीआई पासकोड की वैल्यू को बिल्ड वैरिएबल तक मैप करता है. यह वैल्यू, पहले तय की गई कुंजी के नाम MAPS_API_KEY से मेल खाती है. जब आप अपना ऐप्लिकेशन बनाएंगे, तब Android के लिए Secrets Gradle प्लग इन आपकी local.properties फ़ाइल की कुंजियों को मेनिफ़ेस्ट बिल्ड वैरिएबल के तौर पर उपलब्ध करा देगा.

    <meta-data
      android:name="com.google.android.geo.API_KEY"
      android:value="${MAPS_API_KEY}" />
    

    आपकी build.gradle फ़ाइल में, नीचे दी गई लाइन आपकी एपीआई कुंजी को Android मेनिफ़ेस्ट में भेजती है.

      id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    

नीचे पूरे मेनिफ़ेस्ट का एक उदाहरण दिया गया है:

<?xml version="1.0" encoding="utf-8"?>
<!--
 Copyright 2020 Google LLC

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <!--
             The API key for Google Maps-based APIs.
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="${MAPS_API_KEY}" />

        <activity
            android:name=".MapsMarkerActivity"
            android:label="@string/title_activity_maps"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

मैप जोड़ें

Android के लिए Maps SDK टूल का इस्तेमाल करके, मैप दिखाएं.

  1. अपनी गतिविधि की लेआउट फ़ाइल में कोई <fragment> एलिमेंट जोड़ें, activity_maps.xml. इस एलिमेंट में SupportMapFragment के बारे में बताया गया है, ताकि मैप के कंटेनर के तौर पर काम किया जा सके और GoogleMap ऑब्जेक्ट का ऐक्सेस दिया जा सके. यह ट्यूटोरियल, मैप फ़्रैगमेंट के Android सपोर्ट लाइब्रेरी वर्शन का इस्तेमाल करता है. इससे यह पक्का किया जाता है कि Android फ़्रेमवर्क के पुराने वर्शन के साथ, पुराने वर्शन के साथ काम किया जा सके.

    <!--
     Copyright 2020 Google LLC
    
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
    
          http://www.apache.org/licenses/LICENSE-2.0
    
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
    -->
    
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
    
    
  2. अपनी गतिविधि के onCreate() तरीके में, लेआउट फ़ाइल को कॉन्टेंट व्यू के तौर पर सेट करें. FragmentManager.findFragmentById() पर कॉल करके, मैप फ़्रैगमेंट के लिए हैंडल पाएं. इसके बाद, मैप कॉलबैक के लिए रजिस्टर करने के लिए, getMapAsync() का इस्तेमाल करें:

    Java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve the content view that renders the map.
        setContentView(R.layout.activity_maps);
    
        // Get the SupportMapFragment and request notification when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    

    Kotlin

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Retrieve the content view that renders the map.
        setContentView(R.layout.activity_maps)
    
        // Get the SupportMapFragment and request notification when the map is ready to be used.
        val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
        mapFragment?.getMapAsync(this)
    }
    
  3. OnMapReadyCallback इंटरफ़ेस को लागू करें और GoogleMap ऑब्जेक्ट उपलब्ध होने पर मैप सेट अप करने के लिए, onMapReady() तरीके को बदलें:

    Java

    public class MapsMarkerActivity extends AppCompatActivity
            implements OnMapReadyCallback {
    
        // ...
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
            LatLng sydney = new LatLng(-33.852, 151.211);
            googleMap.addMarker(new MarkerOptions()
                .position(sydney)
                .title("Marker in Sydney"));
        }
    }
    

    Kotlin

    class MapsMarkerActivity : AppCompatActivity(), OnMapReadyCallback {
    
        // ...
    
        override fun onMapReady(googleMap: GoogleMap) {
          val sydney = LatLng(-33.852, 151.211)
          googleMap.addMarker(
            MarkerOptions()
              .position(sydney)
              .title("Marker in Sydney")
          )
        }
    }
    

डिफ़ॉल्ट रूप से, जब उपयोगकर्ता किसी मार्कर पर टैप करता है, तो Android के लिए Maps SDK टूल, जानकारी विंडो का कॉन्टेंट दिखाता है. अगर आपको डिफ़ॉल्ट व्यवहार का इस्तेमाल करना है, तो मार्कर के लिए क्लिक लिसनर जोड़ने की कोई ज़रूरत नहीं है.

अगले चरण

मैप ऑब्जेक्ट के बारे में ज़्यादा जानें. साथ ही, यह भी जानें कि मार्कर की मदद से क्या-क्या किया जा सकता है.