একটি মার্কার সহ একটি মানচিত্র যোগ করা হচ্ছে

এই টিউটোরিয়ালটি আপনাকে দেখাবে কিভাবে আপনার অ্যান্ড্রয়েড অ্যাপে একটি গুগল ম্যাপ যোগ করবেন। মানচিত্রে একটি মার্কার রয়েছে, যাকে পিনও বলা হয়, যা একটি নির্দিষ্ট অবস্থান নির্দেশ করে।

Android এর জন্য Maps SDK ব্যবহার করে একটি Android অ্যাপ তৈরি করতে টিউটোরিয়ালটি অনুসরণ করুন। প্রস্তাবিত ডেভেলপমেন্ট পরিবেশ হল Android Studio

কোডটি পান

GitHub থেকে Google Maps Android API v2 Samples সংগ্রহস্থল ক্লোন করুন অথবা ডাউনলোড করুন

কার্যকলাপের জাভা সংস্করণটি দেখুন:

    // 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));
    }
}

    

কার্যকলাপের কোটলিন সংস্করণটি দেখুন:

    // 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))
    }
}

    

আপনার উন্নয়ন প্রকল্প সেট আপ করুন

অ্যান্ড্রয়েড স্টুডিওতে টিউটোরিয়াল প্রকল্প তৈরি করতে এই পদক্ষেপগুলি অনুসরণ করুন।

  1. অ্যান্ড্রয়েড স্টুডিও ডাউনলোড এবং ইনস্টল করুন
  2. অ্যান্ড্রয়েড স্টুডিওতে গুগল প্লে সার্ভিস প্যাকেজ যোগ করুন
  3. এই টিউটোরিয়ালটি পড়া শুরু করার সময় যদি আপনি Google Maps Android API v2 Samples রিপোজিটরিটি ক্লোন বা ডাউনলোড না করে থাকেন, তাহলে তা ক্লোন করুন।
  4. টিউটোরিয়াল প্রকল্পটি আমদানি করুন:

    • অ্যান্ড্রয়েড স্টুডিওতে, File > New > Import Project নির্বাচন করুন।
    • ডাউনলোড করার পর, আপনি যেখানে Google Maps Android API v2 Samples সংগ্রহস্থলটি সংরক্ষণ করেছিলেন সেখানে যান।
    • এই স্থানে MapWithMarker প্রকল্পটি খুঁজুন:
      PATH-TO-SAVED-REPO /android-samples/tutorials/java/MapWithMarker (জাভা) অথবা
      PATH-TO-SAVED-REPO /android-samples/tutorials/kotlin/MapWithMarker (কোটলিন)
    • প্রোজেক্ট ডিরেক্টরিটি নির্বাচন করুন, তারপর Open এ ক্লিক করুন। অ্যান্ড্রয়েড স্টুডিও এখন গ্র্যাডেল বিল্ড টুল ব্যবহার করে আপনার প্রোজেক্ট তৈরি করে।

প্রয়োজনীয় API গুলি সক্রিয় করুন এবং একটি API কী পান

এই টিউটোরিয়ালটি সম্পূর্ণ করার জন্য, আপনার প্রয়োজনীয় API গুলি সক্ষম করে একটি Google ক্লাউড প্রকল্প এবং Android এর জন্য Maps SDK ব্যবহারের জন্য অনুমোদিত একটি API কী প্রয়োজন। আরও বিস্তারিত জানার জন্য, দেখুন:

আপনার অ্যাপে API কী যোগ করুন

  1. আপনার প্রোজেক্টের local.properties ফাইলটি খুলুন।
  2. নিম্নলিখিত স্ট্রিংটি যোগ করুন এবং তারপর YOUR_API_KEY আপনার API কী এর মান দিয়ে প্রতিস্থাপন করুন:

    MAPS_API_KEY=YOUR_API_KEY
    

    যখন আপনি আপনার অ্যাপ তৈরি করবেন, তখন Android এর জন্য Secrets Gradle Plugin API কীটি কপি করবে এবং এটিকে Android ম্যানিফেস্টে একটি বিল্ড ভেরিয়েবল হিসেবে উপলব্ধ করবে, যেমনটি নীচে ব্যাখ্যা করা হয়েছে।

আপনার অ্যাপ তৈরি করুন এবং চালান

অ্যাপটি তৈরি এবং চালানোর জন্য:

  1. আপনার কম্পিউটারের সাথে একটি অ্যান্ড্রয়েড ডিভাইস সংযুক্ত করুন। আপনার অ্যান্ড্রয়েড ডিভাইসে ডেভেলপার বিকল্পগুলি সক্ষম করতে নির্দেশাবলী অনুসরণ করুন এবং ডিভাইসটি সনাক্ত করার জন্য আপনার সিস্টেমটি কনফিগার করুন।

    বিকল্পভাবে, আপনি একটি ভার্চুয়াল ডিভাইস কনফিগার করতে অ্যান্ড্রয়েড ভার্চুয়াল ডিভাইস (AVD) ম্যানেজার ব্যবহার করতে পারেন। একটি এমুলেটর নির্বাচন করার সময়, নিশ্চিত করুন যে আপনি এমন একটি ছবি বেছে নিয়েছেন যাতে Google API গুলি অন্তর্ভুক্ত রয়েছে। আরও বিস্তারিত জানার জন্য, একটি অ্যান্ড্রয়েড স্টুডিও প্রকল্প সেট আপ করুন দেখুন।

  2. অ্যান্ড্রয়েড স্টুডিওতে, রান মেনু অপশনে (অথবা প্লে বোতাম আইকনে) ক্লিক করুন। অনুরোধ অনুসারে একটি ডিভাইস নির্বাচন করুন।

অ্যান্ড্রয়েড স্টুডিও অ্যাপটি তৈরির জন্য গ্র্যাডেলকে ব্যবহার করে, এবং তারপর ডিভাইসে বা এমুলেটরে অ্যাপটি চালায়। আপনি অস্ট্রেলিয়ার পূর্ব উপকূলে সিডনির দিকে নির্দেশিত একটি মার্কারের মানচিত্র দেখতে পাবেন, যা এই পৃষ্ঠার ছবির মতো।

সমস্যা সমাধান:

কোডটি বুঝুন

টিউটোরিয়ালের এই অংশে MapWithMarker অ্যাপের সবচেয়ে গুরুত্বপূর্ণ অংশগুলি ব্যাখ্যা করা হয়েছে, যাতে আপনি বুঝতে পারেন কিভাবে একই ধরণের অ্যাপ তৈরি করতে হয়।

আপনার অ্যান্ড্রয়েড ম্যানিফেস্ট পরীক্ষা করুন

আপনার অ্যাপের AndroidManifest.xml ফাইলে নিম্নলিখিত উপাদানগুলি লক্ষ্য করুন:

  • অ্যাপটি যে ভার্সন দিয়ে তৈরি করা হয়েছে, সেই ভার্সনটিতে গুগল প্লে সার্ভিস এম্বেড করার জন্য একটি meta-data এলিমেন্ট যোগ করুন।

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    
  • আপনার API কী নির্দিষ্ট করে একটি meta-data উপাদান যোগ করুন। এই টিউটোরিয়ালের সাথে থাকা নমুনাটি API কী-এর মানকে একটি বিল্ড ভেরিয়েবলের সাথে ম্যাপ করে যা আপনি আগে যে কী-এর নাম MAPS_API_KEY সংজ্ঞায়িত করেছেন তার সাথে মিলে যায়। যখন আপনি আপনার অ্যাপ তৈরি করবেন, তখন Android-এর জন্য Secrets Gradle Plugin আপনার local.properties ফাইলের কীগুলিকে ম্যানিফেস্ট বিল্ড ভেরিয়েবল হিসেবে উপলব্ধ করবে।

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

    আপনার build.gradle ফাইলে, নিম্নলিখিত লাইনটি আপনার API কীটি আপনার 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>

একটি মানচিত্র যোগ করুন

অ্যান্ড্রয়েডের জন্য ম্যাপস SDK ব্যবহার করে একটি ম্যাপ প্রদর্শন করুন।

  1. আপনার অ্যাক্টিভিটির লেআউট ফাইল, activity_maps.xml এ একটি <fragment> উপাদান যোগ করুন। এই উপাদানটি একটি SupportMapFragment মানচিত্রের জন্য একটি ধারক হিসেবে কাজ করার জন্য এবং GoogleMap অবজেক্টে অ্যাক্সেস প্রদানের জন্য সংজ্ঞায়িত করে। টিউটোরিয়ালটি অ্যান্ড্রয়েড ফ্রেমওয়ার্কের পূর্ববর্তী সংস্করণগুলির সাথে ব্যাকওয়ার্ড সামঞ্জস্যতা নিশ্চিত করার জন্য মানচিত্রের অংশের অ্যান্ড্রয়েড সাপোর্ট লাইব্রেরি সংস্করণ ব্যবহার করে।

    <!--
     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() ব্যবহার করুন:

    জাভা

    @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);
    }

    কোটলিন

    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. GoogleMap অবজেক্ট উপলব্ধ থাকলে ম্যাপ সেট আপ করার জন্য OnMapReadyCallback ইন্টারফেসটি বাস্তবায়ন করুন এবং onMapReady() পদ্ধতিটি ওভাররাইড করুন:

    জাভা

    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"));
        }
    }

    কোটলিন

    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 তথ্য উইন্ডোর বিষয়বস্তু প্রদর্শন করে। আপনি যদি ডিফল্ট আচরণ ব্যবহার করতে খুশি হন তবে মার্কারের জন্য ক্লিক লিসেনার যোগ করার কোনও প্রয়োজন নেই।

পরবর্তী পদক্ষেপ

মানচিত্রের বস্তু এবং মার্কার দিয়ে আপনি কী করতে পারেন সে সম্পর্কে আরও জানুন।