Use of Baidu Map POI City search based on specified text (thinking most important)

Source: Internet
Author: User

(reproduced please specify the source OH) specific Baidu map permissions and Apikey configuration and the configuration of the base map is not described, Baidu map positioning can see this link http://blog.csdn.net/heweigzf/article/details/ 51084358, first look at the basic layout of the search needs

<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 can be replaced by edittextview , all can, since is the city poi Search, there will be the need for the city name, can be located or can be transmitted value, here I will intent In the form of a value, first initialize the city search core class

/** * in-City search */private void Citysearch (int page,string keyword,int PAG    ecapacity) {Intent intent=getintent ();    if (intent!=null) {String City=intent.getstringextra ("City");       if (city!=null) {mpoisearch=poisearch.newinstance (); Mpoisearch.setongetpoisearchresultlistener (this);              Listener retrieval result callback//Set retrieval parameter poicitysearchoption citysearchoption = new Poicitysearchoption (); Citysearchoption.city (city);//urban Citysearchoption.keyword (keyword);//Keywords citysearchoption.              Pagecapacity (pagecapacity);//default per page 10 Citysearchoption.pagenum (page);//page numbering//initiating retrieval requests     Mpoisearch.searchincity (citysearchoption); }    } }

The next step is to use the above method, we can clearly see that we are automatically retrieved through the input box, the need for autocompletetextview set input listener

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) {}//Automatic retrieval call after input completion The above Citysearch method @overridepublic void aftertextchanged (Editable s) {String searchdialog=s.tostring (); Searchdialog.length () >1) {citysearch (0, Searchdialog, 20);}}  

after successful retrieval, the following is Ongetpoisearchresultlistener The result of the callback retrieval, Getallpoi () contains all the results of the search

Private arraylist<string> addrs=new arraylist<string> ();p rivate arraylist<string> names=new Arraylist<string> ();p rivate arraylist<latlng> mllatlngs=new arraylist<latlng> (); @Overridepublic void Ongetpoidetailresult (Poidetailresult arg0) {//Get Place Details page search result} @Overridepublic void Ongetpoiresult (Poiresult arg0 {//Get POI Search Result if (arg0.error!=searchresult.errorno.no_error) {//baseapp.getinstance (). Showtoast ("retrieve 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);//Set the adapter for the required information, and create a callback interface yourself if you want to use it on a different interface msearchlistadapter=new Searchlistadapter (Addrs, names, Baseapp.getinstance ());p oiresultlist.setadapter (Msearchlistadapter);}}}

The last important step is to remember to destroy Poisearch object to avoid the possibility of reporting a null pointer exception

@Overridepublic void onbackpressed () {super.onbackpressed (); if (mpoisearch!=null) {Mpoisearch.destroy ();}} @Overridepublic void OnDestroy () {Super.ondestroy (); if (mpoisearch!=null) {Mpoisearch.destroy ();}}

Feel good can collect oh, think there is a problem can come up OH

Use of Baidu Map POI City search based on specified text (thinking most important)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.