একটি ঠিকানা ফর্ম স্থান স্বয়ংসম্পূর্ণ যোগ করুন

ডেলিভারি ঠিকানা, বিলিং তথ্য বা ইভেন্টের তথ্য পূরণ করার সময়, ফর্মগুলিতে ‘প্লেস অটোকমপ্লিট’ চালু করলে ঠিকানা লেখার সময় ব্যবহারকারীদের টাইপিংয়ের সংখ্যা ও ভুল কমে যায়। এই টিউটোরিয়ালে একটি ইনপুট ফিল্ডে ‘প্লেস অটোকমপ্লিট’ চালু করার, ব্যবহারকারীর নির্বাচিত ঠিকানার অংশ দিয়ে অ্যাড্রেস ফর্মের ফিল্ডগুলো পূরণ করার এবং চাক্ষুষ নিশ্চিতকরণের সুবিধার্থে নির্বাচিত ঠিকানাটি একটি মানচিত্রে দেখানোর প্রয়োজনীয় ধাপগুলো দেখানো হয়েছে।

ভিডিও: প্লেস অটোকমপ্লিট ব্যবহার করে ঠিকানা ফর্ম উন্নত করুন

ঠিকানা ফর্ম

অ্যান্ড্রয়েড

আইওএস

ওয়েব

গুগল ম্যাপস প্ল্যাটফর্ম মোবাইল প্ল্যাটফর্ম এবং ওয়েবের জন্য একটি প্লেস অটোকমপ্লিট উইজেট প্রদান করে। পূর্ববর্তী চিত্রগুলিতে দেখানো এই উইজেটটি একটি সার্চ ডায়ালগ প্রদান করে, যাতে বিল্ট-ইন অটোকমপ্লিট কার্যকারিতা রয়েছে এবং আপনি এটিকে অবস্থান-ভিত্তিক অনুসন্ধানের জন্য অপ্টিমাইজও করতে পারেন।

কোডটি নিন

গিটহাব থেকে অ্যান্ড্রয়েড ডেমোর জন্য গুগল প্লেসেস এসডিকে রিপোজিটরিটি ক্লোন বা ডাউনলোড করুন।

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

    /*
 * Copyright 2022 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
 *
 *     https://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.placesdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.example.placesdemo.databinding.AutocompleteAddressActivityBinding;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
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.MapStyleOptions;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.AddressComponent;
import com.google.android.libraries.places.api.model.AddressComponents;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.model.PlaceTypes;
import com.google.android.libraries.places.api.net.PlacesClient;
import com.google.android.libraries.places.widget.Autocomplete;
import com.google.android.libraries.places.widget.model.AutocompleteActivityMode;

import java.util.Arrays;
import java.util.List;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static com.google.maps.android.SphericalUtil.computeDistanceBetween;
import androidx.activity.EdgeToEdge;

/**
 * Activity for using Place Autocomplete to assist filling out an address form.
 */
@SuppressWarnings("FieldCanBeLocal")
public class AutocompleteAddressActivity extends AppCompatActivity implements OnMapReadyCallback {

    private static final String TAG = "ADDRESS_AUTOCOMPLETE";
    private static final String MAP_FRAGMENT_TAG = "MAP";
    private LatLng coordinates;
    private boolean checkProximity = false;
    private SupportMapFragment mapFragment;
    private GoogleMap map;
    private Marker marker;
    private PlacesClient placesClient;
    private View mapPanel;
    private LatLng deviceLocation;
    private static final double acceptedProximity = 150;

    private AutocompleteAddressActivityBinding binding;

    View.OnClickListener startAutocompleteIntentListener = view -> {
        view.setOnClickListener(null);
        startAutocompleteIntent();
    };

    private final ActivityResultLauncher<Intent> startAutocomplete = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Intent intent = result.getData();
                    if (intent != null) {
                        Place place = Autocomplete.getPlaceFromIntent(intent);

                        // Write a method to read the address components from the Place
                        // and populate the form with the address components
                        Log.d(TAG, "Place: " + place.getAddressComponents());
                        fillInAddress(place);
                    }
                } else if (result.getResultCode() == Activity.RESULT_CANCELED) {
                    // The user canceled the operation.
                    Log.i(TAG, "User canceled autocomplete");
                }
            });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        binding.autocompleteAddress1.setOnClickListener(startAutocompleteIntentListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Enable edge-to-edge display. This must be called before calling super.onCreate().
        EdgeToEdge.enable(this);
        super.onCreate(savedInstanceState);

        binding = AutocompleteAddressActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // Retrieve a PlacesClient (previously initialized - see MainActivity)
        placesClient = Places.createClient(this);

        // Attach an Autocomplete intent to the Address 1 EditText field
        binding.autocompleteAddress1.setOnClickListener(startAutocompleteIntentListener);

        // Update checkProximity when user checks the checkbox
        CheckBox checkProximityBox = findViewById(R.id.checkbox_proximity);
        checkProximityBox.setOnCheckedChangeListener((view, isChecked) -> {
            // Set the boolean to match user preference for when the Submit button is clicked
            checkProximity = isChecked;
        });

        // Submit and optionally check proximity
        Button saveButton = findViewById(R.id.autocomplete_save_button);
        saveButton.setOnClickListener(v -> saveForm());

        // Reset the form
        Button resetButton = findViewById(R.id.autocomplete_reset_button);
        resetButton.setOnClickListener(v -> clearForm());
    }

    private void startAutocompleteIntent() {

        // Set the fields to specify which types of place data to
        // return after the user has made a selection.
        List<Place.Field> fields = Arrays.asList(Place.Field.ADDRESS_COMPONENTS,
                Place.Field.LOCATION, Place.Field.VIEWPORT);

        // Build the autocomplete intent with field, country, and type filters applied
        Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fields)
                .setCountries(List.of("US"))
                .setTypesFilter(List.of("establishment"))
                .build(this);
        startAutocomplete.launch(intent);
    }

    @Override
    public void onMapReady(@NonNull GoogleMap googleMap) {
        map = googleMap;
        try {
            // Customise the styling of the base map using a JSON object defined
            // in a string resource.
            boolean success = map.setMapStyle(
                    MapStyleOptions.loadRawResourceStyle(this, R.raw.style_json));

            if (!success) {
                Log.e(TAG, "Style parsing failed.");
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Can't find style. Error: ", e);
        }
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15f));
        marker = map.addMarker(new MarkerOptions().position(coordinates));
    }

    private void fillInAddress(Place place) {
        AddressComponents components = place.getAddressComponents();
        StringBuilder address1 = new StringBuilder();
        StringBuilder postcode = new StringBuilder();

        // Get each component of the address from the place details,
        // and then fill-in the corresponding field on the form.
        // Possible AddressComponent types are documented at https://goo.gle/32SJPM1
        if (components != null) {
            for (AddressComponent component : components.asList()) {
                String type = component.getTypes().get(0);
                switch (type) {
                    case "street_number": {
                        address1.insert(0, component.getName());
                        break;
                    }

                    case "route": {
                        address1.append(" ");
                        address1.append(component.getShortName());
                        break;
                    }

                    case "postal_code": {
                        postcode.insert(0, component.getName());
                        break;
                    }

                    case "postal_code_suffix": {
                        postcode.append("-").append(component.getName());
                        break;
                    }

                    case "locality":
                        binding.autocompleteCity.setText(component.getName());
                        break;

                    case "administrative_area_level_1": {
                        binding.autocompleteState.setText(component.getShortName());
                        break;
                    }

                    case "country":
                        binding.autocompleteCountry.setText(component.getName());
                        break;
                }
            }
        }

        binding.autocompleteAddress1.setText(address1.toString());
        binding.autocompletePostal.setText(postcode.toString());

        // After filling the form with address components from the Autocomplete
        // prediction, set cursor focus on the second address line to encourage
        // entry of sub-premise information such as apartment, unit, or floor number.
        binding.autocompleteAddress2.requestFocus();

        // Add a map for visual confirmation of the address
        showMap(place);
    }

    private void showMap(Place place) {
        coordinates = place.getLocation();

        // It isn't possible to set a fragment's id programmatically so we set a tag instead and
        // search for it using that.
        mapFragment = (SupportMapFragment)
                getSupportFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);

        // We only create a fragment if it doesn't already exist.
        if (mapFragment == null) {
            mapPanel = ((ViewStub) findViewById(R.id.stub_map)).inflate();
            GoogleMapOptions mapOptions = new GoogleMapOptions();
            mapOptions.mapToolbarEnabled(false);

            // To programmatically add the map, we first create a SupportMapFragment.
            mapFragment = SupportMapFragment.newInstance(mapOptions);

            // Then we add it using a FragmentTransaction.
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.confirmation_map, mapFragment, MAP_FRAGMENT_TAG)
                    .commit();
            mapFragment.getMapAsync(this);
        } else {
            updateMap(coordinates);
        }
    }

    private void updateMap(LatLng latLng) {
        marker.setPosition(latLng);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f));
        if (mapPanel.getVisibility() == View.GONE) {
            mapPanel.setVisibility(View.VISIBLE);
        }
    }

    private void saveForm() {
        Log.d(TAG, "checkProximity = " + checkProximity);
        if (checkProximity) {
            checkLocationPermissions();
        } else {
            Toast.makeText(
                            this,
                            R.string.autocomplete_skipped_message,
                            Toast.LENGTH_SHORT)
                    .show();
        }
    }

    private void clearForm() {
        binding.autocompleteAddress1.setText("");
        binding.autocompleteAddress2.getText().clear();
        binding.autocompleteCity.getText().clear();
        binding.autocompleteState.getText().clear();
        binding.autocompletePostal.getText().clear();
        binding.autocompleteCountry.getText().clear();
        if (mapPanel != null) {
            mapPanel.setVisibility(View.GONE);
        }
        binding.autocompleteAddress1.requestFocus();
    }

    // Register the permissions callback, which handles the user's response to the
    // system permissions dialog. Save the return value, an instance of
    // ActivityResultLauncher, as an instance variable.
    private final ActivityResultLauncher<String> requestPermissionLauncher =
            registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
                if (isGranted) {
                    // Since ACCESS_FINE_LOCATION is the only permission in this sample,
                    // run the location comparison task once permission is granted.
                    // Otherwise, check which permission is granted.
                    getAndCompareLocations();
                } else {
                    // Fallback behavior if user denies permission
                    Log.d(TAG, "User denied permission");
                }
            });

    private void checkLocationPermissions() {
        if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            getAndCompareLocations();
        } else {
            requestPermissionLauncher.launch(
                    ACCESS_FINE_LOCATION);
        }
    }

    @SuppressLint("MissingPermission")
    private void getAndCompareLocations() {
        // TODO: Detect and handle if user has entered or modified the address manually and update
        // the coordinates variable to the Lat/Lng of the manually entered address. May use
        // Geocoding API to convert the manually entered address to a Lat/Lng.
        LatLng enteredLocation = coordinates;
        map.setMyLocationEnabled(true);

        FusedLocationProviderClient fusedLocationClient =
                LocationServices.getFusedLocationProviderClient(this);

        fusedLocationClient.getLastLocation()
                .addOnSuccessListener(this, location -> {
                    // Got last known location. In some rare situations this can be null.
                    if (location == null) {
                        return;
                    }

                    deviceLocation = new LatLng(location.getLatitude(), location.getLongitude());
                    Log.d(TAG, "device location = " + deviceLocation);
                    Log.d(TAG, "entered location = " + enteredLocation.toString());

                    // Use the computeDistanceBetween function in the Maps SDK for Android Utility Library
                    // to use spherical geometry to compute the distance between two Lat/Lng points.
                    double distanceInMeters = computeDistanceBetween(deviceLocation, enteredLocation);
                    if (distanceInMeters <= acceptedProximity) {
                        Log.d(TAG, "location matched");
                        // TODO: Display UI based on the locations matching
                    } else {
                        Log.d(TAG, "location not matched");
                        // TODO: Display UI based on the locations not matching
                    }
                });
    }
}
    

এপিআই সক্ষম করা

এই সুপারিশগুলো বাস্তবায়ন করতে, আপনাকে গুগল ক্লাউড কনসোলে নিম্নলিখিত এপিআইগুলো সক্রিয় করতে হবে:

সেটআপ সম্পর্কে আরও তথ্যের জন্য, আপনার গুগল ক্লাউড প্রজেক্ট সেট আপ করুন দেখুন।

ইনপুট ফিল্ডে অটোকমপ্লিট যোগ করা

এই অংশে একটি ঠিকানা ফর্মে কীভাবে স্থান স্বয়ংসম্পূর্ণতা (Place Autocomplete) যোগ করতে হয় তা বর্ণনা করা হয়েছে।

স্থান স্বয়ংসম্পূর্ণ উইজেট যোগ করা

অ্যান্ড্রয়েডে, আপনি একটি অটোকমপ্লিট ইন্টেন্ট ব্যবহার করে অটোকমপ্লিট উইজেটটি যোগ করতে পারেন। এই ইন্টেন্টটি 'অ্যাড্রেস লাইন ১' ইনপুট ফিল্ড থেকে 'প্লেস অটোকমপ্লিট' চালু করে, যেখানে ব্যবহারকারী তার ঠিকানা লেখা শুরু করবেন। টাইপ করা শুরু করলেই, তিনি অটোকমপ্লিট প্রেডিকশনের তালিকা থেকে নিজের ঠিকানা বেছে নিতে পারবেন।

প্রথমে, ActivityResultLauncher ব্যবহার করে একটি অ্যাক্টিভিটি লঞ্চার প্রস্তুত করুন, যা চালু করা অ্যাক্টিভিটি থেকে ফলাফলের জন্য অপেক্ষা করবে। রেজাল্ট কলব্যাকে একটি Place অবজেক্ট থাকবে, যা ব্যবহারকারীর Autocomplete প্রেডিকশন থেকে নির্বাচিত ঠিকানার সাথে সঙ্গতিপূর্ণ হবে।

    private final ActivityResultLauncher<Intent> startAutocomplete = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Intent intent = result.getData();
                    if (intent != null) {
                        Place place = Autocomplete.getPlaceFromIntent(intent);

                        // Write a method to read the address components from the Place
                        // and populate the form with the address components
                        Log.d(TAG, "Place: " + place.getAddressComponents());
                        fillInAddress(place);
                    }
                } else if (result.getResultCode() == Activity.RESULT_CANCELED) {
                    // The user canceled the operation.
                    Log.i(TAG, "User canceled autocomplete");
                }
            });

এরপরে, Place Autocomplete ইন্টেন্টের fields, location, এবং type প্রোপার্টিগুলো সংজ্ঞায়িত করুন এবং Autocomplete.IntentBuilder ব্যবহার করে এটি তৈরি করুন। সবশেষে, পূর্ববর্তী কোড স্যাম্পলে সংজ্ঞায়িত ActivityResultLauncher ব্যবহার করে ইন্টেন্টটি চালু করুন।

    private void startAutocompleteIntent() {

        // Set the fields to specify which types of place data to
        // return after the user has made a selection.
        List<Place.Field> fields = Arrays.asList(Place.Field.ADDRESS_COMPONENTS,
                Place.Field.LOCATION, Place.Field.VIEWPORT);

        // Build the autocomplete intent with field, country, and type filters applied
        Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fields)
                .setCountries(List.of("US"))
                .setTypesFilter(List.of("establishment"))
                .build(this);
        startAutocomplete.launch(intent);
    }

প্লেস অটোকমপ্লিট দ্বারা প্রাপ্ত ঠিকানা পরিচালনা করা

পূর্বে ActivityResultLauncher সংজ্ঞায়িত করার মাধ্যমে এটাও নির্ধারণ করা হয়েছিল যে, কলব্যাকে অ্যাক্টিভিটির ফলাফল ফেরত এলে কী করা হবে। যদি ব্যবহারকারী কোনো প্রেডিকশন নির্বাচন করে থাকেন, তবে সেটি রেজাল্ট অবজেক্টের মধ্যে থাকা ইন্টেন্টে সরবরাহ করা হবে। যেহেতু ইন্টেন্টটি Autocomplete.IntentBuilder দ্বারা তৈরি করা হয়েছিল, তাই Autocomplete.getPlaceFromIntent() মেথডটি এর থেকে Place অবজেক্টটি বের করে নিতে পারে।

    private final ActivityResultLauncher<Intent> startAutocomplete = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Intent intent = result.getData();
                    if (intent != null) {
                        Place place = Autocomplete.getPlaceFromIntent(intent);

                        // Write a method to read the address components from the Place
                        // and populate the form with the address components
                        Log.d(TAG, "Place: " + place.getAddressComponents());
                        fillInAddress(place);
                    }
                } else if (result.getResultCode() == Activity.RESULT_CANCELED) {
                    // The user canceled the operation.
                    Log.i(TAG, "User canceled autocomplete");
                }
            });

সেখান থেকে, Place.getAddressComponents() কল করুন এবং অ্যাড্রেস ফর্মের প্রতিটি অ্যাড্রেস কম্পোনেন্টকে তার সংশ্লিষ্ট ইনপুট ফিল্ডের সাথে মেলান, এবং ব্যবহারকারীর নির্বাচিত Place থেকে পাওয়া ভ্যালু দিয়ে ফিল্ডটি পূরণ করুন।

এই পৃষ্ঠার 'Get the code' বিভাগের অধীনে প্রদত্ত নমুনা কোডের fillInAddress মেথডে ঠিকানা ফর্মের ফিল্ডগুলি পূরণ করার একটি উদাহরণমূলক বাস্তবায়ন শেয়ার করা হয়েছে।

হাতে লিখে ঠিকানা দেওয়ার পরিবর্তে পূর্বাভাস থেকে ঠিকানার তথ্য সংগ্রহ করলে তা ঠিকানার নির্ভুলতা নিশ্চিত করতে সাহায্য করে, ঠিকানাটি পরিচিত এবং সেখানে ডেলিভারি করা সম্ভব তা নিশ্চিত করে এবং ব্যবহারকারীর কীস্ট্রোক কমায়।

প্লেস অটোকমপ্লিট বাস্তবায়নের সময় বিবেচ্য বিষয়সমূহ

প্লেস অটোকমপ্লিট-এ বেশ কিছু অপশন রয়েছে যা এর বাস্তবায়নকে নমনীয় করে তোলে, যদি আপনি শুধু উইজেটের বাইরেও আরও কিছু ব্যবহার করতে চান। কোনো একটি অবস্থানকে সঠিকভাবে মেলানোর জন্য আপনার যা প্রয়োজন, তা পেতে আপনি বিভিন্ন সার্ভিসের সমন্বয় ব্যবহার করতে পারেন।

  • একটি ADDRESS ফর্মের ক্ষেত্রে, শুধুমাত্র সম্পূর্ণ রাস্তার ঠিকানার সাথে মিল সীমাবদ্ধ করতে types প্যারামিটারটি address এ সেট করুন। Place Autocomplete অনুরোধগুলিতে সমর্থিত প্রকারগুলি সম্পর্কে আরও জানুন।

  • যদি বিশ্বব্যাপী অনুসন্ধানের প্রয়োজন না হয়, তবে উপযুক্ত বিধিনিষেধ এবং পক্ষপাতিত্ব নির্ধারণ করুন। এমন অনেক প্যারামিটার রয়েছে যা ব্যবহার করে যেকোনো মিলকে শুধুমাত্র নির্দিষ্ট অঞ্চলে সীমাবদ্ধ বা পক্ষপাতদুষ্ট করা যায়।

    • কোনো একটি এলাকাকে সীমাবদ্ধ করার জন্য আয়তক্ষেত্রাকার সীমানা নির্ধারণ করতে RectangularBounds ব্যবহার করুন, এবং শুধুমাত্র সেই এলাকার ঠিকানাগুলোই যাতে ফেরত আসে তা নিশ্চিত করতে setLocationRestriction() ব্যবহার করুন।

    • নির্দিষ্ট কিছু দেশের মধ্যে প্রতিক্রিয়া সীমাবদ্ধ করতে setCountries() ব্যবহার করুন।

  • যদি মিল খুঁজে পাওয়ার সময় নির্দিষ্ট কোনো ফিল্ড বাদ পড়ে যায়, সেই ক্ষেত্রে ফিল্ডগুলো সম্পাদনাযোগ্য রাখুন এবং প্রয়োজনে গ্রাহকদের ঠিকানা আপডেট করার সুযোগ দিন। যেহেতু প্লেস অটোকমপ্লিট দ্বারা প্রাপ্ত বেশিরভাগ ঠিকানায় অ্যাপার্টমেন্ট, স্যুট বা ইউনিট নম্বরের মতো সাব-প্রিমিজ নম্বর থাকে না, তাই প্রয়োজনে ব্যবহারকারীকে সেই তথ্য পূরণ করতে উৎসাহিত করার জন্য আপনি অ্যাড্রেস লাইন ২-এ ফোকাস সরিয়ে নিতে পারেন।

ঠিকানার চাক্ষুষ নিশ্চিতকরণ প্রদান

ঠিকানা লেখার অংশ হিসেবে, ব্যবহারকারীদের একটি মানচিত্রে ঠিকানাটির চাক্ষুষ নিশ্চিতকরণ প্রদান করুন। এটি ব্যবহারকারীদের ঠিকানাটি সঠিক হওয়ার বিষয়ে অতিরিক্ত আশ্বাস দেয়।

নিচের চিত্রে ঠিকানার নিচে একটি মানচিত্র দেখানো হয়েছে, যেখানে প্রবেশ করানো ঠিকানায় একটি পিন রয়েছে।

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

SupportMapFragment যোগ করা

প্রথমে, লেআউট XML ফাইলে একটি SupportMapFragment ফ্র্যাগমেন্ট যোগ করুন।

    <fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/confirmation_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

তারপর, ফ্র্যাগমেন্টটি আগে থেকে না থাকলে প্রোগ্রাম্যাটিকভাবে সেটি যোগ করুন।

    private void showMap(Place place) {
        coordinates = place.getLocation();

        // It isn't possible to set a fragment's id programmatically so we set a tag instead and
        // search for it using that.
        mapFragment = (SupportMapFragment)
                getSupportFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);

        // We only create a fragment if it doesn't already exist.
        if (mapFragment == null) {
            mapPanel = ((ViewStub) findViewById(R.id.stub_map)).inflate();
            GoogleMapOptions mapOptions = new GoogleMapOptions();
            mapOptions.mapToolbarEnabled(false);

            // To programmatically add the map, we first create a SupportMapFragment.
            mapFragment = SupportMapFragment.newInstance(mapOptions);

            // Then we add it using a FragmentTransaction.
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.confirmation_map, mapFragment, MAP_FRAGMENT_TAG)
                    .commit();
            mapFragment.getMapAsync(this);
        } else {
            updateMap(coordinates);
        }
    }

ফ্র্যাগমেন্টের হ্যান্ডেল পাওয়া এবং কলব্যাক নিবন্ধন করা

  1. ফ্র্যাগমেন্টের হ্যান্ডেল পেতে, FragmentManager.findFragmentById মেথডটি কল করুন এবং আপনার লেআউট ফাইলে থাকা ফ্র্যাগমেন্টের রিসোর্স আইডিটি পাস করুন। যদি আপনি ফ্র্যাগমেন্টটি ডাইনামিকভাবে যোগ করে থাকেন , তবে এই ধাপটি এড়িয়ে যান, কারণ আপনি ইতিমধ্যেই হ্যান্ডেলটি সংগ্রহ করেছেন।

  2. ফ্র্যাগমেন্টে কলব্যাক সেট করতে getMapAsync মেথডটি কল করুন।

উদাহরণস্বরূপ, যদি আপনি ফ্র্যাগমেন্টটি স্ট্যাটিক্যালি যোগ করে থাকেন:

কোটলিন

val mapFragment = supportFragmentManager
    .findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)

      

জাভা

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

      

মানচিত্রে মার্কারের স্টাইলিং এবং সংযোজন।

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

    @Override
    public void onMapReady(@NonNull GoogleMap googleMap) {
        map = googleMap;
        try {
            // Customise the styling of the base map using a JSON object defined
            // in a string resource.
            boolean success = map.setMapStyle(
                    MapStyleOptions.loadRawResourceStyle(this, R.raw.style_json));

            if (!success) {
                Log.e(TAG, "Style parsing failed.");
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Can't find style. Error: ", e);
        }
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15f));
        marker = map.addMarker(new MarkerOptions().position(coordinates));
    }

( সম্পূর্ণ কোড নমুনা দেখুন )

মানচিত্র নিয়ন্ত্রণ নিষ্ক্রিয় করা

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