at present, the Baidu Map SDK integrated search services include: POI search, bus information query, Route Planning , geocoding, online suggestion query, short string sharing.
The previous blog explains the POI search and online suggestion query, this blog will explain the frequently used Route planning .
Before you explain the code, go to the table:
It's all right! Now we have the code to implement the above function (the code has made the corresponding annotations)
Route planning search There are three kinds of search: driving, walking, bus! The steps for three implementations are basically similar, so let's take one to parse (the bus).
1. First we will instantiate an instance of the route plan search
Initialize the search module, register event listener Msearch = Routeplansearch.newinstance (); Msearch.setongetrouteplanresultlistener (this);
2.Create a bus route planning retrieval listener
@Overridepublic void Ongettransitrouteresult (transitrouteresult result) {if (result = = NULL | | Result.error! = SEARCHRESU Lt. Errorno.no_error) {Toast.maketext (mainactivity.this, "Sorry, no results found", Toast.length_short). Show (); if (Result.error = = SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {//end or pass-point address is ambiguous, the following interface to obtain the proposed query information// Result.getsuggestaddrinfo () return;} if (Result.error = = SearchResult.ERRORNO.NO_ERROR) {Nodeindex = -1;pre_next_id.setvisibility (view.visible); route = Result.getroutelines (). get (0); Transitrouteoverlay overlay = new Mytransitrouteoverlay (MBAIDUMAP); Mbaidumap.setonmarkerclicklistener (overlay); Routeoverlay = Overlay;overlay.setdata (Result.getroutelines (). Get (0)); Overlay.addtomap (); Overlay.zoomtospan ();}}
3. retrieving the starting and ending information
/** * Initiate route planning Search Example * * @param v */public void search_routeplan_process (View v) {//Reset the route data for the browse node route = Null;pre_next_id.s Etvisibility (view.invisible); Mbaidumap.clear ();//Process Search button response EditText editst = (EditText) Findviewbyid (R.id.start); EditText Editen = (EditText) Findviewbyid (r.id.end);//Set the end message, for Tranist search, the city name is meaningless plannode Stnode = Plannode.withcitynameandplacename ("Beijing", Editst.gettext (). toString ()); Plannode Ennode = plannode.withcitynameandplacename ("Beijing", Editen.gettext (). toString ()), or//actual use, please set the correct setting for the start Point city if ( V.getid () = = r.id.drive) {Msearch.drivingsearch ((new Drivingrouteplanoption ()). from (Stnode). to (Ennode));} else if ( V.getid () = = R.id.transit) {msearch.transitsearch (New transitrouteplanoption ()). from (Stnode)-City ("Beijing"). to (Ennode ));} else if (v.getid () = = R.id.walk) {Msearch.walkingsearch ((new Walkingrouteplanoption ()). from (Stnode). to (Ennode));}}
4. Implementation of node browsing (that is, the previous and next stations)
/** * Node Browsing example * * @param v */public void Nodeclick (View v) {if (route = = NULL | | route.getallstep () = = null) {return;} if (Nodeindex = =-1 && v.getid () = = R.id.pre) {return;} Set the node index if (v.getid () = = R.id.next) {if (Nodeindex < Route.getallstep (). Size ()-1) {nodeindex++;} else {return;}} els E if (v.getid () = = R.id.pre) {if (Nodeindex > 0) {nodeindex--;} else {return;}} Gets the section result information latlng nodelocation = null; String Nodetitle = Null;object step = Route.getallstep (). get (Nodeindex); if (step instanceof Drivingrouteline.drivingstep) {nodelocation = ((drivingrouteline.drivingstep) step). Getentrace (). GetLocation (); Nodetitle = ((drivingrouteline.drivingstep) step). Getinstructions ();} else if (step instanceof walkingrouteline.walkingstep) {nodelocation = ((walkingrouteline.walkingstep) step). Getentrace (). GetLocation (); nodetitle = ((walkingrouteline.walkingstep) step). Getinstructions (); else if (step instanceof transitrouteline.transitstep) {nodelocation = ((transitrouteline.transitstEP) step). Getentrace (). GetLocation (); nodetitle = ((transitrouteline.transitstep) step). Getinstructions ();} if (nodelocation = = NULL | | nodetitle = = NULL) {return;} Move node to center Mbaidumap.setmapstatus (MAPSTATUSUPDATEFACTORY.NEWLATLNG (nodelocation));//Show popuppopuptext = new TextView (mainactivity.this);p Opuptext.setbackgroundresource (r.drawable.popup);p Opuptext.settextcolor ( 0xff000000);p Opuptext.settext (nodetitle); Mbaidumap.showinfowindow (New Infowindow (Popuptext, nodeLocation, 0));}
5. Get start and end coordinates
Private class Mytransitrouteoverlay extends Transitrouteoverlay {public mytransitrouteoverlay (Baidumap baidumap) { Super (BAIDUMAP);} @Overridepublic bitmapdescriptor Getstartmarker () {//Get start coordinates if (Usedefaulticon) {return Bitmapdescriptorfactory.fromresource (R.drawable.icon_st);} return null;} @Overridepublic bitmapdescriptor Getterminalmarker () {//Get end coordinates if (Usedefaulticon) {return Bitmapdescriptorfactory.fromresource (r.drawable.icon_en);} return null;}}
6.release the retrieval instance;
Msearch.destroy ();
All right! About the bus route search finished! Driving and walking and bus routes of similar, want to know can download Source view!
This blog everyone think there is any new ideas and suggestions can be commented in the comment area, thank you!
Source code
Android Baidu map SDK v3_3_0 (vi)---driving, walking, bus route search