“定位查询”locate()方法,增加一个线程,在该线程中处理查询地点的功能,请参考代码清单16-10,完整代码请参考chapter16_7工程中src/com/work/map/MyMapActivity.java文件locate()方法代码部分。

【代码清单16-1】

 

  1. /** 
  2. * 定位查询 
  3. */ 
  4. private void locate() { 
  5.  
  6. LayoutInflater factory = LayoutInflater.from(MyMapActivity.this); 
  7. View locationView = factory.inflate(R.layout.find_dialog, null); 
  8. final EditText findText = (EditText) locationView 
  9. .findViewById(R.id.dailog_find); 
  10. new AlertDialog.Builder(this).setTitle(R.string.dialog_find).setView( 
  11. locationView).setPositiveButton(R.string.button_ok, 
  12. new DialogInterface.OnClickListener() { 
  13. public void onClick(DialogInterface dialog, int whichButton) { 
  14. findString = findText.getText().toString(); 
  15. progDialog = ProgressDialog.show(MyMapActivity.this
  16. "处理中...""定位" + findString, truefalse); 
  17. new Thread() { 
  18. @Override 
  19. public void run() { 
  20. try { 
  21. Geocoder geocoder = new Geocoder( 
  22. MyMapActivity.this); 
  23. addresses = geocoder.getFromLocationName( 
  24. findString, 1); 
  25.  
  26. if (addresses.size() > 0) { 
  27. List<OverlayItem> overlayitems = new ArrayList<OverlayItem>(); 
  28. double lat = addresses.get(0
  29. .getLatitude(); 
  30. double lng = addresses.get(0
  31. .getLongitude(); 
  32. // 设定中心点 
  33. centerPoit = new GeoPoint( 
  34. (int) (lat * 1E6), 
  35. (int) (lng * 1E6)); // 地理坐标 
  36. mc.setCenter(centerPoit); 
  37. Log.i(TAG, " lat " + lat + "  lng " 
  38. + lng); 
  39. int intMaxAddressLineIndex = addresses 
  40. .get(0
  41. .getMaxAddressLineIndex(); 
  42. String address = "地址:"
  43. for (int j = 0; j <= intMaxAddressLineIndex; j++) { 
  44. if (addresses.get(0) == null
  45. continue
  46. address += addresses.get(0
  47. .getAddressLine(j) 
  48. ","
  49. if (address.endsWith(",")) { 
  50. address = address.substring(0
  51. address.length() - 1); 
  52. String title = ""
  53. if (addresses.get(0).getFeatureName() == null) { 
  54. title = ""
  55. else { 
  56. title = addresses.get(0
  57. .getFeatureName(); 
  58. overlayitems.add(new OverlayItem( 
  59. centerPoit, title, address)); 
  60.  
  61. Drawable marker = getResources() 
  62. .getDrawable( 
  63. R.drawable.markermap2); 
  64. locs = new LocationItemsOverlay(marker, 
  65. overlayitems); 
  66.  
  67. handler.sendEmptyMessage(0); 
  68. else { 
  69. handler.sendEmptyMessage(1); 
  70. catch (Exception e) { 
  71. e.printStackTrace(); 
  72. handler.sendEmptyMessage(1); 
  73.  
  74. }.start(); 
  75. }).setNegativeButton(R.string.button_cancel, 
  76. new DialogInterface.OnClickListener() { 
  77.  
  78. public void onClick(DialogInterface dialog, int which) { 
  79.  
  80. }).show(); 
  81. }  

通过下面的代码是实现显示进度条:

progDialog =ProgressDialog.show(MyMapActivity.this, "处理中...", "定位" + findString, true, false);

启动一个子线程,在该线程中实现地点查询,但是不能有更新UI的处理,如果查询成功调用handler.sendEmptyMessage(0),如果失败调用handler.sendEmptyMessage(1)。

 

  1. new Thread() { 
  2.  
  3.          @Override 
  4.  
  5.          public void run() { 
  6.  
  7. … … 
  8.  
  9.  
  10. }.start(); 

在Hander的handleMessage方法中处理更新UI操作,其中成功(case 0)时候清除屏幕上原来的图层,重新添加图层,最后progDialog.dismiss()方法关闭进度条对话框。如果是查询失败(case 1)弹出Toast说明一下,也要通过progDialog.dismiss()方法关闭进度条对话框,否则进度条对话框不会关闭。

 

  1. private Handler handler = new Handler() { 
  2.  
  3.   
  4.  
  5.          @Override 
  6.  
  7.          public voidhandleMessage(Message msg) { 
  8.  
  9.                   switch (msg.what) { 
  10.  
  11.                   case 0
  12.  
  13.                            mapView.getOverlays().clear(); 
  14.  
  15.                            mapView.getOverlays().add(locs); 
  16.  
  17.                            progDialog.dismiss(); 
  18.  
  19.                            break
  20.  
  21.                   case 1
  22.  
  23.                            Toast.makeText(MyMapActivity.this,"暂时无法" + findString + "信息。"
  24.  
  25.                                              Toast.LENGTH_SHORT).show(); 
  26.  
  27.                            progDialog.dismiss(); 
  28.  
  29.                   } 
  30.  
  31.          } 
  32.  
  33. }; 

 

                                                    出自《Android开发案例驱动教程》第十六章