1

I am trying to get location name or address. I have successfully fetched the latitude and longitude by Google Fused Location API.

Now I want to fetch the location address (example : City name,Road no, or specific address) by using the latitude and longitude.

For this purpose I am using Google Geocoder and it works fine. But in some devices the location address returns the null value.

I searched in the net for the solution of this problem and found that some device manufacturers does not include this feature in their devices. That's why those device can't find the the address by Reverse Geocoding method. Here is the link of that information

So is there any alternative way to find the address name as string without Geoconding ?

Here is my code for fetching address by Geocoding

public static void getAddress(Context context, double LATITUDE, double LONGITUDE) {


    try {
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
        if (addresses != null && addresses.size() > 0) {



            String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
            String city = addresses.get(0).getLocality();
            String state = addresses.get(0).getAdminArea();
            String country = addresses.get(0).getCountryName();
            String postalCode = addresses.get(0).getPostalCode();
            String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL

            Log.d(TAG, "getAddress:  address" + address);
            Log.d(TAG, "getAddress:  city" + city);
            Log.d(TAG, "getAddress:  state" + state);
            Log.d(TAG, "getAddress:  postalCode" + postalCode);
            Log.d(TAG, "getAddress:  knownName" + knownName);

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return;
}
XpressGeek
  • 2,889
  • 3
  • 23
  • 27

1 Answers1

0

Use this its working for me

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

    private static String area = null;

    public 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<Address> 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");
                        }

                        area = address.getSubLocality();

                        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);
                        bundle.putString("area",area);
                        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);
                        bundle.putString("area",area);
                        message.setData(bundle);
                    }
                    message.sendToTarget();
                }
            }
        };
        thread.start();
    }
}

To call this in your Activity use

 LocationAddress locationAddress = new LocationAddress();
                locationAddress.getAddressFromLocation(latitude,longitude,
                        getApplicationContext(), new GeocoderHandler());

GeocoderHandler

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

            if(locationAddress != null)
            locationAddress=locationAddress.replaceAll("[\r\n]+", " ");

            Log.d("===========>>>",area);
        Log.d("===========>>>>",locationAddress);
        }
    }
Anil
  • 1,605
  • 1
  • 14
  • 24