How to use Geocoder to get the current location zip code

Posted by Noble6 on Stack Overflow See other posts from Stack Overflow or by Noble6
Published on 2012-06-29T23:38:59Z Indexed on 2012/06/30 3:16 UTC
Read the original article Hit count: 80

Filed under:
|

I am trying to get the zip code of the users current location.I have a teditText in MyActivity which should get populated based on the zip code I get from this activity.

public class LocationActivity extends MyActivity {
    double LATITUDE;
    double LONGITUDE;

    Geocoder geocoder = new Geocoder(this, Locale.ENGLISH); 
    {
        try {
            List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

            if(addresses != null) {
                Address returnedZip = addresses.get(0);
                StringBuilder currentZip = new StringBuilder("Address:\n");
                for(int i=0; i<returnedZip.getMaxAddressLineIndex(); i++) {
                    strcurrentZip.append(returnedZip.getPostalCode());
                }
                m_zip.setText(strcurrentZip.toString());
            }
            else {
                m_zip.setText("No zip returned!");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            m_zip.setText("zip not found!");
        }
    }
}

I am not getting any response,the app logcat does not show any errors but the the editText field I want to populate remains blank.

© Stack Overflow or respective owner

Related posts about java

Related posts about android