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.
How to get zip code or area code of the current location in android?
did you have a look at this: https://mcmap.net/q/1631760/-zipcode-from-location –
Tutt
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.
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 Google –
Virilism
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
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
Read this carefully.
The getFromLocation method is what you need.
© 2022 - 2024 — McMap. All rights reserved.