我从 android 站点获取了一个代码,该代码执行反向地理编码(将位置从数字传输到文本)
它工作正常,但由于我的应用程序发生了一些变化,我需要将此代码更改为普通方法,现在它是一个 AsyncTask。它应该得到一个位置并返回一个字符串。
这段代码对我来说有点奇怪,所以我需要你们的帮助:
private class ReverseGeocodingTask extends AsyncTask<Location, Void, Void>
{
Context mContext;
public ReverseGeocodingTask(Context context)
{
super();
mContext = context;
}
@Override
protected Void doInBackground(Location... params)
{
Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
Location loc = params[0];
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
// Update address field with the exception.
LocationService.this.address = e.toString();
}
if (addresses != null && addresses.size() > 0)
{
Address address = addresses.get(0);
// Format the first line of address (if available), city, and country name.
String addressText = String.format("%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
// Update address field on UI.
// Message.obtain(mHandler, UPDATE_ADDRESS, addressText).sendToTarget();
LocationService.this.address = addressText;
}
return null;
}
}