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

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

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

कोड प्राप्त करें

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

गतिविधि का 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 में 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 (कोटलिन)
    • प्रोजेक्ट डायरेक्ट्री चुनें. इसके बाद, खोलें पर क्लिक करें. Android Studio अब बिल्ड Gradle बिल्ड टूल का इस्तेमाल करके, अपने प्रोजेक्ट में मदद कर सकता है.

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

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

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

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

    MAPS_API_KEY=YOUR_API_KEY
    

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

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

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

  1. किसी Android डिवाइस को अपने कंप्यूटर से कनेक्ट करें. फ़ॉलो करें अपने Android पर डेवलपर के लिए सेटिंग और टूल की सुविधा चालू करने के लिए निर्देश डिवाइस पर जाएं और अपने सिस्टम को कॉन्फ़िगर करें, ताकि डिवाइस का पता लगाया जा सके.

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

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

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

समस्या का हल:

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

कोड को समझना

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

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

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

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

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    
  • एपीआई पासकोड की जानकारी देने वाला meta-data एलिमेंट जोड़ें. साथ में दिया गया सैंपल यह ट्यूटोरियल, एपीआई पासकोड की वैल्यू को बिल्ड वैरिएबल से मैच करने के लिए मैप करता है आपके ज़रिए पहले तय की गई कुंजी का नाम, MAPS_API_KEY. ऐप्लिकेशन बनाते समय, Android के लिए सीक्रेट ग्रेडल प्लगिन आपकी 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 इंटरफ़ेस लागू करें और मैप सेट अप करने के लिए, onMapReady() तरीके को ओवरराइड करें, जब GoogleMap ऑब्जेक्ट उपलब्ध है:

    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, उपयोगकर्ता के किसी मार्कर पर टैप करने पर जानकारी विंडो. क्लिक लिसनर जोड़ने की ज़रूरत नहीं है इस्तेमाल करें.

अगले चरण

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