Search code examples
androidgoogle-mapslocation

how to bring it Show me the address with longti,lati


This text view should fetch data String longti = "13.082680"; String lati = "80.270721";Show me address with longti,lati TextView textView = (TextView) findViewById(R.id.address); textView.setText();Address should be displayed through these

public class MainActivity extends AppCompatActivity {
String longti = "13.082680";
String lati = "80.270721";
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    TextView textView = (TextView) findViewById(R.id.address);

    textView.setText();


    
    LocationManager locationManager;

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=                PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,   Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            1000, 1, new MyLocationListners());

    final Location location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);


    lati = String.valueOf(location.getLatitude());
    longti = String.valueOf(location.getLongitude())

  }


public class MyLocationListners implements LocationListener {

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

}
String ad = getAddress(location.getLatitude(),location.getLongitude());


@SuppressLint("LongLogTag")
private String getAddress(double longti, double lati) {
    String strAdd = "";
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(longti,
                lati, 1);
        if (addresses != null) {
            Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder("");

            for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                strReturnedAddress
                        .append(returnedAddress.getAddressLine(i)).append(
                        "\n");
            }
            strAdd = strReturnedAddress.toString();
            Log.w("My Current loction address",
                    "" + strReturnedAddress.toString());
        } else {
            Log.w("My Current loction address", "No Address returned!");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.w("My Current loction address", "Canont get Address!");
    }
    return strAdd;
}

}

String longti = "13.082680"; String lati = "80.270721";Put two and show the address


Solution

  • This is the one way you can try

    private fun getAddressInfo(latitude:Double, longitude:Double){
        val geocoder = Geocoder(this, Locale.getDefault())
        val addresses: List<Address> = geocoder.getFromLocation(latitude, longitude, 1)
    
        val address: String = addresses[0].getAddressLine(0)
        val city: String = addresses[0].locality
        val state: String = addresses[0].adminArea
        val country: String = addresses[0].countryName
        val postalCode: String = addresses[0].postalCode
        val knownName: String = addresses[0].featureName
    }
    

    or in java

    private String getAddress(double latitude, double longitude) {
        StringBuilder result = new StringBuilder();
        try {
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                result.append(address.getLocality()).append("\n");
                result.append(address.getCountryName());
            }
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }
    
        return result.toString();
    }