How to get zip code or area code of the current location in android?
Asked Answered
R

3

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.

Rigsby answered 9/12, 2011 at 6:51 Comment(1)
did you have a look at this: https://mcmap.net/q/1631760/-zipcode-from-locationTutt
V
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.

Virilism answered 9/12, 2011 at 6:59 Comment(4)
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.Ileac
@SudheerKumar, It's probably not filled in. Try the other AddressLine(1..n). You can correct the data by making a request at GoogleVirilism
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?Ferrocene
@AdamMc331, that's part of another question.Tournament
H
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

Haggerty answered 9/12, 2011 at 6:59 Comment(0)
T
0

Read this carefully.

The getFromLocation method is what you need.

Tutt answered 9/12, 2011 at 7:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.