I already tried several possible "solutions" here on SO, but nothing worked for me. Basically, I am trying to get the zip code from lat/lng. I read that there are some issues with Geocoder
and the emulator Genymotion, but I am not sure, if that really is the problem. Furthermore, I also tried to get the zip code via HTTP Request as a JSON object, but this also didn't work.
Here is my code:
@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;
}
The problem is, that addr.size()
is 0 and I don't know why.
Any help would be greatly appreciated.