Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get postal code using user current location in android

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
like image 954
Sri Avatar asked Sep 02 '13 08:09

Sri


People also ask

How do I find a zip code for a location?

The Address object has the getPostalCode() function. Grab the first object and find it's Postal code. There you go.

How can I get zip code in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken a text view to show postal code information.


3 Answers

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;
    }
}
like image 194
Haresh Chhelana Avatar answered Oct 06 '22 06:10

Haresh Chhelana


    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;
                     }

                }
like image 39
thrylos Avatar answered Oct 06 '22 06:10

thrylos


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.

like image 32
Sri Avatar answered Oct 06 '22 06:10

Sri