首页 > 代码库 > 百度地图3.2教程(2)公交查询

百度地图3.2教程(2)公交查询

上一篇地址

百度地图3.1教程—检索功能演示

还记得两天前 我在找公交接口,非常不幸。接口有些查不到,几年的前的数据,哎 。算了呗,突然想起来了百度地图有这个功能,结果去看了API,尼玛这API 改的我都不认识  源代码在最小面

说好的MK开头的数据呢 去哪了,仅仅有从新看了。想从站点找点数据看看结果,找不到了,都是曾经API,好吧 自己写一篇 给大家补全吧。 

 

至于Demo配置我就不说了 。请參考上一篇文章.......


由于仅仅是做研究用的。所以UI做的不是非常美观 仅仅是把数据放到上面去了。 UI例如以下

技术分享

这个仅仅是教学教程所以 UI做的比較丑,只是这个代码你能够拷贝到你的APP里做成查询公交的功能,待完好。。。

。。

。。。。。。

一个button点击事件。联网请求数据,返回在Textview和listview显示


-----------第一步。发起POI检索。获取对应线路的UID;------------

先用poi检索查询 全部公交信息返回,这里是上一节就讲过的内容 我就不叙述了。

<pre name="code" class="java">	private void initMap() {
		poiSearch = PoiSearch.newInstance();
		poiSearch.setOnGetPoiSearchResultListener(onGetPoiSearchResultListener );
		//当公交信息出现以后,会出发这个监听器,这部分会在后面解说
		mBusLineSearch=BusLineSearch.newInstance();
		mBusLineSearch.setOnGetBusLineSearchResultListener(onGetBusLineSearchResultListener);
	}



 -----第二步,通过button的点击事件我们出发检索事件------

	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的点击事件处理,进行联网检索任务*/
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.button1:
			poiSearch.searchInCity((new PoiCitySearchOption())  
				    .city("深圳")  
				    .keyword("46")  
				    );
			
			break;

		default:
			break;
		}

	}

----------------第三步。通过联网 百度会返回一些公交数据。到回调接口 --------------------
           

//	--------------------------------POI检索-----------------------
	OnGetPoiSearchResultListener onGetPoiSearchResultListener  = new OnGetPoiSearchResultListener() {

		
		@Override
		public void onGetPoiResult(PoiResult result) {
			  if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {  
			        return;  
			    }  
			    //遍历全部POI。找到类型为公交线路的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("深圳")  
					    .uid(busLineId)));
			            break;  
			        } 
			    }  
			    
		 
		}
			//后面是具体信息
		@Override
		public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
			
		}
	};
//	--------------------------------POI检索-----------------------

------------------第四步 触发onGetBusLineSearchResultListener接口  从接口里的result里去取出我们要的数据

	//----------------------------bus检索-------------------------------------
	OnGetBusLineSearchResultListener onGetBusLineSearchResultListener=new OnGetBusLineSearchResultListener() {
		
		@Override
		public 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检索-------------------------------------
我就去了  最早时间 和最晚时间 还有 经过的站 其它的你们能够自行 扩展一下


	//----------------------------bus检索-------------------------------------
	OnGetBusLineSearchResultListener onGetBusLineSearchResultListener=new OnGetBusLineSearchResultListener() {
		
		@Override
		public 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检索-------------------------------------

java 所有代码

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.SDKInitializer;
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.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
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;

/**
 * 此demo用来展示怎样进行公交线路详情检索,并使用RouteOverlay在地图上绘制 同一时候展示怎样浏览路线节点并弹出泡泡
 */
public class BusLineSearchDemo extends Activity implements OnClickListener {
	private Button button1;
	private PoiSearch poiSearch;
	private BusLineSearch mBusLineSearch;
	private PoiCitySearchOption poiCitySearchOption;
	private TextView search_result;
	private ListView bus_station;
	private String busLineId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 注意该方法要再setContentView方法之前实现
		SDKInitializer.initialize(getApplicationContext());
		setContentView(R.layout.activity_busline);
		initMap();
		iniview();
	}

	private void initMap() {
		poiSearch = PoiSearch.newInstance();
		poiSearch.setOnGetPoiSearchResultListener(onGetPoiSearchResultListener );
		//当公交信息出现以后。会出发这个监听器,这部分会在后面解说
		mBusLineSearch=BusLineSearch.newInstance();
		mBusLineSearch.setOnGetBusLineSearchResultListener(onGetBusLineSearchResultListener);
	}

	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的点击事件处理,进行联网检索任务*/
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.button1:
			poiSearch.searchInCity((new PoiCitySearchOption())  
				    .city("深圳")  
				    .keyword("46")  
				    );
			
			break;

		default:
			break;
		}

	}
//	--------------------------------POI检索-----------------------
	OnGetPoiSearchResultListener onGetPoiSearchResultListener  = new OnGetPoiSearchResultListener() {

		
		@Override
		public void onGetPoiResult(PoiResult result) {
			  if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {  
			        return;  
			    }  
			    //遍历全部POI,找到类型为公交线路的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("深圳")  
					    .uid(busLineId)));
			            break;  
			        } 
			    }  
			    
		 
		}
			//后面是具体信息
		@Override
		public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
			
		}
	};
//	--------------------------------POI检索-----------------------
	//----------------------------bus检索-------------------------------------
	OnGetBusLineSearchResultListener onGetBusLineSearchResultListener=new OnGetBusLineSearchResultListener() {
		
		@Override
		public 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检索-------------------------------------
/* 释放资源*/
	@Override
	protected void onDestroy() {
		super.onDestroy();
		poiSearch.destroy();
	}

}
------------------------------------------------------------------end-----------------------------------------------

源代码里面其它类 ,你就别看了。由于还在研究 以下那个功能留下来的...........................

点击下载源代码

百度地图3.2教程(2)公交查询