620
技術社區[雲棲]
android中google“定位查詢”編輯
“定位查詢”locate()方法,增加一個線程,在該線程中處理查詢地點的功能,請參考代碼清單16-10,完整代碼請參考chapter16_7工程中src/com/work/map/MyMapActivity.java文件locate()方法代碼部分。
【代碼清單16-1】
/** * 定位查詢 */ private void locate() { LayoutInflater factory= LayoutInflater.from(MyMapActivity.this); View locationView =factory.inflate(R.layout.find_dialog, null); final EditTextfindText = (EditText) locationView .findViewById(R.id.dailog_find); newAlertDialog.Builder(this).setTitle(R.string.dialog_find).setView( locationView).setPositiveButton(R.string.button_ok, newDialogInterface.OnClickListener() { public void onClick(DialogInterfacedialog, int whichButton) { findString= findText.getText().toString(); progDialog =ProgressDialog.show(MyMapActivity.this, "處理中...", "定位" +findString, true, false); new Thread() { @Override publicvoid run() { try{ Geocoder geocoder = new Geocoder( MyMapActivity.this); addresses =geocoder.getFromLocationName( findString, 1); if (addresses.size() > 0) { List<OverlayItem>overlayitems = new ArrayList<OverlayItem>(); doublelat = addresses.get(0) .getLatitude(); doublelng = addresses.get(0) .getLongitude(); //設定中心點 centerPoit= new GeoPoint( (int)(lat * 1E6), (int)(lng * 1E6)); // 地理坐標 mc.setCenter(centerPoit); Log.i(TAG," lat " + lat + " lng" +lng); intintMaxAddressLineIndex = addresses .get(0) .getMaxAddressLineIndex(); Stringaddress = "地址:"; for(int j = 0; j <= intMaxAddressLineIndex; j++) { if (addresses.get(0) == null) continue; address += addresses.get(0) .getAddressLine(j) + ","; } if(address.endsWith(",")) { address = address.substring(0, address.length() - 1); } Stringtitle = ""; if(addresses.get(0).getFeatureName() == null) { title = ""; }else { title = addresses.get(0) .getFeatureName(); } overlayitems.add(newOverlayItem( centerPoit,title, address)); Drawablemarker = getResources() .getDrawable( R.drawable.markermap2); locs= new LocationItemsOverlay(marker, overlayitems); handler.sendEmptyMessage(0); } else { handler.sendEmptyMessage(1); } }catch (Exception e) { e.printStackTrace(); handler.sendEmptyMessage(1); } } }.start(); } }).setNegativeButton(R.string.button_cancel, newDialogInterface.OnClickListener() { public void onClick(DialogInterfacedialog, int which) { } }).show(); }
通過下麵的代碼是實現顯示進度條:
progDialog =ProgressDialog.show(MyMapActivity.this, "處理中...", "定位" + findString, true, false);
啟動一個子線程,在該線程中實現地點查詢,但是不能有更新UI的處理,如果查詢成功調用handler.sendEmptyMessage(0),如果失敗調用handler.sendEmptyMessage(1)。
new Thread() {
@Override
public void run() {
… …
}
}.start();
在Hander的handleMessage方法中處理更新UI操作,其中成功(case 0)時候清除屏幕上原來的圖層,重新添加圖層,最後progDialog.dismiss()方法關閉進度條對話框。如果是查詢失敗(case 1)彈出Toast說明一下,也要通過progDialog.dismiss()方法關閉進度條對話框,否則進度條對話框不會關閉。
private Handler handler = new Handler() { @Override public voidhandleMessage(Message msg) { switch (msg.what) { case 0: mapView.getOverlays().clear(); mapView.getOverlays().add(locs); progDialog.dismiss(); break; case 1: Toast.makeText(MyMapActivity.this,"暫時無法" + findString + "信息。", Toast.LENGTH_SHORT).show(); progDialog.dismiss(); } } };
出自《Android開發案例驅動教程》第十六章
最後更新:2017-04-02 06:51:49