इस ट्यूटोरियल में, Android ऐप्लिकेशन में Google मैप जोड़ने का तरीका बताया गया है. मैप में किसी जगह को दिखाने के लिए, पिन मौजूद है, जिसे पिन भी कहा जाता है.
Android के लिए मैप 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 में ट्यूटोरियल प्रोजेक्ट बनाने के लिए यह तरीका अपनाएं.
- Android Studio को डाउनलोड और इंस्टॉल करें.
- Android Studio में Google Play सेवाएं पैकेज जोड़ें.
- अगर इस ट्यूटोरियल को पढ़ना शुरू करते समय आपने ऐसा नहीं किया है, तो Google Maps Android API v2 सैंपल डेटा स्टोर करने की जगह को डाउनलोड या डाउनलोड करें.
ट्यूटोरियल प्रोजेक्ट को इंपोर्ट करें:
- 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 टूल का इस्तेमाल करने की अनुमति मिली हो. ज़्यादा जानकारी के लिए, ये देखें:
अपने ऐप्लिकेशन में एपीआई पासकोड जोड़ें
- अपने प्रोजेक्ट की
local.properties
फ़ाइल खोलें. नीचे दी गई स्ट्रिंग जोड़ें और फिर
YOUR_API_KEY
को अपनी एपीआई कुंजी की वैल्यू से बदलें:MAPS_API_KEY=YOUR_API_KEY
अपना ऐप्लिकेशन बनाने के बाद, Android के लिए सीक्रेट ग्रेडल प्लग इन को एपीआई कुंजी कॉपी करके, Android मेनिफ़ेस्ट में बिल्ड वैरिएबल के तौर पर उपलब्ध कराया जाएगा. इसका तरीका नीचे बताया गया है.
अपना ऐप्लिकेशन बनाना और चलाना
ऐप्लिकेशन बनाने और चलाने के लिए:
Android डिवाइस को अपने कंप्यूटर से कनेक्ट करें. अपने Android डिवाइस पर डेवलपर के लिए सेटिंग और टूल को चालू करने के लिए, निर्देशों का पालन करें.
इसके अलावा, वर्चुअल डिवाइस को कॉन्फ़िगर करने के लिए, Android वर्चुअल डिवाइस (एवीडी) मैनेजर का भी इस्तेमाल किया जा सकता है. एम्युलेटर चुनते समय, ऐसी इमेज चुनें जो Google API में शामिल हो. ज़्यादा जानकारी के लिए, Android Studio प्रोजेक्ट सेट अप करना देखें.
Android Studio में, चलाएं मेन्यू के विकल्प (या प्ले बटन आइकॉन) पर क्लिक करें. स्क्रीन पर बताया गया डिवाइस चुनें.
Android Studio, Gradle को ऐप्लिकेशन बनाने के लिए इस्तेमाल करता है. इसके बाद, यह ऐप्लिकेशन को डिवाइस या एम्युलेटर पर चलाता है. आपको ऑस्ट्रेलिया के पूर्वी तट पर सिडनी को दर्शाता हुआ एक मार्कर दिखाई देगा, जो इस पेज पर मौजूद इमेज से मिलता-जुलता है.
समस्या का हल:
- अगर आपको कोई मैप नहीं दिखता है, तो देखें कि आपने एपीआई कुंजी ली है या नहीं. साथ ही, उसे ऐप्लिकेशन में भी जोड़ा है, जैसा कि ऊपर बताया गया है. एपीआई कुंजी से जुड़े गड़बड़ी के मैसेज के लिए, Android Studio के Android मॉनिटर में लॉग देखें.
- लॉग देखने और ऐप्लिकेशन को डीबग करने के लिए, 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" package="com.example.mapwithmarker"> <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 के लिए मैप SDK का इस्तेमाल करके कोई मैप दिखाएं.
अपनी गतिविधि की लेआउट फ़ाइल में,
<fragment>
एलिमेंट जोड़ें,activity_maps.xml
. यह एलिमेंट, मैप के कंटेनर के तौर पर काम करने औरGoogleMap
ऑब्जेक्ट का ऐक्सेस देने के लिए,SupportMapFragment
के बारे में बताता है. ट्यूटोरियल में, मैप फ़्रैगमेंट के 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" />
अपनी गतिविधि की
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) }
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 टूल, जानकारी विंडो की सामग्री तब दिखाता है, जब उपयोगकर्ता किसी मार्कर पर टैप करता है. अगर आप डिफ़ॉल्ट व्यवहार का इस्तेमाल करते हैं, तो मार्कर के लिए क्लिक लिसनर जोड़ने की ज़रूरत नहीं है.
अगले चरण
मैप ऑब्जेक्ट के बारे में ज़्यादा जानें. साथ ही, यह भी जानें कि मार्कर से क्या किया जा सकता है.