I want to get the zip code of the address located by my phone through address, but null is returned.
The phone has turned on the GPS, the location information can be obtained, but the zip code can not be obtained.
/**
* 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;
}
Hope to get help, thank you!