基於指定文本的百度地圖poi城市檢索的使用(思路最重要)


(轉載請注明出處哦)具體的百度地圖權限和apikey配置以及基礎地圖的配置不敘述,百度地圖定位可以看這個鏈接的http://blog.csdn.net/heweigzf/article/details/51084358,先來看一波搜索需要的基本布局layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<AutoCompleteTextView
             android:id= "@+id/autosearchresult"
             android:background= "@null"
             android:hint= "@string/please_input_addr"
             android:layout_width= "match_parent"
              android:layout_height= "@dimen/y30"
             android:completionThreshold= "2"
         android:background= "@drawable/edittext_shap"
             android:singleLine= "true" />
<ListView
         android:id= "@+id/poimsglist"
         android:divider= "@color/grey"
         android:dividerHeight= "1px"
         android:layout_width= "match_parent"
         android:layout_height= "match_parent" />

AutoCompleteTextView可以換成EditTextView,都是可以的,既然是城市poi檢索,就會有需要的城市名,可以是定位得到的也可以是傳值過來的,這里我就以Intent傳值的形式了,先初始化城市檢索核心類

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
      * 城市內搜索
      */ 
     private  void  citySearch( int  page,String keyword, int  pageCapacity) { 
         Intent intent=getIntent();
         if (intent!= null ){
             String city=intent.getStringExtra( "city" );
             if (city!= null ){
                mPoiSearch=PoiSearch.newInstance();
                mPoiSearch.setOnGetPoiSearchResultListener( this );  // 監聽檢索結果回調
                 // 設置檢索參數 
                 PoiCitySearchOption citySearchOption =  new  PoiCitySearchOption(); 
                 citySearchOption.city(city); // 城市 
                 citySearchOption.keyword(keyword); // 關鍵字 
                 citySearchOption.pageCapacity(pageCapacity); // 默認每頁10條 
                 citySearchOption.pageNum(page); // 分頁編號 
                 // 發起檢索請求 
                 mPoiSearch.searchInCity(citySearchOption);
             }
         }
}

接下來就是運用以上的方法了,可以明顯的看出來我們是通過輸入框來自動檢索,需要的給AutoCompleteTextView設置輸入監聽

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
autosearchresult.addTextChangedListener( this );
 
@Override
     public  void  beforeTextChanged(CharSequence s,  int  start,  int  count,
             int  after) {
     }
 
     @Override
     public  void  onTextChanged(CharSequence s,  int  start,  int  before,  int  count) {
     }
 
     // 輸入完成后自動進行檢索調用以上citySearch方法
     @Override
     public  void  afterTextChanged(Editable s) {
         String searchdialog=s.toString();
         if (searchdialog.length()> 1 ){
             citySearch( 0 , searchdialog,  20 );
         }
    

檢索成功后,以下為OnGetPoiSearchResultListener回調的檢索結果,getAllPoi()包含所有的檢索結果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private  ArrayList<String> addrs= new  ArrayList<String>();
private  ArrayList<String> names= new  ArrayList<String>();
private  ArrayList<LatLng> mlLatLngs= new  ArrayList<LatLng>();
 
@Override
     public  void  onGetPoiDetailResult(PoiDetailResult arg0) {
         //獲取Place詳情頁檢索結果
     }
 
     @Override
     public  void  onGetPoiResult(PoiResult arg0) {
         //獲取POI檢索結果
         if (arg0.error!=SearchResult.ERRORNO.NO_ERROR){
             //BaseApp.getInstance().showToast("檢索fail");
         } else  {
             if (addrs!= null ){
                 addrs.clear();
             }
             if (mlLatLngs!= null ){
                 mlLatLngs.clear();
             }
             if (names!= null ){
                 names.clear();
             }
             if (arg0.getAllPoi()!= null &&arg0.getAllPoi().size()> 0 ){
                 List<PoiInfo> mPoiInfos=arg0.getAllPoi();
                 for  ( int  i =  0 ; i < mPoiInfos.size(); i++) {
                     names.add(mPoiInfos.get(i).name);
                     addrs.add(mPoiInfos.get(i).address);
                     mlLatLngs.add(mPoiInfos.get(i).location);
                 //對需要的信息設置適配器,如果想在其他界面用,可以自己創建回調接口
mSearchListAdapter= new  SearchListAdapter(addrs, names, BaseApp.getInstance());
                     poiresultlist.setAdapter(mSearchListAdapter);
                 }
             }
         }
     }

最后很重要的一步,要記得銷毀poisearch對象,避免可能會報空指針異常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
     public  void  onBackPressed() {
         super .onBackPressed();
         if (mPoiSearch!= null ){
             mPoiSearch.destroy();
         }
     }
     
     @Override
     public  void  onDestroy() {
         super .onDestroy();
         if (mPoiSearch!= null ){
             mPoiSearch.destroy();
         }
     }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM