This article describes how to use the Baidu map API to query the corresponding address information based on a latitude and longitude value (geographic coordinate) and the point of interest (POI) information around the address.
The Baidu map mobile edition API not only contains the basic interface for creating a map, but also integrates many search services, including: location search, peripheral search, range search, bus search, Driver/Passenger search, walking search, address information query, etc.
The search service provided by Baidu map mobile API is mainly implemented by initializing the mksearch class and registering the listening object mksearchlistener for the search results. First, you need to customize a mysearchlistener class, which implements the mksearchlistener interface, and then obtain the corresponding search results by implementing different callback methods in the interface. The mysearchlistener class is defined as follows:
/** <Br/> * implements the mksearchlistener interface to Implement Asynchronous search services, search Result <br/> * @ author Liufeng <br/> */<br/> public class mysearchlistener implements mksearchlistener {<br/>/** <br/> * search address information result based on latitude and longitude <br/> * @ Param result search result <br/> * @ Param ierror error code (0 indicates a correct return) <br/> */<br/> @ override <br/> Public void ongetaddrresult (mkaddrinfo result, int ierror) {<br/>}</P> <p>/** <br/> * driving route search results <br/> * @ Param result <br/> * @ Param ierror error code (0 indicates a correct response) <br/> */<br/> @ override <br/> Public void ongetdrivingrouteresult (mkdrivingrouteresult result, int ierror) {<br/>}</P> <p>/** <br/> * poi search results (range search, city poi search, and peripheral search) <br/> * @ Param result search result <br/> * @ Param type return result type (, 21: poi list 7: City list) <br/> * @ Param ierror No. (0 indicates correct return) <br/> */<br/> @ override <br/> Public void ongetpoiresult (mkpoiresult result, int type, int ierror) {<br/>}</P> <p>/** <br/> * search results of bus transfer routes <br/> * @ Param result <br/> * @ Param ierror error code (0 indicates a correct response) <br/> */<br/> @ override <br/> Public void ongettransitrouteresult (mktransitrouteresult result, int ierror) {<br/>}</P> <p>/** <br/> * walking Route Search Result <br/> * @ Param result <br/> * @ Param ierror error code (0 indicates a correct response) <br/> */<br/> @ override <br/> Public void ongetwalkingrouteresult (mkwalkingrouteresult result, int ierror) {<br/>}< br/>}
Note:The class definition above only describes the functions of the five methods of the mksearchlistener class, all of which are empty implementations and does not provide specific implementations. Based on the content you want to search, and then implement the corresponding method above, you can get the search results. For example: 1) If you want to search for address information through a geographical coordinate (longitude and latitude value), you only need to implement the ongetaddrresult () method above to get the search result. 2) if you want to search for the driving route information, you only need to implement the ongetdrivingrouteresult () method to get the search result.
Next, initialize the mksearch class:
// Initialize mksearch <br/> mmksearch = new mksearch (); <br/> mmksearch. INIT (mapmanager, new mysearchlistener ());
After the preceding two steps, you can call some retrieval methods provided by mksearch to search for the desired information.
The following is a specific example: query the corresponding address information and the poi (point of interest) information around the address based on a latitude and longitude value (geographic coordinate.
1) Layout file Res/layout/query_address.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <scrollview xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <linearlayout <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_con Tent "<br/> Android: text =" longitude: "<br/> <edittext Android: id = "@ + ID/longitude_input" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "106.720397" <br/> </P> <p> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "latitude:" <br/> <edittext Android: id = "@ + ID/latitude_input" <B R/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "26.597239" <br/> </P> <p> <button Android: Id = "@ + ID/query_button" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_gravity = "right" <br/> Android: TEXT = "address query" <br/> </P> <p> <textview Android: Id = "@ + ID/address_text" <br/> Android: layout_width =" Wrap_content "<br/> Android: layout_height =" wrap_content "<br/> <! -- <Br/> although mapview is defined, Android: visibility = "gone" is set to hide it. <br/> This example does not require map display, but not defined (Baidu map API Requirements) <br/> --> <br/> <COM. baidu. mapapi. mapview Android: Id = "@ + ID/map_view" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: clickable = "true" <br/> Android: visibility = "gone" <br/> </linearlayout> <br/> </scrollview>
2) inherit the activity class of COM. Baidu. mapapi. mapactivity
Package COM. liufeng. baidumap; </P> <p> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. edittext; <br/> Import android. widget. textview; </P> <p> Import COM. baidu. mapapi. bmapmanager; <br/> Import COM. baidu. mapapi. geopoint; <br/> Import COM. baidu. mapapi. mkaddrinfo; <br/> Import COM. baidu. mapapi. Mkdrivingrouteresult; <br/> Import COM. baidu. mapapi. mkpoiinfo; <br/> Import COM. baidu. mapapi. mkpoiresult; <br/> Import COM. baidu. mapapi. mksearch; <br/> Import COM. baidu. mapapi. mksearchlistener; <br/> Import COM. baidu. mapapi. mktransitrouteresult; <br/> Import COM. baidu. mapapi. mkwalkingrouteresult; <br/> Import COM. baidu. mapapi. mapactivity; </P> <p>/** <br/> * query address information by latitude and longitude <br/> * @ author Liufeng <br/> * @ Date <br/> */<br/> public class queryaddressactivity extends mapactivity {<br/> // defines the map engine management class <br/> private bmapmanager mapmanager; <br/> // define the search service class <br/> private mksearch mmksearch; </P> <p> private edittext longitudeedittext; <br/> private edittext latitudeedittext; <br/> private textview addresstextview; <br/> private button querybutton; </P> <p> @ override <br/> Public void oncreate (bundle SA Vedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. query_address); </P> <p> // initialize mapactivity <br/> mapmanager = new bmapmanager (getapplication ()); <br/> // set the first parameter of the init Method to the requested API key. <br/> mapmanager. init ("285b450ebab2a92293e85502150ada7f03c777c4", null); <br/> super. initmapactivity (mapmanager); </P> <p> // initialize mksearch <br/> mmksearch = new mksearch (); <br/> mmksearch. in It (mapmanager, new mysearchlistener (); </P> <p> // query controls defined in the layout file by ID <br/> longitudeedittext = (edittext) findviewbyid (R. id. longitude_input); <br/> latitudeedittext = (edittext) findviewbyid (R. id. latitude_input); <br/> addresstextview = (textview) findviewbyid (R. id. address_text); <br/> querybutton = (button) findviewbyid (R. id. query_button); </P> <p> // click the event listener for the address query button. <br/> querybutton. setonclicklistener (n EW onclicklistener () {<br/> @ override <br/> Public void onclick (view V) {<br/> // user-input longitude value <br/> string longitudestr = longitudeedittext. gettext (). tostring (); <br/> // latitude value entered by the user <br/> string latitudestr = latitudeedittext. gettext (). tostring (); </P> <p> try {<br/> // converts the latitude and longitude values entered by the user to the int type. <br/> int longpolling = (INT) (1000000 * double. parsedouble (longitudestr); <br/> int latitude = (INT) (1000000 * double. parse Double (latitudestr); </P> <p> // query the address location information corresponding to the latitude and longitude value <br/> mmksearch. reversegeocode (New geopoint (latitude, longpolling); <br/>} catch (exception e) {<br/> addresstextview. settext ("query error. Please check the entered latitude and longitude value! "); <Br/>}< br/> }); <br/>}</P> <p> @ override <br/> protected Boolean isroutedisplayed () {<br/> return false; <br/>}</P> <p> @ override <br/> protected void ondestroy () {<br/> If (mapmanager! = NULL) {<br/> // call this method before exiting the Program <br/> mapmanager. destroy (); <br/> mapmanager = NULL; <br/>}< br/> super. ondestroy (); <br/>}</P> <p> @ override <br/> protected void onpause () {<br/> If (mapmanager! = NULL) {<br/> // terminate Baidu map API <br/> mapmanager. stop (); <br/>}< br/> super. onpause (); <br/>}</P> <p> @ override <br/> protected void onresume () {<br/> If (mapmanager! = NULL) {<br/> // enable Baidu map API <br/> mapmanager. start (); <br/>}< br/> super. onresume (); <br/>}</P> <p>/** <br/> * internal class implements the mksearchlistener interface, used to Implement Asynchronous search service <br/> * @ author Liufeng <br/> */<br/> public class mysearchlistener implements mksearchlistener {<br/> /* * <br/> * search address information result based on latitude and longitude <br/> * @ Param result search result <br/> * @ Param ierror error code (0 indicates returned correctly) <br/> */<br/> @ override <br/> Public void ongetaddrr Esult (mkaddrinfo result, int ierror) {<br/> If (result = NULL) {<br/> return; <br/>}< br/> stringbuffer sb = new stringbuffer (); <br/> // location corresponding to latitude and longitude <br/> Sb. append (result. straddr ). append ("/N"); </P> <p> // determines whether poi (point of interest) exists near the address. <br/> If (null! = Result. poilist) {<br/> // traverse all interest Point Information <br/> for (mkpoiinfo poiinfo: result. poilist) {<br/> Sb. append ("----------------------------------------"). append ("/N"); <br/> Sb. append ("name :"). append (poiinfo. name ). append ("/N"); <br/> Sb. append ("Address :"). append (poiinfo. address ). append ("/N"); <br/> Sb. append ("longitude :"). append (poiinfo.pt. getlongitudee6 ()/0000000000f ). append ("/N"); <br/> Sb. append ("latitude :"). append (poiinfo.pt. getlatitudee6 ()/0000000000f ). append ("/N"); <br/> Sb. append ("Phone :"). append (poiinfo. phonenum ). append ("/N"); <br/> Sb. append ("zip code :"). append (poiinfo. postcode ). append ("/N"); <br/> // poi type, 0: Normal, 1: bus stop, 2: bus line, 3: subway station, 4: subway Line <br/> Sb. append ("type :"). append (poiinfo. epoitype ). append ("/N"); <br/>}< br/> // display address information and interest point information on textview <br/> addresstextview. settext (sb. tostring ()); <br/>}</P> <p>/** <br/> * driving route search result <br/> * @ Param result search result <br/> * @ Param ierror code (0 indicates a correct response) <br/> */<br/> @ override <br/> Public void ongetdrivingrouteresult (mkdrivingrouteresult result, int ierror) {<br/>}</P> <p>/** <br/> * poi search results (range search, city poi search, and peripheral search) <br/> * @ Param result search result <br/> * @ Param type return result type (11,12, 21: poi list 7: City list) <br/> * @ Param ierror No. (0 indicates correct return) <br/> */<br/> @ override <br/> Public void ongetpoiresult (mkpoiresult result, int type, int ierror) {<br/>}</P> <p>/** <br/> * search results of bus transfer routes <br/> * @ Param result result <br/> * @ Param ierror error code (0 indicates a correct response) <br/> */<br/> @ override <br/> Public void ongettransitrouteresult (mktransitrouteresult result, int ierror) {<br/>}</P> <p>/** <br/> * walking Route Search Result <br/> * @ Param result search result <br/> * @ Param ierror code (0 indicates a correct return) <br/> */<br/> @ override <br/> Public void ongetwalkingrouteresult (mkwalkingrouteresult result, int ierror) {<br/>}< br/>}
3) configuration in androidmanifest. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. liufeng. baidumap "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". queryaddressactivity "Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" androi D. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "4"/> </P> <p> <! -- Network access permission --> <br/> <uses-Permission Android: Name = "android. Permission. Internet"/> <br/> <! -- Permission for precise access --> <br/> <uses-Permission Android: Name = "android. Permission. access_fine_location"/> <br/> <! -- Permission to access the network status --> <br/> <uses-Permission Android: Name = "android. Permission. access_network_state"/> <br/> <! -- Access the WiFi network status permission --> <br/> <uses-Permission Android: Name = "android. Permission. access_wifi_state"/> <br/> <! -- Change the permission of the Wi-Fi network --> <br/> <uses-Permission Android: Name = "android. Permission. change_wifi_state"/> <br/> <! -- Read and write permissions on the memory card --> <br/> <uses-Permission Android: Name = "android. Permission. write_external_storage"/> <br/> <! -- Permission to read the phone status --> <br/> <uses-Permission Android: Name = "android. Permission. read_phone_state"/> <br/> </manifest>
4) running result and description
Shows the initial effect of a program running on the simulator. It can be seen that the map is not displayed, which is the same as we imagined during design. The other two input boxes also display the default longitude and latitude values.
After you click "address query", the page containing the query results is displayed, as shown in:
Note:The figure "Putuo Road, yunyan district, Guiyang City, Guizhou Province" shows the address information corresponding to the geographical coordinates (longitude: 106.720397, latitude: 26.597239) We want to query; at the same time, 10 Interest points (POI) near the address are displayed below the address information ), each point of interest contains "name", "Address", "latitude and longitude", "phone", "zip code", and "point of interest type" information.
Note:If the example in this article continues, mapview should be displayed, and the use of itemizedoverlay in conjunction with the 8th article "[008] Baidu map API (Android) the address information and interest points are marked on the map. I think the two aspects have already been explained in detail and provided examples. It is not difficult to achieve this. You can do it as soon as you read the article!