我正在構建一個android應用程序,並且我正在使用地理編碼器來解碼位置,但我注意到有時當連接性相對較低時,Gecoder不起作用。但在同一時間,瀏覽器和其他基於互聯網的應用程序的功能。有沒有什麼辦法解決這一問題 ?這件事可能會出錯。我明白它的後端服務,但它不應該。對於常規的http請求,我們可以重試並修復類似的問題,但我該如何解決這個問題?有什麼建議麼?地理編碼器侷限性
class ReverseGeocodingTask extends AsyncTask<Double, Void, String> implements OnDismissListener{
Context context;
ProgressDialog progDialog;
String progressString;
public ReverseGeocodingTask(Context context, String progressStr) {
this.context = context;
this.progressString = progressStr;
if(!isCancelled()){
initProgDialog();
}
}
void initProgDialog(){
progDialog = new ProgressDialog(context);
progDialog.setCanceledOnTouchOutside(false);
progDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.btn_cancel),new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
progDialog.dismiss();
}
});
progDialog.setMessage(progressString);
progDialog.setOnDismissListener(this);
progDialog.show();
}
@Override
protected void onPreExecute() {
//removeAllPendingRevGeocodingTasks();
addTask(this);
super.onPreExecute();
}
@Override
protected String doInBackground(Double... params) {
String result = null;
if(!isCancelled()){
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> list = geocoder.getFromLocation(latitude, longitude, 1);
if (list != null && list.size() > 0) {
Address address = list.get(0);
result = address.getAddressLine(0) + ", " + address.getLocality();
}
} catch (IOException e) {
progDialog.dismiss();
failedToUpdateLocation();
Log.e("Location listener", "Failed to decode location name");
}
}
return result;
}
@Override
protected void onCancelled() {
progDialog.dismiss();
this.cancel(true);
super.onCancelled();
}
@Override
protected void onPostExecute(String result) {
latLongInDegrees = " "+latLongInDegreesToDMSString(latitude,longitude);
setLocationName(result);
updateLocationName(result + latLongInDegrees);
String progressStr=getString(R.string.msg_slp_fetch_from_geocoder_result_1)+latLongInDegreesToDMSString(latitude,longitude)+"...";
executeSLPWebserviceTask(latitude, longitude, progressStr);
finishedUpdatingLocation(latitude,longitude);
progDialog.dismiss();
super.onPostExecute(result);
this.cancel(true);
//removeAllPendingRevGeocodingTasks();
}
public void onDismiss(DialogInterface dialog) {
this.cancel(true);
}
}
protected void executeRevGeocoderTask(double lat, double longitude)
{
String progressStr=getString(R.string.msg_rev_geocoder_fetch)+" "+latLongInDegreesToDMSString(lat,longitude);
ReverseGeocodingTask task = new ReverseGeocodingTask(this,progressStr);
task.execute(lat,longitude);
}
是的,有時Geocoder失敗。你可以請你發佈你的代碼嗎? – Scorpion 2012-03-13 06:25:52
我已經發布了我的代碼,Btw ..任何機會,是Geocoder使用谷歌服務,在那裏在Android? – 2012-03-13 06:36:32
我想是的。我讀了Google的地理編碼器,你可以從IP發送1個請求/ 15秒... – Scorpion 2012-03-13 06:40:01