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

এই টিউটোরিয়াল থেকে আপনি জানতে পারবেন যে কীভাবে আপনার 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-তে, ফাইল > নতুন > প্রোজেক্ট ইমপোর্ট করুন বিকল্প বেছে নিন।
    • 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 বিল্ড টুল ব্যবহার করে আপনার প্রোজেক্ট তৈরি করে।

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

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

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

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

    MAPS_API_KEY=YOUR_API_KEY
    

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

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

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

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

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

  2. Android Studio-তে, রান মেনু বিকল্পে (বা প্লে বোতাম আইকন) ক্লিক করুন। প্রম্পট করা হলে কোনও ডিভাইস বেছে নিন।

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_services_version" />
    
  • আপনার API key উল্লেখ করে একটি meta-data এলিমেন্ট যোগ করুন। এই টিউটোরিয়ালের সাথে থাকা স্যাম্পেল, API key-এর ভ্যালুকে বিল্ড ভেরিয়েবলের সাথে ম্যাপ করে, যা আপনার আগে থেকে সংজ্ঞায়িত করা কী-এর নামের সাথে ম্যাচ করে, 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 ফাইলে, নিম্নলিখিত লাইন আপনার 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>

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

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

  1. আপনার অ্যাক্টিভিটির লেআউট ফাইলে <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" />
  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 তথ্য উইন্ডোর কন্টেন্ট দেখায়। ডিফল্ট অ্যাকশন ব্যবহার করে আপনি খুশি হলে, মার্কারের জন্য ক্লিক লিসনার যোগ করার প্রয়োজন নেই।

পরবর্তী ধাপ

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