5

I am trying to get postal code, but I am unable to get zipcode(postalcode). I can able to get current city but when I try to get the zipcode it's giving me a null pointer exception. Can anyone help me.

final Geocoder gcd = new Geocoder(getApplicationContext(),
                Locale.getDefault());

List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)                                   Log.d(addresses.get(0).getLocality()); // I can get city name here.
Log.d(addresses.get(0).getPostalCode();// here i am getting nullpoiter exception
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
Sri
  • 165
  • 2
  • 3
  • 12
  • possible duplicate of [How to get zip code or area code of the current location in android?](http://stackoverflow.com/questions/8442050/how-to-get-zip-code-or-area-code-of-the-current-location-in-android) – g00dy Sep 02 '13 at 08:14
  • Read the second comment on the accepted answer in the link. – g00dy Sep 02 '13 at 09:08
  • Other AddressLine means? – Sri Sep 02 '13 at 09:21
  • This means - http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation(double, double, int). Then `addresses.get(1)).getPostalCode()` or try with 2,3,4. After you read this - take a look here - http://stackoverflow.com/questions/16515682/get-the-particular-address-using-latitude-and-longitude/16515848#16515848 – g00dy Sep 02 '13 at 09:28
  • When I used like this i got the following exception 09-02 java.lang.IndexOutOfBoundsException: Invalid index 3, size is 1 – Sri Sep 02 '13 at 09:35
  • Yes, it's becasue you're calling `List
    addresses = gcd.getFromLocation(latitude, longitude, 1);`. Change the last digit to 3,4 or 5.
    – g00dy Sep 02 '13 at 10:19
  • What is the maximum value for that last digit.What it means? can you give some clarification? – Sri Sep 03 '13 at 05:15
  • The information is in the second link, but it failed to paste I think - try all, which was marked with "`````" -> `http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation(double, double, int)` – g00dy Sep 03 '13 at 06:46
  • But I can get the city name.Y should i didn't get postal code? What is the reason? – Sri Sep 03 '13 at 09:09
  • Strange ... You can do it with another approach, by using this -> https://developers.google.com/maps/documentation/geocoding/ . However, paste the manifest here please. – g00dy Sep 03 '13 at 09:20

4 Answers4

8

Try to use android built-in Geocoder to get details from latitude and longitude without calling google location api as below :

Initialize Gecoder using Context :

final Geocoder gcd = new Geocoder(context);

Get Address as result from Lat-Long, Here (10) max result.

List<Address> addresses = gcd.getFromLocation(latitude, longitude, 10);

Iterate to result get required location details :

for (Address address : addresses) {
    if(address.getLocality()!=null && address.getPostalCode()!=null){
        Log.d(address.getLocality());
        Log.d(address.getPostalCode();
       break;
    }
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • But I should get postalcode? I know the above solution but I need to get postal code.. – Sri Sep 02 '13 at 08:56
7
    Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
    List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
    Address address=null;
    String addr="";
    String zipcode="";
    String city="";
    String state="";
    if (addresses != null && addresses.size() > 0){

            addr=addresses.get(0).getAddressLine(0)+"," +addresses.get(0).getSubAdminArea();
                 city=addresses.get(0).getLocality();
                 state=addresses.get(0).getAdminArea();

                 for(int i=0 ;i<addresses.size();i++){
                     address = addresses.get(i);
                     if(address.getPostalCode()!=null){
                         zipcode=address.getPostalCode();
                         break;
                     }

                }
thrylos
  • 1,513
  • 1
  • 10
  • 5
6

I Used google webservice to get the zipcode.

The following is the google web service

http://maps.googleapis.com/maps/api/geocode/json?latlng=lattitude,longitude&sensor=true

here lattitude and longitude. so replace those values and you will get response and parse them and get postal code.

Sri
  • 165
  • 2
  • 3
  • 12
1

I have utility method to grab postcode which is pretty neat and works fine..

public static String getPostalCodeByCoordinates(Context context, double lat, double lon) throws IOException {

    Geocoder mGeocoder = new Geocoder(context, Locale.getDefault());
    String zipcode=null;
    Address address=null;

    if (mGeocoder != null) {

        List<Address> addresses = mGeocoder.getFromLocation(lat, lon, 5);

        if (addresses != null && addresses.size() > 0) {

            for (int i = 0; i < addresses.size(); i++) {
                address = addresses.get(i);
                if (address.getPostalCode() != null) {
                    zipcode = address.getPostalCode();
                    Log.d(TAG, "Postal code: " + address.getPostalCode());
                    break;
                }

            }
            return zipcode;
        }
    }

    return null;
}
Ali Nawaz
  • 2,016
  • 20
  • 30