Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 577
Karthik Avatar asked Dec 09 '11 06:12

Karthik


People also ask

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.

How can I track a zip code?

Find Zip Codes at USPS.com If you have an address or partial address, you can find the zip code through USPS Zip Code Lookup, a tool on the U.S. postal service's website. You can also search by city or state, or find all cities that are part of a particular zip code.

How do I change the ZIP code on my Android?

On your Android phone or tablet, say "Hey Google, open Assistant settings." Or, go to Assistant settings. Your places. Add, change, or delete an address.

How can I get latitude and longitude from my Android ZIP code?

(We could force the locale – that the postcode is in the SG– by using"http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=POSTCODE,SG".) Simply call this url in a jquery-ajax and you will get the lat long in result.


2 Answers

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.

like image 105
st0le Avatar answered Oct 06 '22 07:10

st0le


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

like image 34
Josnidhin Avatar answered Oct 06 '22 08:10

Josnidhin