Android Baidu map SDK v3_3_0 (5), androidv3_3_0

Source: Internet
Author: User

Android Baidu map SDK v3_3_0 (5), androidv3_3_0

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

Currently, Baidu map SDK integrates the following search services: POI search, bus information query, route planning, geographic code, online suggestion query, and short string sharing.

This blog will first introduce POI search and online suggestion query (based on the MAP status function, you do not know how to locate the boots, please refer to Android Baidu map SDK v3.3.0 (2) --- map positioning and layer display)

Baidu map SDK provides three types of POI searches: Peripheral search, regional search, and city search. The following uses city search as an example to describe how to use the search service.

Before introducing the search service, you want to implement the following:

Okay! Now we can use the code to implement the above functions (corresponding annotations are made in the Code)

1. Create POI and online suggestion query instances

/*** Initialize the search module and register the search event listener */private void initPOIListener () {// POI search instance mreceivearch = receivearch. newInstance (); // create a POI search listener mPoiSearch. setongetincluearchresultlistener (this); // Lenovo word search instance mSuggestionSearch = SuggestionSearch. newInstance (); // Lenovo word search listener mSuggestionSearch. setOnGetSuggestionResultListener (this );}

2. Create a POI search listener and handle related events.

@ Overridepublic void onGetSuggestionResult (SuggestionResult res) {if (res = null | res. getAllSuggestions () = null) {return;} sugAdapter. clear (); for (SuggestionResult. suggestionInfo info: res. getAllSuggestions () {if (info. key! = Null) sugAdapter. add (info. key);} sugAdapter. policydatasetchanged () ;}@ Overridepublic void onGetPoiDetailResult (PoiDetailResult result) {// No result 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) {// if (result = null | result. error = SearchResult. ERRORNO. RESULT_NOT_FOUND) {Toast. makeText (MainActivity. this, "no results found", Toast. LENGTH_LONG ). show (); return;} // The result is not abnormal. 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;} // if the keyword is not found in this city but is found in another city, the list of cities containing this keyword is returned if (result. error = SearchResult. ERRORNO. AMBIGUOUS_KEYWORD) {String strInfo = "in"; for (CityInfo cityInfo: result. getSuggestCityList () {strInfo + = cityInfo. city; strInfo + = "," ;}strinfo + = "find result"; Toast. makeText (MainActivity. this, strInfo, Toast. LENGTH_LONG ). show () ;}}/*** provides a method to identify search results on a map ** @ author TanZuAi **/private class MyPoiOverlay extends PoiOverlay {public MyPoiOverlay (BaiduMap baiduMap) {super (baiduMap) ;}@ Overridepublic boolean onPoiClick (int index) {super. onPoiClick (index); // obtain the poi covering details. PoiInfo poi = getPoiResult (). getAllPoi (). get (index); // uid is the poi id information obtained in the POI search. searchPoiDetail (new PoiDetailSearchOption ()). poiUid (poi. uid); return true ;}}

3. Set button related events.

/*** Affected Search button click event ** @ param v */public void searchButtonProcess (View v) {EditText editCity = (EditText) findViewById (R. id. city); // enter the city EditText editSearchKey = (EditText) findViewById (R. id. searchkey); // The input keyword // initiate the retrieval request mPoiSearch. searchInCity (new PoiCitySearchOption ()). city (editCity. getText (). toString () // Based on the city. keyword (editSearchKey. getText (). toString () // Based on the keyword. pageNum (load_Index); // number of pages to be queried}/*** query each page added ** @ param v */public void goToNextPage (View v) {load_Index ++; searchButtonProcess (null );}
4. To save power and resources, you must set the search lifecycle.

@ Overrideprotected void onDestroy () {mshortearch. destroy (); mSuggestionSearch. destroy (); // execute mMapView when onDestroy is executed in activity. onDestroy () to implement mMapView for map lifecycle management. onDestroy (); super. onDestroy () ;}@ Overrideprotected void onResume () {super. onResume (); // execute mMapView when onResume is executed in the activity. onResume () to implement mMapView for map lifecycle management. onResume () ;}@ Overrideprotected void onPause () {super. onPause (); // execute mMapView when onPause is executed in activity. onPause () enables mMapView for map lifecycle management. onPause ();}

Okay! Code implementation has been introduced! I believe everyone can understand it! If you have any questions or suggestions, leave a message below!

The following is the source code of this blog:

Source code

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.