0

I am new in Android. I develop one application which have to display Current Location name. I tried to use Geocoder class. But it seems in some phone display properly Location but in some it is displaying any location and getting null.

My code is here,

Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
    List<Address> listAddresses = null;
    try {
        listAddresses = geocoder.getFromLocation(gps.getLatitude(), gps.getLongitude(), 1);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (null != listAddresses && listAddresses.size() > 0) {
        Location = "";
        for (int i = 0; i < 4; i++) {
            if (listAddresses.get(0).getAddressLine(i) != null) {
                Location += listAddresses.get(0).getAddressLine(i) + ",";
            }
        }
        Log.e("Location", Location);
    }

My Manifest class,

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

I can't understand what is the problem ? Is it depend on different devices ? Or any other reason ?

Punit Sharma
  • 2,951
  • 1
  • 20
  • 35

2 Answers2

0
    **Just pass your current lat-long in this method and you will get address:**    

private void getAddress(double locationLatitude, double locationLongitude) {
                Geocoder gCoder = new Geocoder(getActivity());
                try {
                    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.getAddressLine(0) != null) {
                            sb.append(address.getAddressLine(0)).append("\n");
                        }
                        sb.append(address.getLocality()).append(",");
                        // sb.append(address.getPostalCode()).append(",");
                        sb.append(address.getCountryName());
                        strAddress = sb.toString();
                        strAddress = strAddress.replace(",null", "");
                        strAddress = strAddress.replace("null", "");
                        strAddress = strAddress.replace("Unnamed", "");
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (!TextUtils.isEmpty(strAddress)) {
                                Logg.e("address++++", "" + strAddress);
                                edtZipcode.setFocusable(false);
                                edtZipcode.setFocusableInTouchMode(false);
                                edtZipcode.setText(strAddress); // set your current address in this edittext using your current lat-long
                                edtZipcode.setFocusable(true);
                                edtZipcode.setFocusableInTouchMode(true);
                            } else {
                                Logg.e("address", "");
                                edtZipcode.setText("");
                                Toast.show(getActivity(), getResources().getString(R.string.toast_enter_zipcode));
                                new Animation(Techniques.Shake, 1000, edtZipcode);
                            }
                        }
                    });

                } catch (IOException exc) {
                    exc.printStackTrace();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
Bhavnik
  • 2,020
  • 14
  • 21
0

Create a intent service first for getting a location address.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms.location.sample.locationaddress" >
<application
    ...
    <service
        android:name=".FetchAddressIntentService"
        android:exported="false"/>
</application>
...

Then extends your class with IntentService to use onHandle

 @Override
protected void onHandleIntent(Intent intent) {
String errorMessage = "";

// Get the location passed to this service through an extra.
Location location = intent.getParcelableExtra(
        Constants.LOCATION_DATA_EXTRA);

...

List<Address> addresses = null;

try {
    addresses = geocoder.getFromLocation(
            location.getLatitude(),
            location.getLongitude(),
            // In this sample, get just a single address.
            1);
} catch (IOException ioException) {
    // Catch network or other I/O problems.
    errorMessage = getString(R.string.service_not_available);
    Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
    // Catch invalid latitude or longitude values.
    errorMessage = getString(R.string.invalid_lat_long_used);
    Log.e(TAG, errorMessage + ". " +
            "Latitude = " + location.getLatitude() +
            ", Longitude = " +
            location.getLongitude(), illegalArgumentException);
}

// Handle case where no address was found.
if (addresses == null || addresses.size()  == 0) {
    if (errorMessage.isEmpty()) {
        errorMessage = getString(R.string.no_address_found);
        Log.e(TAG, errorMessage);
    }
    deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
} else {
    Address address = addresses.get(0);
    ArrayList<String> addressFragments = new ArrayList<String>();

    // Fetch the address lines using getAddressLine,
    // join them, and send them to the thread.
    for(int i = 0; i < address.getMaxAddressLineIndex(); i++) {
        addressFragments.add(address.getAddressLine(i));
    }
    Log.i(TAG, getString(R.string.address_found));
    deliverResultToReceiver(Constants.SUCCESS_RESULT,
            TextUtils.join(System.getProperty("line.separator"),
                    addressFragments));
}
  }

For more clarification you can use following link too

Developer Android

Github Sample Project

Punit Sharma
  • 2,951
  • 1
  • 20
  • 35