【发布时间】:2014-10-04 19:44:31
【问题描述】:
我已经在 SO 上尝试了几种可能的“解决方案”,但对我没有任何帮助。基本上,我正在尝试从 lat/lng 获取邮政编码。我读到Geocoder 和模拟器 Genymotion 存在一些问题,但我不确定这是否真的是问题所在。此外,我还尝试通过 HTTP 请求将邮政编码作为 JSON 对象获取,但这也不起作用。
这是我的代码:
@Override
public void onLocationChanged(Location location) {
EditText zipCode = (EditText) findViewById(R.id.zip_code);
zipCode.setText(getZipCodeFromLocation(location));
}
private String getZipCodeFromLocation(Location location) {
Address addr = getAddressFromLocation(location);
return addr.getPostalCode() == null ? "" : addr.getPostalCode();
}
private Address getAddressFromLocation(Location location) {
Geocoder geocoder = new Geocoder(this);
Address address = new Address(Locale.getDefault());
try {
List<Address> addr = geocoder.getFromLocation(
location.getLatitude(), location.getLongitude(), 1);
if (addr.size() > 0) {
address = addr.get(0);
}
} catch (IOException e) {
e.printStackTrace();
}
return address;
}
问题是,addr.size() 是 0,我不知道为什么。
任何帮助将不胜感激。
【问题讨论】:
标签: java android google-maps reverse-geocoding