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

來源:互聯網
上載者:User

標籤:

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

<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傳值的形式了,先初始化城市檢索核心類

/**      * 城市內搜尋      */      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設定輸入監聽

autosearchresult.addTextChangedListener(this);@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}// 輸入完成後自動進行檢索調用以上citySearch方法@Overridepublic void afterTextChanged(Editable s) {String searchdialog=s.toString();if(searchdialog.length()>1){citySearch(0, searchdialog, 20);}}  

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

private ArrayList<String> addrs=new ArrayList<String>();private ArrayList<String> names=new ArrayList<String>();private ArrayList<LatLng> mlLatLngs=new ArrayList<LatLng>();@Overridepublic void onGetPoiDetailResult(PoiDetailResult arg0) {//擷取Place詳情頁檢索結果}@Overridepublic 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對象,避免可能會報null 指標異常

@Overridepublic void onBackPressed() {super.onBackPressed();if(mPoiSearch!=null){mPoiSearch.destroy();}}@Overridepublic void onDestroy() {super.onDestroy();if(mPoiSearch!=null){mPoiSearch.destroy();}}

覺得好的可以收藏哦,覺得有問題的可以提出來哦

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.