-1

I want to get location address from LatLng
I tried some ways but i did not get answer, because it seems this service closed by google, so i getting timeout error when i using following code, is there another solution?

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
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);
Ali Hasanzade
  • 151
  • 15

3 Answers3

0
 private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
            String strAdd = "";
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            try {
                List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 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;
        }

Try this function, it is working fine.

Hulk
  • 237
  • 1
  • 13
0

This is working fine, check code below and keep your geocoder.getFromLocation() method in try block Click Here

Community
  • 1
  • 1
Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46
0

Try this one

    public static String getAddressInString(Context context, LatLng latLng) {
    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
        if (addresses != null && addresses.size() > 0) {
            return convertToString(addresses.get(0));
        } else {
            return "";
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

public static String convertToString(Address obj) {
    String add = "";
    if (obj == null)
        return "";
    add = obj.getAddressLine(0);
    if (obj.getSubAdminArea() != null)
        add = add + "\n" + obj.getSubAdminArea();
    if (obj.getPostalCode() != null)
        add = add + " - " + obj.getPostalCode();
    if (obj.getAdminArea() != null)
        add = add + "\n" + obj.getAdminArea();
    if (obj.getCountryName() != null)
        add = add + "\n" + obj.getCountryName();
    return add;
}
Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67