পলিলাইন এবং বহুভুজ রুট এবং এলাকার প্রতিনিধিত্ব করে

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

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.polygons;

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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CustomCap;
import com.google.android.gms.maps.model.Dash;
import com.google.android.gms.maps.model.Dot;
import com.google.android.gms.maps.model.Gap;
import com.google.android.gms.maps.model.JointType;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PatternItem;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.maps.model.RoundCap;
import java.util.Arrays;
import java.util.List;


/**
 * An activity that displays a Google map with polylines to represent paths or routes,
 * and polygons to represent areas.
 */
public class PolyActivity extends AppCompatActivity
        implements
                OnMapReadyCallback,
                GoogleMap.OnPolylineClickListener,
                GoogleMap.OnPolygonClickListener {

    @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 tutorial, we add polylines and polygons to represent routes and areas on the map.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {

        // Add polylines to the map.
        // Polylines are useful to show a route or some other connection between points.
        Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
                .clickable(true)
                .add(
                        new LatLng(-35.016, 143.321),
                        new LatLng(-34.747, 145.592),
                        new LatLng(-34.364, 147.891),
                        new LatLng(-33.501, 150.217),
                        new LatLng(-32.306, 149.248),
                        new LatLng(-32.491, 147.309)));
        // Store a data object with the polyline, used here to indicate an arbitrary type.
        polyline1.setTag("A");
        // Style the polyline.
        stylePolyline(polyline1);

        Polyline polyline2 = googleMap.addPolyline(new PolylineOptions()
                .clickable(true)
                .add(
                        new LatLng(-29.501, 119.700),
                        new LatLng(-27.456, 119.672),
                        new LatLng(-25.971, 124.187),
                        new LatLng(-28.081, 126.555),
                        new LatLng(-28.848, 124.229),
                        new LatLng(-28.215, 123.938)));
        polyline2.setTag("B");
        stylePolyline(polyline2);

        // Add polygons to indicate areas on the map.
        Polygon polygon1 = googleMap.addPolygon(new PolygonOptions()
                .clickable(true)
                .add(
                        new LatLng(-27.457, 153.040),
                        new LatLng(-33.852, 151.211),
                        new LatLng(-37.813, 144.962),
                        new LatLng(-34.928, 138.599)));
        // Store a data object with the polygon, used here to indicate an arbitrary type.
        polygon1.setTag("alpha");
        // Style the polygon.
        stylePolygon(polygon1);

        Polygon polygon2 = googleMap.addPolygon(new PolygonOptions()
                .clickable(true)
                .add(
                        new LatLng(-31.673, 128.892),
                        new LatLng(-31.952, 115.857),
                        new LatLng(-17.785, 122.258),
                        new LatLng(-12.4258, 130.7932)));
        polygon2.setTag("beta");
        stylePolygon(polygon2);

        // Position the map's camera near Alice Springs in the center of Australia,
        // and set the zoom factor so most of Australia shows on the screen.
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-23.684, 133.903), 4));

        // Set listeners for click events.
        googleMap.setOnPolylineClickListener(this);
        googleMap.setOnPolygonClickListener(this);
    }

    private static final int COLOR_BLACK_ARGB = 0xff000000;
    private static final int POLYLINE_STROKE_WIDTH_PX = 12;

    /**
     * Styles the polyline, based on type.
     * @param polyline The polyline object that needs styling.
     */
    private void stylePolyline(Polyline polyline) {
        String type = "";
        // Get the data object stored with the polyline.
        if (polyline.getTag() != null) {
            type = polyline.getTag().toString();
        }

        switch (type) {
            // If no type is given, allow the API to use the default.
            case "A":
                // Use a custom bitmap as the cap at the start of the line.
                polyline.setStartCap(
                        new CustomCap(
                                BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow), 10));
                break;
            case "B":
                // Use a round cap at the start of the line.
                polyline.setStartCap(new RoundCap());
                break;
        }

        polyline.setEndCap(new RoundCap());
        polyline.setWidth(POLYLINE_STROKE_WIDTH_PX);
        polyline.setColor(COLOR_BLACK_ARGB);
        polyline.setJointType(JointType.ROUND);
    }

    private static final int PATTERN_GAP_LENGTH_PX = 20;
    private static final PatternItem DOT = new Dot();
    private static final PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);

    // Create a stroke pattern of a gap followed by a dot.
    private static final List<PatternItem> PATTERN_POLYLINE_DOTTED = Arrays.asList(GAP, DOT);

    /**
     * Listens for clicks on a polyline.
     * @param polyline The polyline object that the user has clicked.
     */
    @Override
    public void onPolylineClick(Polyline polyline) {
        // Flip from solid stroke to dotted stroke pattern.
        if ((polyline.getPattern() == null) || (!polyline.getPattern().contains(DOT))) {
            polyline.setPattern(PATTERN_POLYLINE_DOTTED);
        } else {
            // The default pattern is a solid stroke.
            polyline.setPattern(null);
        }

        Toast.makeText(this, "Route type " + polyline.getTag().toString(),
                Toast.LENGTH_SHORT).show();
    }

    /**
     * Listens for clicks on a polygon.
     * @param polygon The polygon object that the user has clicked.
     */
    @Override
    public void onPolygonClick(Polygon polygon) {
        // Flip the values of the red, green, and blue components of the polygon's color.
        int color = polygon.getStrokeColor() ^ 0x00ffffff;
        polygon.setStrokeColor(color);
        color = polygon.getFillColor() ^ 0x00ffffff;
        polygon.setFillColor(color);

        Toast.makeText(this, "Area type " + polygon.getTag().toString(), Toast.LENGTH_SHORT).show();
    }

    private static final int COLOR_WHITE_ARGB = 0xffffffff;
    private static final int COLOR_DARK_GREEN_ARGB = 0xff388E3C;
    private static final int COLOR_LIGHT_GREEN_ARGB = 0xff81C784;
    private static final int COLOR_DARK_ORANGE_ARGB = 0xffF57F17;
    private static final int COLOR_LIGHT_ORANGE_ARGB = 0xffF9A825;

    private static final int POLYGON_STROKE_WIDTH_PX = 8;
    private static final int PATTERN_DASH_LENGTH_PX = 20;
    private static final PatternItem DASH = new Dash(PATTERN_DASH_LENGTH_PX);

    // Create a stroke pattern of a gap followed by a dash.
    private static final List<PatternItem> PATTERN_POLYGON_ALPHA = Arrays.asList(GAP, DASH);

    // Create a stroke pattern of a dot followed by a gap, a dash, and another gap.
    private static final List<PatternItem> PATTERN_POLYGON_BETA =
        Arrays.asList(DOT, GAP, DASH, GAP);

    /**
     * Styles the polygon, based on type.
     * @param polygon The polygon object that needs styling.
     */
    private void stylePolygon(Polygon polygon) {
        String type = "";
        // Get the data object stored with the polygon.
        if (polygon.getTag() != null) {
            type = polygon.getTag().toString();
        }

        List<PatternItem> pattern = null;
        int strokeColor = COLOR_BLACK_ARGB;
        int fillColor = COLOR_WHITE_ARGB;

        switch (type) {
            // If no type is given, allow the API to use the default.
            case "alpha":
                // Apply a stroke pattern to render a dashed line, and define colors.
                pattern = PATTERN_POLYGON_ALPHA;
                strokeColor = COLOR_DARK_GREEN_ARGB;
                fillColor = COLOR_LIGHT_GREEN_ARGB;
                break;
            case "beta":
                // Apply a stroke pattern to render a line of dots and dashes, and define colors.
                pattern = PATTERN_POLYGON_BETA;
                strokeColor = COLOR_DARK_ORANGE_ARGB;
                fillColor = COLOR_LIGHT_ORANGE_ARGB;
                break;
        }

        polygon.setStrokePattern(pattern);
        polygon.setStrokeWidth(POLYGON_STROKE_WIDTH_PX);
        polygon.setStrokeColor(strokeColor);
        polygon.setFillColor(fillColor);
    }
}

    

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

    // 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.polygons

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.GoogleMap.OnPolygonClickListener
import com.google.android.gms.maps.GoogleMap.OnPolylineClickListener
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.CustomCap
import com.google.android.gms.maps.model.Dash
import com.google.android.gms.maps.model.Dot
import com.google.android.gms.maps.model.Gap
import com.google.android.gms.maps.model.JointType
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.PatternItem
import com.google.android.gms.maps.model.Polygon
import com.google.android.gms.maps.model.PolygonOptions
import com.google.android.gms.maps.model.Polyline
import com.google.android.gms.maps.model.PolylineOptions
import com.google.android.gms.maps.model.RoundCap

/**
 * An activity that displays a Google map with polylines to represent paths or routes,
 * and polygons to represent areas.
 */
class PolyActivity : AppCompatActivity(), OnMapReadyCallback, OnPolylineClickListener, OnPolygonClickListener {

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

    /**
     * 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 tutorial, we add polylines and polygons to represent routes and areas on the map.
     */
    override fun onMapReady(googleMap: GoogleMap) {

        // Add polylines to the map.
        // Polylines are useful to show a route or some other connection between points.
        val polyline1 = googleMap.addPolyline(PolylineOptions()
            .clickable(true)
            .add(
                LatLng(-35.016, 143.321),
                LatLng(-34.747, 145.592),
                LatLng(-34.364, 147.891),
                LatLng(-33.501, 150.217),
                LatLng(-32.306, 149.248),
                LatLng(-32.491, 147.309)))
        // Store a data object with the polyline, used here to indicate an arbitrary type.
        polyline1.tag = "A"
        // Style the polyline.
        stylePolyline(polyline1)

        val polyline2 = googleMap.addPolyline(PolylineOptions()
            .clickable(true)
            .add(
                LatLng(-29.501, 119.700),
                LatLng(-27.456, 119.672),
                LatLng(-25.971, 124.187),
                LatLng(-28.081, 126.555),
                LatLng(-28.848, 124.229),
                LatLng(-28.215, 123.938)))
        polyline2.tag = "B"
        stylePolyline(polyline2)

        // Add polygons to indicate areas on the map.
        val polygon1 = googleMap.addPolygon(PolygonOptions()
            .clickable(true)
            .add(
                LatLng(-27.457, 153.040),
                LatLng(-33.852, 151.211),
                LatLng(-37.813, 144.962),
                LatLng(-34.928, 138.599)))
        // Store a data object with the polygon, used here to indicate an arbitrary type.
        polygon1.tag = "alpha"
        // Style the polygon.
        stylePolygon(polygon1)

        val polygon2 = googleMap.addPolygon(PolygonOptions()
            .clickable(true)
            .add(
                LatLng(-31.673, 128.892),
                LatLng(-31.952, 115.857),
                LatLng(-17.785, 122.258),
                LatLng(-12.4258, 130.7932)))
        polygon2.tag = "beta"
        stylePolygon(polygon2)

        // Position the map's camera near Alice Springs in the center of Australia,
        // and set the zoom factor so most of Australia shows on the screen.
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(-23.684, 133.903), 4f))

        // Set listeners for click events.
        googleMap.setOnPolylineClickListener(this)
        googleMap.setOnPolygonClickListener(this)
    }

    private val COLOR_BLACK_ARGB = -0x1000000
    private val POLYLINE_STROKE_WIDTH_PX = 12

    /**
     * Styles the polyline, based on type.
     * @param polyline The polyline object that needs styling.
     */
    private fun stylePolyline(polyline: Polyline) {
        // Get the data object stored with the polyline.
        val type = polyline.tag?.toString() ?: ""
        when (type) {
            "A" -> {
                // Use a custom bitmap as the cap at the start of the line.
                polyline.startCap = CustomCap(
                    BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow), 10f)
            }
            "B" -> {
               // Use a round cap at the start of the line.
                polyline.startCap = RoundCap()
            }
        }
        polyline.endCap = RoundCap()
        polyline.width = POLYLINE_STROKE_WIDTH_PX.toFloat()
        polyline.color = COLOR_BLACK_ARGB
        polyline.jointType = JointType.ROUND
    }

    private val PATTERN_GAP_LENGTH_PX = 20
    private val DOT: PatternItem = Dot()
    private val GAP: PatternItem = Gap(PATTERN_GAP_LENGTH_PX.toFloat())

    // Create a stroke pattern of a gap followed by a dot.
    private val PATTERN_POLYLINE_DOTTED = listOf(GAP, DOT)

    /**
     * Listens for clicks on a polyline.
     * @param polyline The polyline object that the user has clicked.
     */
    override fun onPolylineClick(polyline: Polyline) {
        // Flip from solid stroke to dotted stroke pattern.
        if (polyline.pattern == null || !polyline.pattern!!.contains(DOT)) {
            polyline.pattern = PATTERN_POLYLINE_DOTTED
        } else {
            // The default pattern is a solid stroke.
            polyline.pattern = null
        }
        Toast.makeText(this, "Route type " + polyline.tag.toString(),
            Toast.LENGTH_SHORT).show()
    }

    /**
     * Listens for clicks on a polygon.
     * @param polygon The polygon object that the user has clicked.
     */
    override fun onPolygonClick(polygon: Polygon) {
        // Flip the values of the red, green, and blue components of the polygon's color.
        var color = polygon.strokeColor xor 0x00ffffff
        polygon.strokeColor = color
        color = polygon.fillColor xor 0x00ffffff
        polygon.fillColor = color
        Toast.makeText(this, "Area type ${polygon.tag?.toString()}", Toast.LENGTH_SHORT).show()
    }

    private val COLOR_WHITE_ARGB = -0x1
    private val COLOR_DARK_GREEN_ARGB = -0xc771c4
    private val COLOR_LIGHT_GREEN_ARGB = -0x7e387c
    private val COLOR_DARK_ORANGE_ARGB = -0xa80e9
    private val COLOR_LIGHT_ORANGE_ARGB = -0x657db
    private val POLYGON_STROKE_WIDTH_PX = 8
    private val PATTERN_DASH_LENGTH_PX = 20

    private val DASH: PatternItem = Dash(PATTERN_DASH_LENGTH_PX.toFloat())

    // Create a stroke pattern of a gap followed by a dash.
    private val PATTERN_POLYGON_ALPHA = listOf(GAP, DASH)

    // Create a stroke pattern of a dot followed by a gap, a dash, and another gap.
    private val PATTERN_POLYGON_BETA = listOf(DOT, GAP, DASH, GAP)

    /**
     * Styles the polygon, based on type.
     * @param polygon The polygon object that needs styling.
     */
    private fun stylePolygon(polygon: Polygon) {
        // Get the data object stored with the polygon.
        val type = polygon.tag?.toString() ?: ""
        var pattern: List<PatternItem>? = null
        var strokeColor = COLOR_BLACK_ARGB
        var fillColor = COLOR_WHITE_ARGB
        when (type) {
            "alpha" -> {
                // Apply a stroke pattern to render a dashed line, and define colors.
                pattern = PATTERN_POLYGON_ALPHA
                strokeColor = COLOR_DARK_GREEN_ARGB
                fillColor = COLOR_LIGHT_GREEN_ARGB
            }
            "beta" -> {
                // Apply a stroke pattern to render a line of dots and dashes, and define colors.
                pattern = PATTERN_POLYGON_BETA
                strokeColor = COLOR_DARK_ORANGE_ARGB
                fillColor = COLOR_LIGHT_ORANGE_ARGB
            }
        }
        polygon.strokePattern = pattern
        polygon.strokeWidth = POLYGON_STROKE_WIDTH_PX.toFloat()
        polygon.strokeColor = strokeColor
        polygon.fillColor = fillColor
    }
}

    

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

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

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

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

অ্যান্ড্রয়েড স্টুডিও অ্যাপটি তৈরির জন্য গ্রেডলকে আহ্বান করে এবং তারপর ডিভাইসে বা এমুলেটরে অ্যাপটি চালায়।

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

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

কোডটি বুঝুন

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

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

আপনার অ্যাপের 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="com.example.polygons.PolyActivity"
            android:exported="true"
            android:label="@string/title_activity_maps">
            <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.polygons.PolyActivity" />
  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. OnMapReadyCallback ইন্টারফেসটি বাস্তবায়ন করুন এবং onMapReady() পদ্ধতিটি ওভাররাইড করুন। যখন GoogleMap অবজেক্টটি উপলব্ধ থাকে তখন API এই কলব্যাকটি আহ্বান করে, যাতে আপনি মানচিত্রে অবজেক্ট যুক্ত করতে পারেন এবং আপনার অ্যাপের জন্য এটি আরও কাস্টমাইজ করতে পারেন:

    জাভা

    public class PolyActivity extends AppCompatActivity
            implements
                    OnMapReadyCallback,
                    GoogleMap.OnPolylineClickListener,
                    GoogleMap.OnPolygonClickListener {
    
        @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 tutorial, we add polylines and polygons to represent routes and areas on the map.
         */
        @Override
        public void onMapReady(GoogleMap googleMap) {
    
            // Add polylines to the map.
            // Polylines are useful to show a route or some other connection between points.
            Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
                    .clickable(true)
                    .add(
                            new LatLng(-35.016, 143.321),
                            new LatLng(-34.747, 145.592),
                            new LatLng(-34.364, 147.891),
                            new LatLng(-33.501, 150.217),
                            new LatLng(-32.306, 149.248),
                            new LatLng(-32.491, 147.309)));
    
            // Position the map's camera near Alice Springs in the center of Australia,
            // and set the zoom factor so most of Australia shows on the screen.
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-23.684, 133.903), 4));
    
            // Set listeners for click events.
            googleMap.setOnPolylineClickListener(this);
            googleMap.setOnPolygonClickListener(this);
        }

    কোটলিন

    class PolyActivity : AppCompatActivity(), OnMapReadyCallback, OnPolylineClickListener, OnPolygonClickListener {
    
        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)
        }
    
        /**
         * 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 tutorial, we add polylines and polygons to represent routes and areas on the map.
         */
        override fun onMapReady(googleMap: GoogleMap) {
    
            // Add polylines to the map.
            // Polylines are useful to show a route or some other connection between points.
            val polyline1 = googleMap.addPolyline(PolylineOptions()
                .clickable(true)
                .add(
                    LatLng(-35.016, 143.321),
                    LatLng(-34.747, 145.592),
                    LatLng(-34.364, 147.891),
                    LatLng(-33.501, 150.217),
                    LatLng(-32.306, 149.248),
                    LatLng(-32.491, 147.309)))
    
            // Position the map's camera near Alice Springs in the center of Australia,
            // and set the zoom factor so most of Australia shows on the screen.
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(-23.684, 133.903), 4f))
    
            // Set listeners for click events.
            googleMap.setOnPolylineClickListener(this)
            googleMap.setOnPolygonClickListener(this)
        }

মানচিত্রে একটি রেখা আঁকতে একটি পলিলাইন যোগ করুন

Polyline হলো সংযুক্ত রেখাখণ্ডের একটি সিরিজ। মানচিত্রে অবস্থানগুলির মধ্যে রুট, পথ বা অন্যান্য সংযোগ উপস্থাপনের জন্য পলিলাইনগুলি কার্যকর।

  1. একটি PolylineOptions অবজেক্ট তৈরি করুন এবং তাতে পয়েন্ট যোগ করুন। প্রতিটি পয়েন্ট মানচিত্রে একটি অবস্থান প্রতিনিধিত্ব করে, যা আপনি অক্ষাংশ এবং দ্রাঘিমাংশের মান ধারণকারী একটি LatLng অবজেক্ট দিয়ে সংজ্ঞায়িত করেন। নীচের কোড নমুনাটি 6 পয়েন্ট সহ একটি পলিলাইন তৈরি করে।

  2. মানচিত্রে পলিলাইন যোগ করতে GoogleMap.addPolyline() এ কল করুন।

    জাভা

    Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
            .clickable(true)
            .add(
                    new LatLng(-35.016, 143.321),
                    new LatLng(-34.747, 145.592),
                    new LatLng(-34.364, 147.891),
                    new LatLng(-33.501, 150.217),
                    new LatLng(-32.306, 149.248),
                    new LatLng(-32.491, 147.309)));

    কোটলিন

    val polyline1 = googleMap.addPolyline(PolylineOptions()
        .clickable(true)
        .add(
            LatLng(-35.016, 143.321),
            LatLng(-34.747, 145.592),
            LatLng(-34.364, 147.891),
            LatLng(-33.501, 150.217),
            LatLng(-32.306, 149.248),
            LatLng(-32.491, 147.309)))

যদি আপনি পলিলাইনে ক্লিক ইভেন্টগুলি পরিচালনা করতে চান তবে পলিলাইনের clickable বিকল্পটি true সেট করুন। এই টিউটোরিয়ালে ইভেন্ট পরিচালনা সম্পর্কে আরও তথ্য রয়েছে।

একটি পলিলাইন দিয়ে ইচ্ছামত ডেটা সংরক্ষণ করুন

আপনি পলিলাইন এবং অন্যান্য জ্যামিতি বস্তুর সাহায্যে ইচ্ছামত ডেটা বস্তু সংরক্ষণ করতে পারেন।

  1. পলিলাইনের সাথে একটি ডেটা অবজেক্ট সংরক্ষণ করতে Polyline.setTag() কল করুন। নীচের কোডটি একটি ইচ্ছাকৃত ট্যাগ ( A ) সংজ্ঞায়িত করে যা এক ধরণের পলিলাইন নির্দেশ করে।

    জাভা

    Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
        .clickable(true)
        .add(
                new LatLng(-35.016, 143.321),
                new LatLng(-34.747, 145.592),
                new LatLng(-34.364, 147.891),
                new LatLng(-33.501, 150.217),
                new LatLng(-32.306, 149.248),
                new LatLng(-32.491, 147.309)));
    // Store a data object with the polyline, used here to indicate an arbitrary type.
    polyline1.setTag("A");

    কোটলিন

    val polyline1 = googleMap.addPolyline(PolylineOptions()
    .clickable(true)
    .add(
        LatLng(-35.016, 143.321),
        LatLng(-34.747, 145.592),
        LatLng(-34.364, 147.891),
        LatLng(-33.501, 150.217),
        LatLng(-32.306, 149.248),
        LatLng(-32.491, 147.309)))
    // Store a data object with the polyline, used here to indicate an arbitrary type.
    polyline1.tag = "A
  2. পরবর্তী বিভাগে দেখানো হয়েছে, Polyline.getTag() ব্যবহার করে ডেটা পুনরুদ্ধার করুন।

আপনার পলিলাইনে কাস্টম স্টাইলিং যোগ করুন

PolylineOptions অবজেক্টে আপনি বিভিন্ন স্টাইলিং বৈশিষ্ট্য নির্দিষ্ট করতে পারেন। স্টাইলিং বিকল্পগুলির মধ্যে রয়েছে স্ট্রোকের রঙ, স্ট্রোকের প্রস্থ, স্ট্রোক প্যাটার্ন, জয়েন্টের ধরণ এবং শুরু এবং শেষের ক্যাপ। যদি আপনি কোনও নির্দিষ্ট বৈশিষ্ট্য নির্দিষ্ট না করেন, তাহলে API সেই বৈশিষ্ট্যের জন্য একটি ডিফল্ট ব্যবহার করে।

নিচের কোডটি লাইনের শেষে একটি গোলাকার ক্যাপ প্রয়োগ করে, এবং পলিলাইনের ধরণের উপর নির্ভর করে একটি ভিন্ন স্টার্ট ক্যাপ প্রয়োগ করে, যেখানে টাইপটি পলিলাইনের জন্য ডেটা অবজেক্টে সংরক্ষিত একটি ইচ্ছাকৃত সম্পত্তি। নমুনাটি একটি স্ট্রোক প্রস্থ, স্ট্রোকের রঙ এবং জয়েন্টের ধরণও নির্দিষ্ট করে:

জাভা

private static final int COLOR_BLACK_ARGB = 0xff000000;
private static final int POLYLINE_STROKE_WIDTH_PX = 12;

/**
 * Styles the polyline, based on type.
 * @param polyline The polyline object that needs styling.
 */
private void stylePolyline(Polyline polyline) {
    String type = "";
    // Get the data object stored with the polyline.
    if (polyline.getTag() != null) {
        type = polyline.getTag().toString();
    }

    switch (type) {
        // If no type is given, allow the API to use the default.
        case "A":
            // Use a custom bitmap as the cap at the start of the line.
            polyline.setStartCap(
                    new CustomCap(
                            BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow), 10));
            break;
        case "B":
            // Use a round cap at the start of the line.
            polyline.setStartCap(new RoundCap());
            break;
    }

    polyline.setEndCap(new RoundCap());
    polyline.setWidth(POLYLINE_STROKE_WIDTH_PX);
    polyline.setColor(COLOR_BLACK_ARGB);
    polyline.setJointType(JointType.ROUND);
}

কোটলিন

private val COLOR_BLACK_ARGB = -0x1000000
private val POLYLINE_STROKE_WIDTH_PX = 12

/**
 * Styles the polyline, based on type.
 * @param polyline The polyline object that needs styling.
 */
private fun stylePolyline(polyline: Polyline) {
    // Get the data object stored with the polyline.
    val type = polyline.tag?.toString() ?: ""
    when (type) {
        "A" -> {
            // Use a custom bitmap as the cap at the start of the line.
            polyline.startCap = CustomCap(
                BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow), 10f)
        }
        "B" -> {
           // Use a round cap at the start of the line.
            polyline.startCap = RoundCap()
        }
    }
    polyline.endCap = RoundCap()
    polyline.width = POLYLINE_STROKE_WIDTH_PX.toFloat()
    polyline.color = COLOR_BLACK_ARGB
    polyline.jointType = JointType.ROUND
}

উপরের কোডটি A পলিলাইন টাইপের স্টার্ট ক্যাপের জন্য একটি কাস্টম বিটম্যাপ নির্দিষ্ট করে এবং 10 পিক্সেলের একটি রেফারেন্স স্ট্রোক প্রস্থ নির্দিষ্ট করে। API রেফারেন্স স্ট্রোক প্রস্থের উপর ভিত্তি করে বিটম্যাপ স্কেল করে। রেফারেন্স স্ট্রোক প্রস্থ নির্দিষ্ট করার সময়, বিটম্যাপ চিত্রটি ডিজাইন করার সময় আপনি যে প্রস্থটি ব্যবহার করেছিলেন তা চিত্রের মূল মাত্রায় সরবরাহ করুন। ইঙ্গিত: একটি চিত্র সম্পাদকে 100% জুমে আপনার বিটম্যাপ চিত্রটি খুলুন এবং চিত্রের সাপেক্ষে লাইন স্ট্রোকের পছন্দসই প্রস্থ প্লট করুন।

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

পললাইনে ক্লিক ইভেন্টগুলি পরিচালনা করুন

  1. Polyline.setClickable() কল করে পলিলাইনে ক্লিকযোগ্য করুন। (ডিফল্টরূপে, পলিলাইন ক্লিকযোগ্য নয় এবং ব্যবহারকারী যখন কোনও পলিলাইনে ট্যাপ করবেন তখন আপনার অ্যাপ কোনও বিজ্ঞপ্তি পাবে না।)

  2. OnPolylineClickListener ইন্টারফেসটি বাস্তবায়ন করুন এবং মানচিত্রে শ্রোতা সেট করতে GoogleMap.setOnPolylineClickListener() এ কল করুন:

    জাভা

    public class PolyActivity extends AppCompatActivity
            implements
                    OnMapReadyCallback,
                    GoogleMap.OnPolylineClickListener,
                    GoogleMap.OnPolygonClickListener {
    
        @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 tutorial, we add polylines and polygons to represent routes and areas on the map.
         */
        @Override
        public void onMapReady(GoogleMap googleMap) {
    
            // Add polylines to the map.
            // Polylines are useful to show a route or some other connection between points.
            Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
                    .clickable(true)
                    .add(
                            new LatLng(-35.016, 143.321),
                            new LatLng(-34.747, 145.592),
                            new LatLng(-34.364, 147.891),
                            new LatLng(-33.501, 150.217),
                            new LatLng(-32.306, 149.248),
                            new LatLng(-32.491, 147.309)));
    
            // Position the map's camera near Alice Springs in the center of Australia,
            // and set the zoom factor so most of Australia shows on the screen.
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-23.684, 133.903), 4));
    
            // Set listeners for click events.
            googleMap.setOnPolylineClickListener(this);
            googleMap.setOnPolygonClickListener(this);
        }

    কোটলিন

    class PolyActivity : AppCompatActivity(), OnMapReadyCallback, OnPolylineClickListener, OnPolygonClickListener {
    
        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)
        }
    
        /**
         * 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 tutorial, we add polylines and polygons to represent routes and areas on the map.
         */
        override fun onMapReady(googleMap: GoogleMap) {
    
            // Add polylines to the map.
            // Polylines are useful to show a route or some other connection between points.
            val polyline1 = googleMap.addPolyline(PolylineOptions()
                .clickable(true)
                .add(
                    LatLng(-35.016, 143.321),
                    LatLng(-34.747, 145.592),
                    LatLng(-34.364, 147.891),
                    LatLng(-33.501, 150.217),
                    LatLng(-32.306, 149.248),
                    LatLng(-32.491, 147.309)))
    
            // Position the map's camera near Alice Springs in the center of Australia,
            // and set the zoom factor so most of Australia shows on the screen.
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(-23.684, 133.903), 4f))
    
            // Set listeners for click events.
            googleMap.setOnPolylineClickListener(this)
            googleMap.setOnPolygonClickListener(this)
        }
  3. onPolylineClick() কলব্যাক পদ্ধতিটি ওভাররাইড করুন। নিম্নলিখিত উদাহরণটি ব্যবহারকারী যখনই পলিলাইনে ক্লিক করেন তখন সলিড এবং ডটেডের মধ্যে লাইনের স্ট্রোক প্যাটার্নকে বিকল্প করে:

    জাভা

    private static final int PATTERN_GAP_LENGTH_PX = 20;
    private static final PatternItem DOT = new Dot();
    private static final PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);
    
    // Create a stroke pattern of a gap followed by a dot.
    private static final List<PatternItem> PATTERN_POLYLINE_DOTTED = Arrays.asList(GAP, DOT);
    
    /**
     * Listens for clicks on a polyline.
     * @param polyline The polyline object that the user has clicked.
     */
    @Override
    public void onPolylineClick(Polyline polyline) {
        // Flip from solid stroke to dotted stroke pattern.
        if ((polyline.getPattern() == null) || (!polyline.getPattern().contains(DOT))) {
            polyline.setPattern(PATTERN_POLYLINE_DOTTED);
        } else {
            // The default pattern is a solid stroke.
            polyline.setPattern(null);
        }
    
        Toast.makeText(this, "Route type " + polyline.getTag().toString(),
                Toast.LENGTH_SHORT).show();
    }

    কোটলিন

    private val PATTERN_GAP_LENGTH_PX = 20
    private val DOT: PatternItem = Dot()
    private val GAP: PatternItem = Gap(PATTERN_GAP_LENGTH_PX.toFloat())
    
    // Create a stroke pattern of a gap followed by a dot.
    private val PATTERN_POLYLINE_DOTTED = listOf(GAP, DOT)
    
    /**
     * Listens for clicks on a polyline.
     * @param polyline The polyline object that the user has clicked.
     */
    override fun onPolylineClick(polyline: Polyline) {
        // Flip from solid stroke to dotted stroke pattern.
        if (polyline.pattern == null || !polyline.pattern!!.contains(DOT)) {
            polyline.pattern = PATTERN_POLYLINE_DOTTED
        } else {
            // The default pattern is a solid stroke.
            polyline.pattern = null
        }
        Toast.makeText(this, "Route type " + polyline.tag.toString(),
            Toast.LENGTH_SHORT).show()
    }

মানচিত্রে এলাকাগুলি উপস্থাপন করতে বহুভুজ যোগ করুন

Polygon হলো এমন একটি আকৃতি যা Polyline মতো একটি ক্রমানুসারে স্থানাঙ্কের একটি সিরিজ নিয়ে গঠিত। পার্থক্য হল বহুভুজ একটি বদ্ধ এলাকাকে সংজ্ঞায়িত করে যার অভ্যন্তরটি পূরণযোগ্য, অন্যদিকে একটি পলিলাইন খোলা প্রান্তযুক্ত।

  1. একটি PolygonOptions অবজেক্ট তৈরি করুন এবং তাতে পয়েন্ট যোগ করুন। প্রতিটি পয়েন্ট মানচিত্রে একটি অবস্থান প্রতিনিধিত্ব করে, যা আপনি অক্ষাংশ এবং দ্রাঘিমাংশের মান ধারণকারী একটি LatLng অবজেক্ট দিয়ে সংজ্ঞায়িত করেন। নীচের কোড নমুনাটি 4 পয়েন্ট সহ একটি বহুভুজ তৈরি করে।

  2. Polygon.setClickable() কল করে বহুভুজকে ক্লিকযোগ্য করুন। (ডিফল্টরূপে, বহুভুজ ক্লিকযোগ্য নয় এবং ব্যবহারকারী যখন একটি বহুভুজ ট্যাপ করবে তখন আপনার অ্যাপ কোনও বিজ্ঞপ্তি পাবে না।) বহুভুজ ক্লিক ইভেন্টগুলি পরিচালনা করা পলিলাইনের ইভেন্টগুলি পরিচালনা করার মতো, যা এই টিউটোরিয়ালে আগে বর্ণিত হয়েছে।

  3. মানচিত্রে বহুভুজ যোগ করতে GoogleMap.addPolygon() এ কল করুন।

  4. বহুভুজ সহ একটি ডেটা অবজেক্ট সংরক্ষণ করতে Polygon.setTag() কল করুন। নীচের কোডটি বহুভুজের জন্য একটি ইচ্ছাকৃত প্রকার ( alpha ) সংজ্ঞায়িত করে।

    জাভা

    // Add polygons to indicate areas on the map.
    Polygon polygon1 = googleMap.addPolygon(new PolygonOptions()
            .clickable(true)
            .add(
                    new LatLng(-27.457, 153.040),
                    new LatLng(-33.852, 151.211),
                    new LatLng(-37.813, 144.962),
                    new LatLng(-34.928, 138.599)));
    // Store a data object with the polygon, used here to indicate an arbitrary type.
    polygon1.setTag("alpha");

    কোটলিন

    // Add polygons to indicate areas on the map.
    val polygon1 = googleMap.addPolygon(PolygonOptions()
        .clickable(true)
        .add(
            LatLng(-27.457, 153.040),
            LatLng(-33.852, 151.211),
            LatLng(-37.813, 144.962),
            LatLng(-34.928, 138.599)))
    // Store a data object with the polygon, used here to indicate an arbitrary type.
    polygon1.tag = "alpha"
    // Style the polygon.

আপনার বহুভুজে কাস্টম স্টাইলিং যোগ করুন

PolygonOptions অবজেক্টে আপনি বেশ কয়েকটি স্টাইলিং বৈশিষ্ট্য নির্দিষ্ট করতে পারেন। স্টাইলিং বিকল্পগুলির মধ্যে রয়েছে স্ট্রোকের রঙ, স্ট্রোকের প্রস্থ, স্ট্রোকের প্যাটার্ন, স্ট্রোক জয়েন্টের ধরণ এবং ফিল রঙ। যদি আপনি কোনও নির্দিষ্ট বৈশিষ্ট্য নির্দিষ্ট না করেন, তাহলে API সেই বৈশিষ্ট্যের জন্য একটি ডিফল্ট ব্যবহার করে।

নিম্নলিখিত কোডটি বহুভুজের ধরণের উপর নির্ভর করে নির্দিষ্ট রঙ এবং স্ট্রোক প্যাটার্ন প্রয়োগ করে, যেখানে প্রকারটি বহুভুজের ডেটা অবজেক্টে সংরক্ষিত একটি ইচ্ছাকৃত সম্পত্তি:

জাভা

private static final int COLOR_WHITE_ARGB = 0xffffffff;
private static final int COLOR_DARK_GREEN_ARGB = 0xff388E3C;
private static final int COLOR_LIGHT_GREEN_ARGB = 0xff81C784;
private static final int COLOR_DARK_ORANGE_ARGB = 0xffF57F17;
private static final int COLOR_LIGHT_ORANGE_ARGB = 0xffF9A825;

private static final int POLYGON_STROKE_WIDTH_PX = 8;
private static final int PATTERN_DASH_LENGTH_PX = 20;
private static final PatternItem DASH = new Dash(PATTERN_DASH_LENGTH_PX);

// Create a stroke pattern of a gap followed by a dash.
private static final List<PatternItem> PATTERN_POLYGON_ALPHA = Arrays.asList(GAP, DASH);

// Create a stroke pattern of a dot followed by a gap, a dash, and another gap.
private static final List<PatternItem> PATTERN_POLYGON_BETA =
    Arrays.asList(DOT, GAP, DASH, GAP);

/**
 * Styles the polygon, based on type.
 * @param polygon The polygon object that needs styling.
 */
private void stylePolygon(Polygon polygon) {
    String type = "";
    // Get the data object stored with the polygon.
    if (polygon.getTag() != null) {
        type = polygon.getTag().toString();
    }

    List<PatternItem> pattern = null;
    int strokeColor = COLOR_BLACK_ARGB;
    int fillColor = COLOR_WHITE_ARGB;

    switch (type) {
        // If no type is given, allow the API to use the default.
        case "alpha":
            // Apply a stroke pattern to render a dashed line, and define colors.
            pattern = PATTERN_POLYGON_ALPHA;
            strokeColor = COLOR_DARK_GREEN_ARGB;
            fillColor = COLOR_LIGHT_GREEN_ARGB;
            break;
        case "beta":
            // Apply a stroke pattern to render a line of dots and dashes, and define colors.
            pattern = PATTERN_POLYGON_BETA;
            strokeColor = COLOR_DARK_ORANGE_ARGB;
            fillColor = COLOR_LIGHT_ORANGE_ARGB;
            break;
    }

    polygon.setStrokePattern(pattern);
    polygon.setStrokeWidth(POLYGON_STROKE_WIDTH_PX);
    polygon.setStrokeColor(strokeColor);
    polygon.setFillColor(fillColor);
}

কোটলিন

private val COLOR_WHITE_ARGB = -0x1
private val COLOR_DARK_GREEN_ARGB = -0xc771c4
private val COLOR_LIGHT_GREEN_ARGB = -0x7e387c
private val COLOR_DARK_ORANGE_ARGB = -0xa80e9
private val COLOR_LIGHT_ORANGE_ARGB = -0x657db
private val POLYGON_STROKE_WIDTH_PX = 8
private val PATTERN_DASH_LENGTH_PX = 20

private val DASH: PatternItem = Dash(PATTERN_DASH_LENGTH_PX.toFloat())

// Create a stroke pattern of a gap followed by a dash.
private val PATTERN_POLYGON_ALPHA = listOf(GAP, DASH)

// Create a stroke pattern of a dot followed by a gap, a dash, and another gap.
private val PATTERN_POLYGON_BETA = listOf(DOT, GAP, DASH, GAP)

/**
 * Styles the polygon, based on type.
 * @param polygon The polygon object that needs styling.
 */
private fun stylePolygon(polygon: Polygon) {
    // Get the data object stored with the polygon.
    val type = polygon.tag?.toString() ?: ""
    var pattern: List<PatternItem>? = null
    var strokeColor = COLOR_BLACK_ARGB
    var fillColor = COLOR_WHITE_ARGB
    when (type) {
        "alpha" -> {
            // Apply a stroke pattern to render a dashed line, and define colors.
            pattern = PATTERN_POLYGON_ALPHA
            strokeColor = COLOR_DARK_GREEN_ARGB
            fillColor = COLOR_LIGHT_GREEN_ARGB
        }
        "beta" -> {
            // Apply a stroke pattern to render a line of dots and dashes, and define colors.
            pattern = PATTERN_POLYGON_BETA
            strokeColor = COLOR_DARK_ORANGE_ARGB
            fillColor = COLOR_LIGHT_ORANGE_ARGB
        }
    }
    polygon.strokePattern = pattern
    polygon.strokeWidth = POLYGON_STROKE_WIDTH_PX.toFloat()
    polygon.strokeColor = strokeColor
    polygon.fillColor = fillColor
}

স্ট্রোক প্যাটার্ন এবং আকার কাস্টমাইজ করার অন্যান্য বিকল্প সম্পর্কে আরও পড়ুন।

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

বৃত্ত বস্তু সম্পর্কে জানুন। বৃত্তগুলি বহুভুজের মতো, কিন্তু এর বৈশিষ্ট্যগুলি বৃত্তের আকৃতি প্রতিফলিত করে।