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

天大地大妈咪最大 提交于 2019-12-01 03:48:44

问题


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.


回答1:


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.




回答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




回答3:


Read this carefully.

The getFromLocation method is what you need.



来源:https://stackoverflow.com/questions/8442050/how-to-get-zip-code-or-area-code-of-the-current-location-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!