Baidu map 3.2 tutorial (2) bus query, 3.2 tutorial

Source: Internet
Author: User

Baidu map 3.2 tutorial (2) bus query, 3.2 tutorial

Previous address

Baidu map 3.1 tutorial-search function demonstration

I still remember that I was looking for a bus interface two days ago. Unfortunately, some interfaces could not be found. The data from a few years ago, ah, forget it. I suddenly remembered that Baidu map had this function, I went to see the API. I didn't know the source code on the smallest side of Nima's API.

Where can I find the data starting with MK? I only need to read it again. I want to find some data from the website to see the results. I cannot find it. It's all from the previous APIs, let's write an article to help you complete it.

 

I won't talk about the Demo configuration. Please refer to the previous article .......


Because it is only used for research, what the UI does is not very beautiful. It just puts the data on it. The UI is as follows:

This is just a tutorial, so the UI is ugly, but you can copy this code to your APP to make it a function for querying public transit. It will be improved ...........

A button click event, online request data, returned in Textview and listview display


----------- Step 1: Initiate a POI search to obtain the UID of the corresponding line ;------------

First use poi to search and query all bus information. Here is what I have mentioned in the previous section.

<Pre name = "code" class = "java"> private void initMap () {earearch = poiSearch. newInstance (); receivearch. setOnGetPoiSearchResultListener (onGetPoiSearchResultListener); // when the bus information appears, this listener will be set up. This section will explain the following description: mBusLineSearch = BusLineSearch. newInstance (); mBusLineSearch. setOnGetBusLineSearchResultListener (onGetBusLineSearchResultListener );}


----- Step 2: Use the button click event to retrieve events ------ 

private void iniview() {button1 = (Button) findViewById(R.id.button1);search_result = (TextView) findViewById(R.id.result);bus_station = (ListView) findViewById(R.id.bus_station);button1.setOnClickListener(this);}


/* Button click event processing for online search tasks */@ Overridepublic void onClick (View view) {switch (view. getId () {case R. id. button1: Too earch. searchInCity (new PoiCitySearchOption ()). city ("Shenzhen "). keyword ("46"); break; default: break ;}}

---------------- Step 3, Baidu will return some bus data through the Internet, to the callback interface --------------------

// Retrieve the deleongetaskearchresultlistener ongettransferearchresultlistener () {@ Overridepublic void onGetPoiResult (PoiResult result) {if (result = null | result. error! = SearchResult. ERRORNO. NO_ERROR) {return;} // traverse all POI and find the POI for (PoiInfo poi: result. getAllPoi () {if (poi. type = PoiInfo. POITYPE. BUS_LINE | poi. type = PoiInfo. POITYPE. SUBWAY_LINE) {busLineId = poi. uid; mBusLineSearch. searchBusLine (new BusLineSearchOption (). city ("Shenzhen "). uid (busLineId); break ;}}// detailed information @ Overridepublic void onGetPoiDetailResult (PoiDetailResult poiDetailResult) {}; // retrieve POI retrieval -----------------------

------------------ Step 4 triggers the onGetBusLineSearchResultListener interface to retrieve the data we need from the result in the interface

// Retrieve OnGetBusLineSearchResultListener onGetBusLineSearchResultListener = new OnGetBusLineSearchResultListener () {@ Overridepublic void onGetBusLineResult (BusLineResult result) {Date startTime = result. getStartTime (); Date endTime = result. getEndTime (); String start = Commutils. dataToString (startTime); String end = Commutils. dataToString (endTime); String busLineName = result. getBusLineName (); search_result.setText (start + "--" + end + "\ n" + busLineName); List <BusStation> stations = result. getStations (); List <String> list = new ArrayList <String> (); for (int I = 0; I <stations. size (); I ++) {String title = stations. get (I ). getTitle (); list. add (title);} ArrayAdapter <String> adapter = new ArrayAdapter <String> (BusLineSearchDemo. this, android. r. layout. simple_list_item_1, list); bus_station.setAdapter (adapter) ;}}; // ---------------------------- bus retrieval -------------------------------------
I will go to the earliest time and the latest time, and other sites that have passed by, you can expand it on your own.


// Retrieve OnGetBusLineSearchResultListener onGetBusLineSearchResultListener = new OnGetBusLineSearchResultListener () {@ Overridepublic void onGetBusLineResult (BusLineResult result) {Date startTime = result. getStartTime (); Date endTime = result. getEndTime (); String start = Commutils. dataToString (startTime); String end = Commutils. dataToString (endTime); String busLineName = result. getBusLineName (); search_result.setText (start + "--" + end + "\ n" + busLineName); List <BusStation> stations = result. getStations (); List <String> list = new ArrayList <String> (); for (int I = 0; I <stations. size (); I ++) {String title = stations. get (I ). getTitle (); list. add (title);} ArrayAdapter <String> adapter = new ArrayAdapter <String> (BusLineSearchDemo. this, android. r. layout. simple_list_item_1, list); bus_station.setAdapter (adapter) ;}}; // ---------------------------- bus retrieval -------------------------------------

All java code

Package com. example. maptest; import java. util. arrayList; import java. util. date; import java. util. list; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. window; import android. view. view. onClickListener; import android. widget. arrayAdapter; import android. widget. button; import android. widget. listView; import android. widget. textView; import com. baidu. mapapi. SDKIni Tializer; import com. baidu. mapapi. search. busline. busLineResult; import com. baidu. mapapi. search. busline. busLineResult. busStation; import com. baidu. mapapi. search. busline. busLineSearch; import com. baidu. mapapi. search. busline. busLineSearchOption; import com. baidu. mapapi. search. busline. onGetBusLineSearchResultListener; import com. baidu. mapapi. search. core. poiInfo; import com. baidu. mapapi. search. core. searchResu Lt; import com. baidu. mapapi. search. poi. ongetaskearchresultlistener; import com. baidu. mapapi. search. poi. poiCitySearchOption; import com. baidu. mapapi. search. poi. poiDetailResult; import com. baidu. mapapi. search. poi. poiResult; import com. baidu. mapapi. search. poi. poiSearch;/*** this demo is used to show how to search bus line details, use RouteOverlay to draw on the map and show how to browse route nodes and pop up bubbles */public class BusLineSearchDemo extends Activity implements OnClickL Istener {private Button button1; private incluearch; private BusLineSearch mBusLineSearch; private PoiCitySearchOption poiCitySearchOption; private TextView search_result; private ListView bus_station; private String busLineId; @ incluvoid onCreate (Bundle release) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); // note that the method must be before the setContentView Method Implement SDKInitializer. initialize (getApplicationContext (); setContentView (R. layout. activity_busline); initMap (); iniview ();} private void initMap () {shortearch = shortearch. newInstance (); receivearch. setOnGetPoiSearchResultListener (onGetPoiSearchResultListener); // when the bus information appears, this listener will be set up. This section will explain the following description: mBusLineSearch = BusLineSearch. newInstance (); mBusLineSearch. setOnGetBusLineSearchResultListener (onGetBusLineSearchResultLis Tener);} private void iniview () {button1 = (Button) findViewById (R. id. button1); search_result = (TextView) findViewById (R. id. result); bus_station = (ListView) findViewById (R. id. bus_station); button1.setOnClickListener (this);}/* Button click event processing, online search task */@ Overridepublic void onClick (View view) {switch (view. getId () {case R. id. button1: Too earch. searchInCity (new PoiCitySearchOption ()). city ("Shenzhen "). keyword ("46"); break; default: break;} // descripoi retrieval response ongetaskearchresultlistener = new ongetincluearchresultlistener () {@ Overridepublic void onGetPoiResult (PoiResult result) {if (result = null | result. error! = SearchResult. ERRORNO. NO_ERROR) {return;} // traverse all POI and find the POI for (PoiInfo poi: result. getAllPoi () {if (poi. type = PoiInfo. POITYPE. BUS_LINE | poi. type = PoiInfo. POITYPE. SUBWAY_LINE) {busLineId = poi. uid; mBusLineSearch. searchBusLine (new BusLineSearchOption (). city ("Shenzhen "). uid (busLineId); break ;}}// detailed information @ Overridepublic void onGetPoiDetailResult (PoiDetailResult poiDetailResult ){}}; // mongopoi retrieval handler // mongobus retrieval javasongetbuslinesearchresultlistener OnGetBusLineSearchResultListener = new listener () {@ Overridepublic void onGetBusLineResult (BusLineResult result) {Date startTime = result. getStartTime (); Date endTime = result. getEndTime (); String start = Commutils. dataToString (startTime); String end = Commutils. dataToString (endTime); String busLineName = result. getBusLineName (); search_result.setText (start + "--" + end + "\ n" + busLineName); List <BusStation> stations = result. getStations (); List <String> list = new ArrayList <String> (); for (int I = 0; I <stations. size (); I ++) {String title = stations. get (I ). getTitle (); list. add (title);} ArrayAdapter <String> adapter = new ArrayAdapter <String> (BusLineSearchDemo. this, android. r. layout. simple_list_item_1, list); bus_station.setAdapter (adapter) ;}}; // -------------------------- bus retrieval quota/* release resources */@ Overrideprotected void onDestroy () {super. onDestroy (); cancearch. destroy ();}}
------------------------------------------------------------------ End -----------------------------------------------

Other classes in the source code, because we are still studying the function that remains behind ...........................

Click to download source code

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.