0

I have the below code to convert lat, long to human readable address. Now i am some time getting full details of address including address, city, postalCode & some time getting only County name like "India". How can i get accurate address based on latitude & longitude?. Please help me.

 final List<Address> list = gCoder.getFromLocation(locationLatitude, locationLongitude, 1);
        if (list != null && list.size() > 0) {
            Address address = list.get(0);
            StringBuilder sb = new StringBuilder();

            if (address.getMaxAddressLineIndex() > 0) {
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                    sb.append(address.getAddressLine(i)).append(",");
                }
                sb.append(address.getCountryName());
            } else {
                try {
                    sb.append(address.getAddressLine(0));
                } catch (Exception ignored) {
                }
            }

            strAddress = sb.toString();
            strAddress = strAddress.replace(" ", "");
            strAddress = strAddress.replace(",null", "");
            strAddress = strAddress.replace("null", "");
            strAddress = strAddress.replace("Unnamed", "");
        }
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!TextUtils.isEmpty(strAddress)) {
                    tvLocation.setText(strAddress);
                    Log.e("Location", strAddress + "");
                }else {
                    Snackbar.make(getView(), "Couldn't get the location. Make sure location is enabled on the device", Snackbar.LENGTH_SHORT)
                            .show();
                }
            }
        });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Gopal909
  • 7
  • 4

1 Answers1

0

From a Geocoder object, you can call the getFromLocation(double, double, int) method.

    Geocoder gcd = new Geocoder(context, Locale.getDefault());
    List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
    if (addresses.size() > 0) {
        System.out.println(addresses.get(0).getLocality());
    }
    else {
       // do your stuff
    }
Ihor Tovkach
  • 124
  • 1
  • 1
  • 11