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.
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.
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.
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.
(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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With