android中google“定位查询”编辑

作者在 2011-10-27 16:38:37 发布以下内容

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

【代码清单16-1】 

  1. /** 
  2.  
  3.  * 定位查询 
  4.  
  5.  */  
  6.   
  7. private void locate() {  
  8.   
  9.    
  10.   
  11.          LayoutInflater factory= LayoutInflater.from(MyMapActivity.this);  
  12.   
  13.          View locationView =factory.inflate(R.layout.find_dialog, null);  
  14.   
  15.          final EditTextfindText = (EditText) locationView  
  16.   
  17.                            .findViewById(R.id.dailog_find);  
  18.   
  19.          newAlertDialog.Builder(this).setTitle(R.string.dialog_find).setView(  
  20.   
  21.                            locationView).setPositiveButton(R.string.button_ok,  
  22.   
  23.                            newDialogInterface.OnClickListener() {  
  24.   
  25.                                     public void onClick(DialogInterfacedialog, int whichButton) {  
  26.   
  27.                                              findString= findText.getText().toString();  
  28.   
  29.                                              progDialog =ProgressDialog.show(MyMapActivity.this,  
  30.   
  31.                                                                "处理中...""定位" +findString, truefalse);  
  32.   
  33.                                              new Thread() {  
  34.   
  35.                                                       @Override  
  36.   
  37.                                                       publicvoid run() {  
  38.   
  39.                                                                try{  
  40.   
  41.                                                                         Geocoder geocoder = new Geocoder(  
  42.   
  43.                                                                                           MyMapActivity.this);  
  44.   
  45.                                                                         addresses =geocoder.getFromLocationName(  
  46.   
  47.                                                                                           findString, 1);  
  48.   
  49.    
  50.   
  51.                                                                         if (addresses.size() > 0) {  
  52.   
  53.                                                                List<OverlayItem>overlayitems = new ArrayList<OverlayItem>();  
  54.   
  55.                                                                                  doublelat = addresses.get(0)  
  56.   
  57.                                                                                                    .getLatitude();  
  58.   
  59.                                                                                  doublelng = addresses.get(0)  
  60.   
  61.                                                                                                    .getLongitude();  
  62.   
  63.                                                                                  //设定中心点  
  64.   
  65.                                                                                  centerPoit= new GeoPoint(  
  66.   
  67.                                                                                                    (int)(lat * 1E6),  
  68.   
  69.                                                                                                    (int)(lng * 1E6)); // 地理坐标  
  70.   
  71.                                                                                  mc.setCenter(centerPoit);  
  72.   
  73.                                                                                  Log.i(TAG," lat " + lat + "  lng"  
  74.   
  75.                                                                                                    +lng);  
  76.   
  77.                                                                                  intintMaxAddressLineIndex = addresses  
  78.   
  79.                                                                                                    .get(0)  
  80.   
  81.                                                                                                    .getMaxAddressLineIndex();  
  82.   
  83.                                                                                  Stringaddress = "地址:";  
  84.   
  85.                                                                                  for(int j = 0; j <= intMaxAddressLineIndex; j++) {  
  86.   
  87.                                                                                           if (addresses.get(0) == null)  
  88.   
  89.                                                                                                    continue;  
  90.   
  91.                                                                                           address += addresses.get(0)  
  92.   
  93.                                                                                                             .getAddressLine(j)  
  94.   
  95.                                                                                                             + ",";  
  96.   
  97.                                                                                  }  
  98.   
  99.                                                                                  if(address.endsWith(",")) {  
  100.   
  101.                                                                                           address = address.substring(0,  
  102.   
  103.                                                                                                             address.length() - 1);  
  104.   
  105.                                                                                  }  
  106.   
  107.                                                                                  Stringtitle = "";  
  108.   
  109.                                                                                  if(addresses.get(0).getFeatureName() == null) {  
  110.   
  111.                                                                                           title = "";  
  112.   
  113.                                                                                  }else {  
  114.   
  115.                                                                                           title = addresses.get(0)  
  116.   
  117.                                                                                                             .getFeatureName();  
  118.   
  119.                                                                                  }  
  120.   
  121.                                                                                  overlayitems.add(newOverlayItem(  
  122.   
  123.                                                                                                    centerPoit,title, address));  
  124.   
  125.    
  126.   
  127.                                                                                  Drawablemarker = getResources()  
  128.   
  129.                                                                                                    .getDrawable(  
  130.   
  131.                                                                                                                      R.drawable.markermap2);  
  132.   
  133.                                                                                  locs= new LocationItemsOverlay(marker,  
  134.   
  135.                                                                                                    overlayitems);  
  136.   
  137.    
  138.   
  139.                                                                                  handler.sendEmptyMessage(0);  
  140.   
  141.                                                                         } else {  
  142.   
  143.                                                                                  handler.sendEmptyMessage(1);  
  144.   
  145.                                                                         }  
  146.   
  147.                                                                }catch (Exception e) {  
  148.   
  149.                                                                         e.printStackTrace();  
  150.   
  151.                                                                         handler.sendEmptyMessage(1);  
  152.   
  153.                                                                }  
  154.   
  155.                                                       }  
  156.   
  157.    
  158.   
  159.                                              }.start();  
  160.   
  161.                                     }  
  162.   
  163.                            }).setNegativeButton(R.string.button_cancel,  
  164.   
  165.                            newDialogInterface.OnClickListener() {  
  166.   
  167.    
  168.   
  169.                                     public void onClick(DialogInterfacedialog, int which) {  
  170.   
  171.                                     }  
  172.   
  173.    
  174.   
  175.                            }).show();  
  176.   
  177. }  


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

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()方法关闭进度条对话框,否则进度条对话框不会关闭。

  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开发案例驱动教程》第十六章
默认分类 | 阅读 643 次
文章评论,共0条
游客请输入验证码
浏览7235次
文章分类
文章归档
最新评论