下面的代码非常适合在 1.5、1.6 和 2.0 上运行的真实设备以及在 2.1 上运行的模拟器。
但是,在 Nexus One(运行 2.1)上执行它会引发 IOException:
java.io.IOException: Unable to parse response from server
at android.location.Geocoder.getFromLocation(Geocoder.java:124)
这就是它发生的代码片段:
Double myLatitude = AppObject.myLocation.getLatitude();
Double myLongitude = AppObject.myLocation.getLongitude();
DEBUG.i(TAG, "My location: " + myLatitude + " | " + myLongitude);
Geocoder geocoder = new Geocoder(MainActivity.this);
java.util.List<Address> addressList;
try {
addressList = geocoder.getFromLocation(myLatitude, myLongitude, 5);
if(addressList!=null && addressList.size()>0) {
currentAddress = new String();
DEBUG.i(TAG,addressList.get(0).toString());
currentAddress = addressList.get(0).getAddressLine(0) + ", "
+ addressList.get(0).getAddressLine(1) + ", "
+ addressList.get(0).getAddressLine(2);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}