Android Learning notes using Baidu Map to achieve route planning + public transport information retrieval

Source: Internet
Author: User

PS: Installed a deepin, feel really very tall on.

Learning content:

1. Public transport Information retrieval

2. Route planning

So much for the development of Baidu maps. The important part is the same. Originally intended to get a POI search even, but see these two aspects still can't help to dabble. In fact, the implementation of the pattern and poi search is not much different. As long as the data information to the Baidu Map server, We are ready to process the return data of the server.

1. Public transport Information retrieval

Bus information retrieval, as its name implies, is the search for buses and subways. The implementation of the process and POI search is no different, is usually a realistic example of the object, and then set the retrieved parameters and retrieve data monitoring. After setting up these two items, the retrieval request is started. Then wait for the server segment to finish processing the data information. Our client is ready to process the data information. (Com.baidu.mapapi.search.busline)

By using the above API, we can realize the public transport information retrieval.

Another point is that the bus route query is also based on POI search. After we have completed the POI search, we are able to search the bus route. Implementing a POI search is a very simple thing to do here. Just talk about some of the details of the callback function after the POI result query.

Ongetpoisearchresultlistener Poisearchresultlistener =NewOngetpoisearchresultlistener () {@Override Public voidOngetpoiresult (Poiresult poiresult) {if(Poiresult = =NULL|| Poiresult.error = = SearchResult.ERRORNO.RESULT_NOT_FOUND) {//No search results foundToast.maketext (buslinesearchactivity. This, "Results not Found", Toast.length_long). Show (); return; }            if(Poiresult.error = = SearchResult.ERRORNO.NO_ERROR) {//search results return normally//Traverse all poi to find a POI of type bus linebuslineidlist.clear ();  for(Poiinfo Poi:poiResult.getAllPoi ()) {if(Poi.type = =PoiInfo.POITYPE.BUS_LINE|| Poi.type = =PoiInfo.POITYPE.SUBWAY_LINE)                    {Buslineidlist.add (POI.UID);            }} searchbusline (); }} @Override Public voidOngetpoidetailresult (Poidetailresult arg0) {}};

The callback function returns a variety of poi, so we need to traverse the POI to find out which bus site we want. So here is a judging condition to use to save the bus information. This is the process of completing the judgment. You can make inquiries about the bus lines.

  if (Poi.type == | | poi.type = = PoiInfo.POITYPE.SUBWAY_LINE) {        buslineidlist.add (poi.uid);  }

The first is to instantiate the object. As with the POI search, the object is instantiated using Newinstance (). Then buslinesearch through the instantiation of the object to complete the bus information retrieval. By sending a request to the server and then getting the data information.

Buslinesearch = Buslinesearch.newinstance ();

Then there is the binding listener event. When the server finishes processing the data, it returns the processed data to the client as a callback function. The client then processes the data information. To obtain the data information that the service side wants.

Ongetbuslinesearchresultlistener Buslinesearchresultlistener =NewOngetbuslinesearchresultlistener () {@Override Public voidOngetbuslineresult (Buslineresult buslineresult) {if(Buslineresult.error! =SearchResult.ERRORNO.NO_ERROR) {Toast.maketext (buslinesearchactivity). This, "Sorry, no results found", Toast.length_short). Show (); } Else{bdmap.clear (); Buslineoverlay Overlay=NewMybuslineoverlay (BDMAP);//overlay for displaying the results of a bus detailOverlay.setdata (Buslineresult); Overlay.addtomap ();//Add overlay to the mapOverlay.zoomtospan ();//Zoom the map so that all overlay are within the right range of visionBdmap.setonmarkerclicklistener (overlay); //Bus Line nameToast.maketext (buslinesearchactivity. This, Buslineresult.getbuslinename (), Toast.length_short). Show (); }        }    };

The settings for listening are very simple. Buslineresult is used as a parameter to the callback function. We can then get to the bus line site information. At the same time, we can also process these site information. Sets the listener events for the site. The data information of the site is also displayed on the map via overlay. We just need to invoke the function inside the overlay. , you can show all the obtained results on the map.

classMybuslineoverlayextendsBuslineoverlay { PublicMybuslineoverlay (Baidumap arg0) {Super(arg0); }        /*** Site Click event*/@Override Public BooleanOnbusstationclick (intarg0) {            //Markeroptions options = (markeroptions) getoverlayoptions (). get (ARG0);//get the clicked markerBdmap.animatemapstatus (MAPSTATUSUPDATEFACTORY.NEWLATLNG (Options.getposition ()));//updates the data information on the map.            return true; }    }

at the same time we finally configure the relevant parameters of the query, Then send the request to complete the information query. The configuration of the parameter is done by buslinesearchoptions. The parameters of the bus information query need to be configured with two properties. One is the city parameter. The UID here represents an identifier (unique). It is not the number of the bus stop. We need to pay attention to this point. The following is a query that makes information after passing through the parameters .

Buslinesearch.searchbusline (new buslinesearchoption () city. UID (Buslineidlist.get (Buslineindex)) ));  

2. Route planning

Route planning is based on the designated route, and then set up the best route to provide users, so that users can get the fastest time from the point of departure to the destination, route planning is divided into three kinds of planning. Drive, transfer, walk three ways to plan.

The plan includes four strategies: avoiding congestion, shortest distance, less cost, and time priority. Four strategies are encapsulated in enumerations. This allows us to set up the strategy directly in the route plan. then send the request. Baidu Map server will be based on the policy we set up the corresponding processing.

Driving route Query Implementation

   /*** Drive Line Query dropdown menu Four properties: Avoid congestion, shortest distance, less cost, time priority*/    Private voidDrivingsearch (intindex) {drivingrouteplanoption drivingoption=Newdrivingrouteplanoption (); Drivingoption.policy (Drivingpolicy.values () [Drivingspinner.getselecteditemposition ()]);//set up a driving route strategy. Use a drop-down menu to get one of four strategies.Drivingoption.from (Plannode.withcitynameandplacename ("Zibo", Startplace));//set the starting pointDrivingoption.to (Plannode.withcitynameandplacename ("Zibo", Endplace));//Set End pointRouteplansearch.drivingsearch (drivingoption);//initiate driving route planning}

Simply say the internal function: from (Plannode) sets the starting point function. Passby (java.util.list<plannode>waypoints) Set the pass point function

Policy Drivingrouteplanoption.drivingpolicy set a driving route planning strategy. to (Plannode to) the location to reach

Set the starting point, pass the point, end of the time need to pass the Plannode object. One way to instantiate a Plannode object is to instantiate it by passing cities and place names. The other way is to instantiate the object by setting the latitude and longitude.

Transfer route query implementation

The transfer route is generally referred to as a subway or a bus. These are the things that need to be transferred.

   /*** Transfer route Enquiry*/    Private voidTransitsearch (intindex) {transitrouteplanoption transitoption=Newtransitrouteplanoption (); Transitoption.city ("Zibo");//set up a transfer route planning city, the city will be ignored in the endTransitoption.from (Plannode.withcitynameandplacename ("Zibo", Startplace)); Transitoption.to (Plannode.withcitynameandplacename (Zibo, Endplace)); Transitoption.policy (Transitpolicy.values () [Transitspinner.getselecteditemposition ()]);//Set transfer PolicyRouteplansearch.transitsearch (transitoption); }

Walking route query implementation:

   /**     * Walking route query      *     /privatevoid  Walksearch () {         New walkingrouteplanoption ();        Walkoption.from (Plannode.withcitynameandplacename ("Zibo", Startplace));        Walkoption.to (Plannode.withcitynameandplacename ("Zibo", Endplace));        Routeplansearch.walkingsearch (walkoption);    }

the way to set up is actually very simple, after setting up the correlation mode, it is necessary to set the related listener event for callback. The concrete implementation of the internal process I will not be implemented, in the end I would like to provide a demo download. You can download it for the relevant research.

    /*** Route planning result Callback*/Ongetrouteplanresultlistener Routeplanresultlistener=NewOngetrouteplanresultlistener () {/*** Walking route result Callback*/@Override Public voidOngetwalkingrouteresult (Walkingrouteresult walkingrouteresult) {}/*** Switch to Route result Callback*/@Override Public voidOngettransitrouteresult (Transitrouteresult transitrouteresult) {}/*** Driving route result callback query results may include multiple driving route scenarios*/@Override Public voidOngetdrivingrouteresult (Drivingrouteresult drivingrouteresult) {}};

Finally, the object is instantiated, and then the request is sent, and after the server finishes processing the data, the data can be returned by callback.

Finally put a Demo:http://files.cnblogs.com/files/rgogoing/bdmap_1.rar

Android Learning notes using Baidu Map to achieve route planning + public transport information retrieval

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.