2

I am trying to show the exact address name using GPS in android. So I have searched and I got this example and i am following it.

The above example is showing only the exact longitude and latitude address in numbers but I need to show the exact addresses in the form of texts of those particular longitude and latitude.

How to achieve this?.I have searched for this as per my knowledge but still not getting any good solution.Can anyone please tell me the solution if you have.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
Manick
  • 817
  • 2
  • 15
  • 24

2 Answers2

1

Pass lat and long to following method it will return you the address

 public String getAddress(double latitude, double longitude) {
 String strCompAdd;
    try {

        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(passHomeCall.this);
        if (latitude != 0 || longitude != 0) {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(1);
            String country = addresses.get(0).getAddressLine(2);
            Log.d("TAG", "address = " + address + ", city =" + city
                    + ", country = " + country);
            strCompAdd = address + ", " + city + ", " + country;
            strCompAdd = strCompAdd.replaceAll("null", "");
            strCompAdd = strCompAdd.replaceAll("Unnamed", "");

        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        // Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
      return strCompAdd;
}
ridoy
  • 6,274
  • 2
  • 29
  • 60
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
0

I use the following code to find address from lat and lng. Its the android's own location api.

import android.location.Geocoder;

public static String getPickupXY(double x, double y) throws IOException {
    Geocoder geocoder = new Geocoder(applicationContext, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocation(x, y, 1);
    if (addresses == null || addresses.size() <= 0)
        return null;
    return (addresses.get(0).getAddressLine(0).toString());
}
Siddharth
  • 9,349
  • 16
  • 86
  • 148