Android Baidu Map SDK v3_3_0 (v)---poi search and online suggestion query function

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/tanzuai/article/details/43835431

at present, Baidu Map SDK integrated search services include: POI retrieval, bus information query, line planning, geocoding, online advice query, short string sharing.

This blog will first introduce the POI search and online advice query (based on the map status function, but also do not know the positioning of the child boots, please refer to the Android Baidu map SDK v3.3.0 (ii)---Map positioning and layer display)

Baidu Map SDK provides three types of POI search: Peripheral Search, regional search and in-City search. The following is an example of an in-city search, which shows you how to use the retrieval service.

Before you introduce the search service, think of the previous one that you want to implement:

All right! Now we have the code to implement the above function (the code has made the corresponding annotations)

1. Create POI and online suggestion query instances

/** * Initialize search module, register search event listener */private void Initpoilistener () {//poi Retrieve instance Mpoisearch = Poisearch.newinstance ();// Create POI Retrieval Listener mpoisearch.setongetpoisearchresultlistener (this);//associative word retrieval instance Msuggestionsearch = Suggestionsearch.newinstance ();//Lenovo Word Retrieval listener msuggestionsearch.setongetsuggestionresultlistener (this);}

2.Create a POI to retrieve listeners and do related event handling

@Overridepublic void Ongetsuggestionresult (Suggestionresult res) {if (res = = NULL | | res.getallsuggestions () = = null) {RET Urn;} Sugadapter.clear (); for (Suggestionresult.suggestioninfo info:res.getAllSuggestions ()) {if (Info.key! = null) Sugadapter.add (Info.key);} Sugadapter.notifydatasetchanged ();} @Overridepublic void Ongetpoidetailresult (poidetailresult result) {//Results not found if (result.error! = SearchResult.ERRORNO.NO_ERROR) {Toast.maketext (mainactivity.this, "Sorry, no results found", Toast.length_short). Show ();} else { Toast.maketext (Mainactivity.this,result.getname () + ":" + result.getaddress (), Toast.length_short). Show ();}} @Overridepublic void Ongetpoiresult (poiresult result) {//Results not found if (result = = Null| | result.error = = Searchresult.errorno. Result_not_found) {Toast.maketext (mainactivity.this, "Results not Found", Toast.length_long). Show (); return;} The result is no exception, finding the result if (Result.error = = SearchResult.ERRORNO.NO_ERROR) {mbaidumap.clear (); Poioverlay overlay = new Mypoioverlay (mbaidumap); Mbaidumap.setonmarkerclicklistener(overlay); Overlay.setdata (result); Overlay.addtomap (); Overlay.zoomtospan (); return;} When the input keyword is not found in the city, but found in other cities, returns the list of cities that contain the keyword if (result.error = = SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {String Strinfo = "in"; for (Cityinfo cityInfo:result.getSuggestCityList ()) {Strinfo + = Cityinfo.city;strinfo + = ",";} Strinfo + = "Find results"; Toast.maketext (Mainactivity.this, Strinfo, Toast.length_long). Show ();}} /** * Provides a way to identify search results on a map * * @author Tanzuai * */private class Mypoioverlay extends Poioverlay {public Mypoioverlay (baidum AP Baidumap) {super (BAIDUMAP);} @Overridepublic boolean onpoiclick (int index) {super.onpoiclick (index);//Get POI overlay details Poiinfo poi = Getpoiresult (). Getallpoi (). get (index);//UID is the POI ID information obtained in the POI Retrieval Mpoisearch.searchpoidetail ((New Poidetailsearchoption ()). Poiuid ( POI.UID)); return true;}}

3. Set the relevant event for the button.

/** * Impact Search Button Click event *  * @param v */public void searchbuttonprocess (View v) {EditText editcity = (EditText) Findviewbyid (R id.city);//input City EditText Editsearchkey = (EditText) Findviewbyid (R.id.searchkey);//input keywords// Initiate a Retrieval request mpoisearch.searchincity ((New Poicitysearchoption ()). City (Editcity.gettext (). toString ())///According to cities. Keyword ( Editsearchkey.gettext (). toString ()//according to the keyword. Pagenum (load_index))///query the number of pages}/** * query for each add page *  * @param v */public void Gotonextpage (View v) {load_index++;searchbuttonprocess (null);}
4. To save power and resources, set the relevant life cycle of the search

@Overrideprotected void OnDestroy () {Mpoisearch.destroy (); Msuggestionsearch.destroy ();// Execute Mmapview.ondestroy () when activity executes OnDestroy, implement map Lifecycle Management Mmapview.ondestroy (); Super.ondestroy ();} @Overrideprotected void Onresume () {super.onresume ();//Mmapview is executed when activity executes Onresume. Onresume (), Implement Map Lifecycle Management Mmapview.onresume ();} @Overrideprotected void OnPause () {super.onpause ();//Mmapview is executed when activity executes OnPause. OnPause (), Implement Map Lifecycle Management Mmapview.onpause ();}

All right! The code implementation has been introduced! I believe everyone can read it! What do not understand or suggestions, you can leave a message below!

Below is the source code of this blog:

Source code

Android Baidu Map SDK v3_3_0 (v)---poi search and online suggestion query function

Related Article

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.