কম্পিউটার টিউটোরিয়াল

How to complete address from latitude and longitude on Android?


অ্যান্ড্রয়েডে ল্যাটিটিউড এবং লองিটিউড থেকে ঠিকানা পাওয়া

এই উদাহরণটি দেখায় কিভাবে Geocoder ব্যবহার করে ল্যাটিটিউড এবং লองিটিউড থেকে পূর্ণ ঠিকানা পাওয়া যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project-এ যান এবং প্রজেক্ট তৈরির জন্য প্রয়োজনীয় বিবরণ পূরণ করুন।

ধাপ ২: লেআউট ফাইল সংশোধন করুন (res/layout/activity_main.xml)

<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16sp"
    tools:context=".MainActivity">
    <TextView
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="Show Address"
        android:id="@+id/btnShowAddress"
        android:layout_centerHorizontal="true" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvAddress"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="134dp"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

ধাপ ৩: MainActivity.java-তে কোড যোগ করুন

import android.support.v7.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button btnShowAddress;
    TextView tvAddress;
    Location location;
    AppLocationService appLocationService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvAddress = findViewById(R.id.tvAddress);
        appLocationService = new AppLocationService(MainActivity.this);
        btnShowAddress = findViewById(R.id.btnShowAddress);

        btnShowAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                location = appLocationService.getLocation(LocationManager.GPS_PROVIDER);
                double latitude = 13.1000727;
                double longitude = 80.2126274;
                LocationAddress locationAddress = new LocationAddress();
                locationAddress.getAddressFromLocation(latitude, longitude,
                        getApplicationContext(), new GeoCodeHandler());
                showSettingsAlert();
            }

            public void showSettingsAlert() {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                alertDialog.setTitle("SETTINGS");
                alertDialog.setMessage("Enable Location Provider! Go to settings menu?");
                alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        MainActivity.this.startActivity(intent);
                    }
                });
                alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                alertDialog.show();
            }

            class GeoCodeHandler extends Handler {
                @Override
                public void handleMessage(Message message) {
                    String locationAddress;
                    switch (message.what) {
                        case 1:
                            Bundle bundle = message.getData();
                            locationAddress = bundle.getString("address");
                            break;
                        default:
                            locationAddress = null;
                    }
                    tvAddress.setText(locationAddress);
                }
            }
        });
    }
}

ধাপ ৪: AppLocationService ক্লাস তৈরি করুন (ApplicationService.java)

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

@SuppressLint("Registered")
public class AppLocationService extends Service implements LocationListener {
    protected LocationManager locationManager;
    Location location;
    private static final long MIN_DISTANCE_FOR_UPDATE = 10;
    private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;

    public AppLocationService(Context context) {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
    }

    @SuppressLint("MissingPermission")
    public Location getLocation(String provider) {
        if (locationManager.isProviderEnabled(provider)) {
            locationManager.requestLocationUpdates(provider,
                    MIN_TIME_FOR_UPDATE,
                    MIN_DISTANCE_FOR_UPDATE, this);
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(provider);
                return location;
            }
        }
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {}
    @Override
    public void onProviderDisabled(String provider) {}
    @Override
    public void onProviderEnabled(String provider) {}
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
    @Override
    public IBinder onBind(Intent arg0) { return null; }
}

ধাপ ৫: LocationAddress ক্লাস তৈরি করুন (LocationAddress.java)

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

class LocationAddress {
    private static final String TAG = "LocationAddress";

    static void getAddressFromLocation(final double latitude,
                                       final double longitude, final Context context, final Handler handler) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                String result = null;
                try {
                    List
addressList = geocoder.getFromLocation(latitude, longitude, 1); if (addressList != null && addressList.size() > 0) { Address address = addressList.get(0); StringBuilder sb = new StringBuilder(); for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { sb.append(address.getAddressLine(i)).append("\n"); } sb.append(address.getLocality()).append("\n"); sb.append(address.getPostalCode()).append("\n"); sb.append(address.getCountryName()); result = sb.toString(); } } catch (IOException e) { Log.e(TAG, "Unable connect to Geocoder", e); } finally { Message message = Message.obtain(); message.setTarget(handler); if (result != null) { message.what = 1; Bundle bundle = new Bundle(); result = "Latitude: " + latitude + " Longitude: " + longitude + "\n\nAddress:\n" + result; bundle.putString("address", result); message.setData(bundle); } else { message.what = 1; Bundle bundle = new Bundle(); result = "Latitude: " + latitude + " Longitude: " + longitude + "\n Unable to get address for this lat-long."; bundle.putString("address", result); message.setData(bundle); } message.sendToTarget(); } } }; thread.start(); } }

ধাপ ৬: AndroidManifest.xml-এ অনুমতি যোগ করুন

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                
                
            </intent-filter>
        </activity>
    </application>
</manifest>

অ্যাপ্লিকেশন চালান

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

How to complete address from latitude and longitude on Android?

  1. অ্যান্ড্রয়েডে ঠিকানা থেকে ল্যাটিটিউড এবং লংগিটিউড বের করা

  2. অ্যান্ড্রয়েডে বর্তমান লোকেশনের ল্যাটিটিউড এবং লองিটিউড কীভাবে পাবেন?

  3. অ্যান্ড্রয়েডে URL থেকে ইমেজ ডাউনলোড ও সংরক্ষণ করা

  4. অ্যান্ড্রয়েডে এক্টিভিটির থেকে ফ্র্যাগমেন্টে ভেরিয়েবল পাস করার পদ্ধতি