0

I followed this to get Location Coordinates and Address

So Here Its getting Updates Location Coordinates Latitude and Longitude for Every 10 seconds

I am trying to Get Location Address along with them

Here To Get Address I am Using Const class

public class Const {

public static String getCompleteAddressString(Context m_context, double LATITUDE, double LONGITUDE) {
    String strAdd = "";
    Geocoder geocoder = new Geocoder(m_context, 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.v("My Current location add", "" + strAdd.toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(m_context, "Sorry, Your location cannot be retrieved !" + e.getMessage(), Toast.LENGTH_SHORT).show();

    }
    return strAdd;
}
}

And Now At Main Activity To get Address I am using

private void updateUI() {
mLatitudeTextView.setText(String.format("%s: %f", mLatitudeLabel,
        mCurrentLocation.getLatitude()));
mLongitudeTextView.setText(String.format("%s: %f", mLongitudeLabel,
        mCurrentLocation.getLongitude()));
mLastUpdateTimeTextView.setText(String.format("%s: %s", mLastUpdateTimeLabel,
        mLastUpdateTime));

//For Address

mLocAddTextView.setText(String.format("%s: %s", mLocAddressLabel,
Const.getCompleteAddressString(this, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude())));
 }

But Here its displays nothing can any one suggest me whats wrong in this

Whats Going On
  • 1,379
  • 6
  • 20
  • 49
  • Did you try using thread.sleep? cause fetching location and then the address could be a bit time consuming. Or can you fetch location values beforehand and then call address method – 7geeky Feb 27 '18 at 09:03
  • I tried same with 30/60 seconds but same.. – Whats Going On Feb 27 '18 at 09:53

1 Answers1

1

Your Code is perfect

Try This in your Const class

strReturnedAddress .append(returnedAddress.getAddressLine(0)).append("\n");

If you want to use postal code,locality and country.. etc... use

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");
    }
        strReturnedAddress .append(returnedAddress.getAddressLine(0)).append("\n");
        strReturnedAddress .append(returnedAddress.getLocality()).append("\n");
        strReturnedAddress .append(returnedAddress.getPostalCode()).append("\n");
        strReturnedAddress .append(returnedAddress.getCountryName());
    strAdd  = strReturnedAddress.toString();
    Log.v("My Current location add", "" + streturnedAddressd.toString());
}

To Get Geocoder address follow Android GeoCode Location

Don't Be negative
  • 1,215
  • 3
  • 19
  • 46