基于百度地图API的Android公交换乘导航

public class busRoute extends MapActivity {

	private Button mBtnTransit = null; // 公交搜索
	private ListView busList;// 公交列表


	private MapView mMapView = null; // 地图View
	private MKSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用
	private MapController mapController;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.busroute);

		BMapApiDemoApp app = (BMapApiDemoApp) this.getApplication();
		if (app.mBMapMan == null) {
			app.mBMapMan = new BMapManager(getApplication());
			app.mBMapMan.init(app.mStrKey,
					new BMapApiDemoApp.MyGeneralListener());
		}
		app.mBMapMan.start();
		// 如果使用地图SDK,请初始化地图Activity
		super.initMapActivity(app.mBMapMan);

		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.setBuiltInZoomControls(true);
		// 设置在缩放动画过程中也显示overlay,默认为不绘制
		mMapView.setDrawOverlayWhenZooming(true);

		GeoPoint geoPoint = new GeoPoint((int) (32.05000 * 1E6),
				(int) ( 118.78333* 1E6));

		// 取得地图控制器对象,用于控制MapView
		mapController = mMapView.getController();
		// 设置地图的中心
		mapController.setCenter(geoPoint);
		// 设置地图默认的缩放级别
		mapController.setZoom(12);

		// 初始化搜索模块,注册事件监听
		mSearch = new MKSearch();
		mSearch.init(app.mBMapMan, new MKSearchListener() {

			@Override
			public void onGetPoiDetailSearchResult(int type, int error) {
			}

			public void onGetDrivingRouteResult(MKDrivingRouteResult res,
					int error) {
				
			}

			public void onGetTransitRouteResult(MKTransitRouteResult res,
					int error) {
				if (error != 0 || res == null) {
					Toast.makeText(busRoute.this, "抱歉,未找到结果",
							Toast.LENGTH_SHORT).show();
					mMapView.setVisibility(View.VISIBLE);
					busList.setVisibility(View.GONE);
					return;
				}
				MyAdapter myAdapter = new MyAdapter(busRoute.this, res);
				busList.setAdapter(myAdapter);
			}

			public void onGetWalkingRouteResult(MKWalkingRouteResult res,
					int error) {
				
			}

			public void onGetAddrResult(MKAddrInfo res, int error) {
			}

			public void onGetPoiResult(MKPoiResult res, int arg1, int arg2) {

			}

			public void onGetBusDetailResult(MKBusLineResult result, int iError) {
			}

			@Override
			public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onGetRGCShareUrlResult(String arg0, int arg1) {
				// TODO Auto-generated method stub

			}
		});

		// 设定搜索按钮的响应
		busList = (ListView) findViewById(R.id.bus_list);
		busList.setCacheColorHint(0);
		mBtnTransit = (Button) findViewById(R.id.transit);

		OnClickListener clickListener = new OnClickListener() {
			public void onClick(View v) {
				mMapView.setVisibility(View.GONE);
				busList.setVisibility(View.VISIBLE);
				SearchButtonProcess(v);
			}
		};
		mBtnTransit.setOnClickListener(clickListener);
	}

	void SearchButtonProcess(View v) {
		// 处理搜索按钮响应
		EditText editSt = (EditText) findViewById(R.id.start);
		EditText editEn = (EditText) findViewById(R.id.end);

		// 对起点终点的name进行赋值,也可以直接对坐标赋值,赋值坐标则将根据坐标进行搜索
		MKPlanNode stNode = new MKPlanNode();
		stNode.name = editSt.getText().toString();
		MKPlanNode enNode = new MKPlanNode();
		enNode.name = editEn.getText().toString();

		// 实际使用中请对起点终点城市进行正确的设定
		if (mBtnTransit.equals(v)) {
			mSearch.transitSearch("南京", stNode, enNode);
		} 
	}

	@Override
	protected void onPause() {
		BMapApiDemoApp app = (BMapApiDemoApp) this.getApplication();
		app.mBMapMan.stop();
		super.onPause();
	}

	@Override
	protected void onResume() {
		BMapApiDemoApp app = (BMapApiDemoApp) this.getApplication();
		app.mBMapMan.start();
		super.onResume();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	// 填充公交列表
	class MyAdapter extends BaseAdapter {
		private MKTransitRouteResult res;
		private LayoutInflater mInflater;

		public MyAdapter(Context context, MKTransitRouteResult res) {
			this.res = res;
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return res.getNumPlan();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HolderView holder = null;
			if (convertView == null) {
				holder = new HolderView();
				convertView = mInflater.inflate(R.layout.list_item, null);
				holder.txt = (TextView) convertView
						.findViewById(R.id.list_item_txt);
				convertView.setTag(holder);
			} else {
				holder = (HolderView) convertView.getTag();
			}
			String lineInfo = "";
			// 得到解决方案
			MKTransitRoutePlan routePlan = res.getPlan(position);
			// 公交线路
			MKLine mkLine = routePlan.getLine(0);
			lineInfo += "乘坐:" + mkLine.getTitle();
			MKPoiInfo mkOnPoiInfo = mkLine.getGetOnStop();
			MKPoiInfo mkOffPoiInfo = mkLine.getGetOffStop();
			lineInfo += "\n\t\t\t\t从" + mkOnPoiInfo.name + "上车,在"
					+ mkOffPoiInfo.name + "下车";
			if (routePlan.getNumLines() > 0) {
				// 循环当前方案公交路线
				for (int i = 1; i < routePlan.getNumLines(); i++) {
					// 公交线路
					mkLine = routePlan.getLine(i);
					lineInfo += "\n换成:" + mkLine.getTitle();
					mkOnPoiInfo = mkLine.getGetOnStop();
					mkOffPoiInfo = mkLine.getGetOffStop();
					lineInfo += "\n\t\t\t\t从" + mkOnPoiInfo.name + "上车,在"
							+ mkOffPoiInfo.name + "下车";
				}
			}
			holder.txt.setText(lineInfo);
			convertView
					.setOnClickListener(new MyListViewOnClick(position, res));
			return convertView;
		}
	}

	class HolderView {
		public TextView txt;
	}

	class MyListViewOnClick implements OnClickListener {
		private int index;
		private MKTransitRouteResult res;

		public MyListViewOnClick(int index, MKTransitRouteResult res) {
			this.index = index;
			this.res = res;
		}

		@Override
		public void onClick(View arg0) {
			TransitOverlay routeOverlay = new TransitOverlay(busRoute.this,
					mMapView);
			routeOverlay.setData(res.getPlan(index));
			mMapView.getOverlays().clear();
			mMapView.getOverlays().add(routeOverlay);
			mMapView.invalidate();
			mMapView.getController().animateTo(res.getStart().pt);
			busList.setVisibility(View.GONE);
			mMapView.setVisibility(View.VISIBLE);
		}
	}
}

下面是鄙人的代码,感谢发现错误和提出意见:

基于百度地图API的公交换乘导航

DEMO APK下载:

busMapDeno.apk 

posted @ 2013-01-12 22:28  Hsiao的二进制人生  阅读(374)  评论(0编辑  收藏  举报