我想通过地址获取手机所在地址的邮政编码,但返回null。
手机已打开GPS,可以获取位置信息,但无法获取邮政编码。
/**
* Convert Location to Address
* @param location Latitude and Longitude
* @return
*/
private String getAddress(Location location) {
// result
List<Address> result = null;
String addressLine = "";// address
try {
if (location != null) {
Geocoder gc = new Geocoder(mContext, Locale.getDefault());
result = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
}
} catch (Exception e) {
e.printStackTrace();
}
if (result != null && result.get(0) != null) {
// success
Address address = result.get(0);
addressLine = address.getAddressLine(0);
String postalCode = address.getPostalCode();
LogUtil.e(Fields.println.Location, "address=======" + addressLine+"===postalCode==="+postalCode);
LogUtil.e(Fields.println.Location, "result=======" + result.toString());
}
return addressLine;
}
希望获得帮助,谢谢!
答案 0 :(得分:0)
在邮政编码说明中
/**
* Returns the postal code of the address, for example "94110",
* or null if it is unknown.
*/
public String getPostalCode() {
return mPostalCode;
}
/**
* Sets the postal code of the address to the given String, which may
* be null.
*/
public void setPostalCode(String postalCode) {
mPostalCode = postalCode;
}
因此,如果从上一个位置未能成功获取邮政编码,则邮政编码始终为空。