মার্কার সহ ম্যাপ যোগ করা

এই টিউটোরিয়াল থেকে আপনি জানতে পারবেন যে কীভাবে আপনার Android অ্যাপে Google Map যোগ করবেন। ম্যাপে একটি মার্কার থাকে, যাকে পিনও বলা হয়, এটি নির্দিষ্ট লোকেশন নির্দেশ করে।

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 পরিষেবা প্যাকেজ যোগ করুন।
  3. আপনি যদি এই টিউটোরিয়ালটি পড়া শুরু করার সময় এটি না করে থাকেন, তাহলে Google Maps Android API v2 স্যাম্পেল রিপোজিটরি ক্লোন বা ডাউনলোড করুন।
  4. Android Studio-তে প্রোজেক্ট খুলুন:

    • Android Studio-তে, ফাইল > খুলুন বিকল্প বেছে নিন।
    • Google Maps Android API v2 স্যাম্পেল রিপোজিটরি ডাউনলোড করার পরে যেখানে সেভ করেছেন সেখানে যান, রুট android-samples ডিরেক্টরি বেছে নিন এবং খুলুন বিকল্পে ক্লিক করুন। Android Studio, Gradle ব্যবহার করে মাল্টি-মডিউল প্রোজেক্ট সিঙ্ক করে।
    • টুলবারের Android Studio রান কনফিগারেশন মেনু থেকে tutorials.java.MapWithMarker (tutorials/java/MapWithMarker-এ সোর্স) অথবা tutorials.kotlin.MapWithMarker (tutorials/kotlin/MapWithMarker-এ সোর্স) বেছে নিন।

প্রয়োজনীয় API চালু করুন এবং API key পান

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

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

  1. রুট android-samples ডিরেক্টরিতে (local.defaults.properties-এর মতো একই ফোল্ডার), secrets.properties ফাইল খুলুন বা তৈরি করুন। ভার্সন কন্ট্রোলে এই ফাইল চেক করবেন না।
  2. secrets.properties-এ নিম্নলিখিত স্ট্রিং যোগ করুন এবং YOUR_API_KEY-এর জায়গায় আপনার API key-এর ভ্যালু লিখুন:

    MAPS_API_KEY=YOUR_API_KEY
    

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

আপনার অ্যাপ তৈরি করুন ও চালান

অ্যাপ তৈরি ও রান করতে:

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

    এছাড়াও, আপনি Android Studio-তে ডিভাইস ম্যানেজার ব্যবহার করে ভার্চুয়াল ডিভাইস কনফিগার করতে পারবেন। এমুলেটর বেছে নেওয়ার সময়, Play কলামে Google Play আইকন সহ ডিভাইস প্রোফাইল বেছে নিন যাতে সিস্টেম ইমেজে Google API অন্তর্ভুক্ত থাকে। আরও তথ্যের জন্য, Android Studio প্রোজেক্ট সেট-আপ করুন দেখুন।

  2. Android Studio-তে, রান কনফিগারেশন মেনুতে tutorials.java.MapWithMarker বা tutorials.kotlin.MapWithMarker বেছে নেওয়া হয়েছে কিনা তা যাচাই করুন, তারপর রান করুন বিকল্পে ক্লিক করুন এবং প্রম্পট অনুযায়ী একটি ডিভাইস বেছে নিন।

    এছাড়াও, আপনি কমান্ড লাইনে রুট android-samples ডিরেক্টরি থেকে টিউটোরিয়াল তৈরি করতে পারবেন:

    ./gradlew :tutorials:java:MapWithMarker:assembleDebug
    ./gradlew :tutorials:kotlin:MapWithMarker:assembleDebug
    

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

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

কোডটি বুঝুন

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

আপনার Android ম্যানিফেস্ট চেক করুন

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

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

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

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

    আপনার মডিউল-লেভেল build.gradle.kts ফাইলে, com.google.android.libraries.mapsplatform.secrets-gradle-plugin প্লাগ-ইন এবং নিম্নলিখিত secrets ব্লক secrets.properties থেকে আপনার API key পড়ার জন্য Secrets Gradle প্লাগ-ইন কনফিগার করে (ফলব্যাক হিসেবে local.defaults.properties সহ) এবং এটি আপনার Android ম্যানিফেস্টে পাস করে:

    secrets {
        // To add your Maps API key to this project:
        // 1. If the secrets.properties file does not exist, create it in the root directory (the same folder as the root 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.defaultt;
    }build.gradle.kts

নিম্নলিখিত উদাহরণে একটি সম্পূর্ণ ম্যানিফেস্ট দেখানো হয়েছে:

<?xml version="1.0" encodin>g<="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 permission>s <and
 limitations under the License.
--

manifest xmlns:android=&quo>t;http<://schemas.android.com/apk/res/android"

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

        meta-data
            android:name=>"com.<google.android.gms.version"
            android:value="@i>nteger/go<ogle_play_services_version" /

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

        activit>y
           < android:name>=".MapsMarke<rActivity"
            android:label="@s>tring/title_activi<ty_maps"
            android:exported="true">;
           < intent-filter>
        <        a>ction< android:nam>e=<"and>roid.intent.action.MAIN" /

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

/manifest

ম্যাপ যোগ করুন

Android-এর জন্য Maps SDK ব্যবহার করে ম্যাপ দেখানো।

  1. আপনার অ্যাক্টিভিটির লেআউট ফাইলে <fragment> এলিমেন্ট যোগ করুন, activity_maps.xml। এই এলিমেন্টটি ম্যাপের জন্য কন্টেনার হিসেবে কাজ করতে এবং GoogleMap অবজেক্টে অ্যাক্সেস প্রদান করতে একটি SupportMapFragment সংজ্ঞায়িত করে। টিউটোরিয়ালটি ম্যাপ ফ্র্যাগমেন্টের AndroidX ভার্সন ব্যবহার করে, যাতে 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 u>nd<er 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"),
            )
        }
    }

ডিফল্ট হিসেবে, ব্যবহারকারী কোনও মার্কার ট্যাপ করলে, Maps SDK for Android তথ্য উইন্ডোর কন্টেন্ট দেখায়। ডিফল্ট অ্যাকশন ব্যবহার করে আপনি খুশি হলে, মার্কারের জন্য ক্লিক লিসনার যোগ করার প্রয়োজন নেই।

পরবর্তী ধাপ

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