Important methods:
Public intGeocode(Java. Lang. String straddr, java. Lang. String City)
-
Obtain Address information based on address name
-
Asynchronous function, which returns the ongetaddrresult method notification of the result in mksearchlistener.
-
Parameters:
-
Straddr-address name
-
City-address City
-
Return Value:
0 is returned for success; otherwise-1 is returned.
Specific implementation:
Package xiaosi. baidumap; import android. app. alertdialog; import android. content. dialoginterface; import android. OS. bundle; import COM. baidu. mapapi. bmapmanager; import COM. baidu. mapapi. mkaddrinfo; import COM. baidu. mapapi. mkdrivingrouteresult; import COM. baidu. mapapi. mkpoiresult; import COM. baidu. mapapi. mksearch; import COM. baidu. mapapi. mksearchlistener; import COM. baidu. mapapi. mktransitrouteresult; import COM. Baidu. mapapi. mkwalkingrouteresult; import COM. baidu. mapapi. mapactivity; import COM. baidu. mapapi. mapcontroller; import COM. baidu. mapapi. mapview; public class baidumapactivity extends mapactivity {/** called when the activity is first created. */private bmapmanager mapmanager = NULL; private string key = "1b79478da01f7800aea8602517a6d89b38151105"; private mapview = NULL; private mksearch; PR Ivate mapcontroller = NULL; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {mapmanager = new bmapmanager (getapplication (); mapmanager. init (Key, null); super. initmapactivity (mapmanager); mapview = (mapview) findviewbyid (R. id. mapview); // enables the built-in zoom control mapview. setbuiltinzoomcontrols (true );// To get control of mmapview, you can use it to control and drive translation and zoom mapcontroller = mapview. getcontroller (); // sets the map zoom level mapcontroller. setzoom (12); mksearch = new mksearch (); // note that only one mksearchlistener is supported. The last setting prevails over mksearch. init (mapmanager, new mysearchlistener (); If (mksearch. geocode ("4th Square", "Qingdao") = 0) {system. out. println ("search successful");} else {system. out. println ("search failed");} public class mysearchlistener implements mksearchlistener {public void ongeta Ddrresult (mkaddrinfo arg0, int arg1) {/** returns the address information search result. Parameter: arg0-arg1-error no. In the search result, 0 indicates that the result is correct, and result indicates that the result is correct. 100 indicates that the result is correct and no relevant address information */string location = NULL; if (arg0 = NULL) {location = "this address is not found";} else {// obtain the latitude and longitude of the search address location = "latitude:" + arg0.geopt. getlatitudee6 ()/1e6 + "\ n" + "longitude:" + arg0.geopt. getlongitudee6 ()/1e6 + "\ n"; mapcontroller. animateto (arg0.geopt);} alertdialog. builder = new alertdialog. builder (baidumapactivity. this); builder. settitle ("search result "); Builder. setmessage (location); builder. setpositivebutton ("off", new android. content. dialoginterface. onclicklistener () {public void onclick (dialoginterface diich, int which) {dialog. dismiss () ;}}); builder. show ();} public void ongetdrivingrouteresult (mkdrivingrouteresult arg0, int arg1) {/** return the driving route search result. Parameter: arg0-arg1-error number of the search result. 0 indicates that the */} public void ongetpoiresult (mkpoiresult arg0, int arg1, int arg2) is returned. {/** the poi search result is returned. Parameter: arg0-search result arg1-return result type: mksearch. type_poi_list mksearch. type_area_poi_list mksearch. type_city_list arg2-error number. 0 indicates that the public void ongettransitrouteresult (mktransitrouteresult arg0, int arg1) is returned. Parameter: arg0-arg1-error number of the search result. 0 indicates that a correct result is returned. When mkevent is returned. error_route_addr indicates that the start point or end point is ambiguous. Call the getaddrresult method of mktransitrouteresult to obtain the recommended start point or end point information */} public void ongetwalkingrouteresult arg0, int arg1) {/** return the walking route search result. Parameter: arg0-arg1-error number of the search result. 0 indicates that the returned result is correct */}}@ overrideprotected Boolean isroutedisplayed () {return false;} @ overrideprotected void ondestroy () {If (mapmanager! = NULL) {mapmanager. Destroy (); mapmanager = NULL;} super. ondestroy () ;}@ overrideprotected void onpause () {If (mapmanager! = NULL) {mapmanager. Stop () ;}super. onpause () ;}@ overrideprotected void onresume () {If (mapmanager! = NULL) {mapmanager. Start () ;}super. onresume ();}}