Baidu Map Development (five) of the bus information retrieval + route planning

Source: Internet
Author: User

Reprint please specify the source:

In the previous blog introduced the use of POI retrieval, this blog mainly introduces the public transport information retrieval and line planning content.

Public transport Information retrieval

In fact, the public transport information retrieval and POI retrieval, online recommendation retrieval is very similar, but also you need to retrieve the information sent to the Baidu Map server, and then resolved the results.

General steps:

1. Set the retrieval parameters

2. Add a search Result listener

3. Initiating a search

4. Parse the returned results

Open the package com.baidu.mapapi.search.busline


In the buslinesearchoption class, we see that one parameter is the city and the other is the UID of the public route.

At first, I thought the second parameter is the bus line number, when the code is written to query, always error. After careful review of the API, it was found that it was not the bus line number, but the UID. UID we have before the POI retrieval time, have seen, it is actually an ID, is I a unique identity.

So note: The second parameter is the UID when setting the query parameters for the public transport information retrieval.

Therefore, before carrying out the public transport information retrieval, we need to do poi retrieval first. Use the Searchincity () method to query the bus route number in each city:

City = Cityet.gettext (). ToString (); busline = Buslineet.gettext (). ToString ();p oisearch.searchincity ((New Poicitysearchoption ()). City (city) keyword (busline));
When you get the results of a POI search, PoiresultThe class has passed Getallpoi ()Gets the POI search results.
@Overridepublic void Ongetpoiresult (Poiresult poiresult) {if (Poiresult = = null| | poiresult.error = = SearchResult.ERRORNO.RESULT_NOT_FOUND) {//No results found Toast.maketext (buslinesearchactivity.this, "result not Found", Toast.length_long). Show (); return;} if (Poiresult.error = = SearchResult.ERRORNO.NO_ERROR) {//Search results return normally//Get results}}
If you remove the UID from the Poiinfo directly, you will find many. However, as we all know, a bus line number has a maximum of two lines (forward and reverse). So we need to filter out the UID of the bus route.

There is a Type field in the Poiinfo class:0: Normal point, 1:, Bus stop 2:, bus route 3:, subway station 4: Subway line

if (Poi.type = = poiinfo.poitype.bus_line| | poi.type = = PoiInfo.POITYPE.SUBWAY_LINE) {buslineidlist.add (poi.uid);}
The correct UID is added to a list by judging the type.
The UID can be combined with the city parameters to retrieve the bus line.

private void Searchbusline () {if (Buslineindex >= buslineidlist.size ()) {buslineindex = 0;} if (buslineindex >= 0 && Buslineindex < buslineidlist.size () && buslineidlist.size () > 0) {Boolea N flag = Buslinesearch.searchbusline ((New Buslinesearchoption (). City, UID (Buslineidlist.get (Buslineindex))); if (flag) {Toast.maketext (buslinesearchactivity.this, "Search succeeded ~", "+"). Show ();} else {Toast.maketext ( Buslinesearchactivity.this, "Search Failed ~", ". Show ();} buslineindex++;}}
/** * Bus information query result listener */ongetbuslinesearchresultlistener Buslinesearchresultlistener = new Ongetbuslinesearchresultlistener () {@Overridepublic void Ongetbuslineresult (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 = new Mybuslineoverlay (BDMAP);//Overlayoverlay.setdata (Buslineresult) for displaying the result of a bus detail; o Verlay.addtomap ();//Add overlay to the map Overlay.zoomtospan ();//zoom map, Make all overlay within the appropriate field of view Bdmap.setonmarkerclicklistener (overlay);//Bus line name Toast.maketext (Buslinesearchactivity.this, Buslineresult.getbuslinename (), Toast.length_short). Show ();}};

Buslineresult is used to store the results of the query, and it has two internal classes and several common functions:

We can get the information we want through these two internal classes, which is not written in the code.

We still add overlays to the map by using related classes (Buslineoverlay) under the Overlayutil package.

Class Mybuslineoverlay extends Buslineoverlay {public mybuslineoverlay (Baidumap arg0) {super (arg0);} /** * Site Click event */@Overridepublic boolean onbusstationclick (int arg0) {markeroptions options = (markeroptions) getoverlayopt Ions (). get (arg0); Bdmap.animatemapstatus (MAPSTATUSUPDATEFACTORY.NEWLATLNG (Options.getposition ())); return true;}}


Route PlanningRoute planning is divided into three types: drive, change (bus), walk. and route planning is similar to the previous searches.

The route planning first class is under this package structure.


I will make a note of the following class in this package, so it is clear that three route planning method is equivalent to a POI retrieval, only when the parameter settings need attention.


There is a concept of strategy in the route planning. The driving route plan says there are four strategies: Dodge congestion, shortest distance, less cost, time first, and these four strategies form an enumeration type: Drivingrouteplanoption.drivingpolicy.


When you set a point, the passed in parameter is of type plannode . The class has two static methods, one can return a Plannode object by setting the city name and the place name, and by setting the latitude and longitude.

OK, so far, the route planning has no difficulty, the following only put out the driving route planning code, the rest can download demo view.

Drivingrouteplanoption drivingoption = new Drivingrouteplanoption ();d Rivingoption.policy (DrivingPolicy.values () [ Drivingspinner.getselecteditemposition ()]);//Set Driving route strategy Drivingoption.from (Plannode.withcitynameandplacename ("Beijing" , startplace));//Set the starting point drivingoption.to (Plannode.withcitynameandplacename ("Beijing", Endplace));// Set the end point Routeplansearch.drivingsearch (drivingoption);//Initiate Driving route planning

/** * Driving directions Results of callback queries may include multiple driving route scenarios */@Overridepublic void Ongetdrivingrouteresult (Drivingrouteresult drivingrouteresult {bdmap.clear (); if (Drivingrouteresult = = null| | Drivingrouteresult.error! = SearchResult.ERRORNO.NO_ERROR) { Toast.maketext (Routeplanningactivity.this, "Sorry, no results found", Toast.length_short). Show (); if (Drivingrouteresult.error = = SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {//end or pass-point address is ambiguous, the following interface to obtain the proposed query information// Drivingrouteresult.getsuggestaddrinfo () return;} if (Drivingrouteresult.error = = SearchResult.ERRORNO.NO_ERROR) {Drivingrouteoverlay Drivingrouteoverlay = new Drivingrouteoverlay (Bdmap);d Rivingrouteoverlay.setdata (Drivingrouteresult.getroutelines (). Get ( Drivintresultindex));//Set up a driving route scheme Bdmap.setonmarkerclicklistener (drivingrouteoverlay); Drivingrouteoverlay.addtomap ();d Rivingrouteoverlay.zoomtospan (); totalline = Drivingrouteresult.getroutelines (). Size (); Toast.maketext (routeplanningactivity.this, "total query out" + totalline + "lines Qualifying line", +). Show (); if (Totalline > 1) { Nextlinebtn.seteNabled (TRUE);} Through Gettaxiinfo () can get a lot of information about taxi toast.maketext (Routeplanningactivity.this, "The route taxi total distance" + Drivingrouteresult.gettaxiinfo (). Getdistance (), (). Show ();}};

Download

demo~~~


Baidu Map Development (five) of the bus information retrieval + route planning

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.