我搶從做反向地理編碼(transfering從數字的位置,以文本)Android的網站代碼ReverseGeocodingTask任務的方法
它工作正常,但因爲我的應用程序已經改變了一點,我需要改變這段代碼變成了一個正常的方法,現在它是一個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;
}
}
反向地理編碼需要大量的時間,你不應該在UI線程上完成。你想達到什麼目的? – JeffS
在將位置發送到數據庫的線程上。所以在我發送它之前,我想將它轉換成一個字符串。 –