本文介绍了如何在Android中使用用户当前位置获取邮政编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取邮政编码,但无法获取邮政编码(邮政编码).我可以获取当前城市,但是当我尝试获取邮政编码时,它给了我一个空指针异常.谁能帮我.
I am trying to get postal code, but I am unable to get zipcode(postalcode). I can able to get current city but when I try to get the zipcode it's giving me a null pointer exception.Can anyone help me.
final Geocoder gcd = new Geocoder(getApplicationContext(),
Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) Log.d(addresses.get(0).getLocality()); // I can get city name here.
Log.d(addresses.get(0).getPostalCode();// here i am getting nullpoiter exception
推荐答案
我使用google网络服务获取邮政编码.
I Used google webservice to get the zipcode.
以下是Google网络服务
The following is the google web service
http://maps.googleapis.com/maps/api/geocode/json?latlng = lattitude,longitude& sensor = true
这里是经度和纬度.因此,替换这些值,您将获得响应并解析它们,并获取邮政编码.
here lattitude and longitude. so replace those values and you will get response and parse them and get postal code.
这篇关于如何在Android中使用用户当前位置获取邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!