6

I would like to get the zip code of the current location in android device for my app,any example or snippet on locating it. I have tried geocoder it gives lat & long position only.

Karthik
  • 4,943
  • 19
  • 53
  • 86

3 Answers3

17

You are clearly not using it right then...

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
// lat,lng, your current location
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1); 

Now the list of Address contains the closest known areas. The Address object has the getPostalCode() function. Grab the first object and find it's Postal code.

There you go.

st0le
  • 33,375
  • 8
  • 89
  • 89
  • 1
    Hi @st0le, I am getting `5-1-304, Koti Main Rd, Badi Chowdi, Koti` as a result when I am calling `addresses.get(0)).getAddressLine(0))` . But I am getting null when I am calling `addresses.get(0)).getPostalCode())` . Please help me to get only pincode. – Sudheer Kumar Jun 25 '13 at 10:21
  • 1
    @SudheerKumar, It's probably not filled in. Try the other AddressLine(1..n). You can correct the data by making a request at Google – st0le Jun 25 '13 at 15:12
  • I got it now. Thank you :) – Sudheer Kumar Jun 26 '13 at 09:34
  • What are `lat` and `lng` in this situation? Obviously latitude and longitude, but if I am trying to get the users location, how do I know what their latitude and longitude are? – AdamMc331 Oct 17 '14 at 02:26
  • @AdamMc331, that's part of another question. – Jorge E. Hernández Oct 04 '17 at 22:25
2

Check our the Geocoder class in Android. That class has getFromLocation method which works for me. You could use like the following in your activity.

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);

Address class docs

If it doesn't for some reason you should look for a reverse geocoding service

Josnidhin
  • 12,469
  • 9
  • 42
  • 61
0

Read this carefully.

The getFromLocation method is what you need.

Parth Doshi
  • 4,200
  • 15
  • 79
  • 129