百度地圖-獲取一條公交上的所有站點 獲取兩站直接的線路:
- mSearch = new MKSearch();// 搜索服務類
- mSearch.init(mapManager, new MKSearchListener() {
-
- public void onGetPoiResult(MKPoiResult res, int type, int error) {
- // 錯誤號可參考MKEvent中的定義
- if (error != 0 || res == null) {
- Toast.makeText(BusRouteSysActivity.this, "抱歉,未找到結果",
- Toast.LENGTH_LONG).show();
- return;
- }
-
- String allStations = null;
-
- MKPoiInfo curPoi = null;
- int totalPoiNum = res.getNumPois();
- for (int idx = 0; idx < totalPoiNum; idx++) {
- Log.d("busline", "the busline is " + idx);
- curPoi = res.getPoi(idx);
- allStations += curPoi.name;//把所有站點放到allStation
-
- Log.i("allStation", allStations);
- if (2 == curPoi.ePoiType) {
- break;
- }
- }
-
- mSearch.busLineSearch(mCityName, curPoi.uid);
- }
-
- public void onGetTransitRouteResult(MKTransitRouteResult result,
- int iError) {
- if (result == null) {
- return;
- }
- Log.d("liuyq", "公交換乘方案數:" + result.getNumPlan());
-
- TransitOverlay routeOverlay = new TransitOverlay(
- BusRouteSysActivity.this, mapView);
- // 此處僅展示一個方案作為示例
- routeOverlay.setData(result.getPlan(0));
- mapView.getOverlays().clear();
- mapView.getOverlays().add(routeOverlay);
- mapView.invalidate();
-
- ResStringList = new ArrayList<String>();
- for (int i = 0; i < result.getNumPlan(); i++) {
- // 公交換乘方案詳情類
- MKTransitRoutePlan routePlan = result.getPlan(i);
- // 再根據MKTransitRoutePlan類提供的方法去獲取具體的換乘信息(省略)
- int lineNum = routePlan.getNumLines();
-
- Log.d("1111111", "方案" + (i + 1) + "包含的公交線路段數:" + lineNum);
-
- StringBuffer reStringBuffer = new StringBuffer("");// 結果句子
- String keyword = "";// 先 再 最後
- for (int j = 0; j < lineNum; j++) {
- MKLine mkLine = routePlan.getLine(j);
-
- MKPoiInfo onstopInfo = mkLine.getGetOnStop();
- MKPoiInfo offstopInfo = mkLine.getGetOffStop();
-
- String lineName = mkLine.getTitle();// 線路名
- String startPointName = onstopInfo.name;// 起點名
- String endPointName = offstopInfo.name;// 終點名
- String endword = "";
- if (j == 0) {
- keyword = "先";
- } else if (j == lineNum - 1) {
- keyword = ",最後";
- endword = "\n\n";
- } else {
- keyword = ",再";
- }
-
- reStringBuffer.append(keyword + "從 " + startPointName
- + " 上車,乘坐" + lineName + "路,在 " + endPointName
- + "下車" + endword);
-
- Log.d(TAG, "第" + (j + 1) + "段起點名稱:" + startPointName);
- Log.d(TAG, "第" + (j + 1) + "段終點名稱:" + endPointName);
- }
- // 如果不重復 就加入
- if (!ResStringList.contains(reStringBuffer.toString())) {
- ResStringList.add(reStringBuffer.toString());
- }
- }
- allResults = "";
- for (String s : ResStringList) {
- System.out.println(s);
- allResults += s;
- }
- mapView.getController().animateTo(result.getStart().pt);
- }
-
- public void onGetDrivingRouteResult(MKDrivingRouteResult res,
- int error) {
- }
-
- public void onGetWalkingRouteResult(MKWalkingRouteResult res,
- int error) {
- }
-
- public void onGetBusDetailResult(MKBusLineResult result, int iError) {
- if (iError != 0 || result == null) {
- Toast.makeText(BusRouteSysActivity.this, "抱歉,未找到結果",
- Toast.LENGTH_LONG).show();
- return;
- }
- allResults = "";
- for (int i = 0; i < result.getBusRoute().getNumSteps(); i++) {
-
- allResults += result.getStation(i).getContent() + "-->";
- textViewTop.setText(allResults);
- }
-
- RouteOverlay routeOverlay = new RouteOverlay(
- BusRouteSysActivity.this, mapView);
- // 此處僅展示一個方案作為示例
- routeOverlay.setData(result.getBusRoute());
- mapView.getOverlays().clear();
- mapView.getOverlays().add(routeOverlay);
- mapView.invalidate();
- mapView.getController().animateTo(
- result.getBusRoute().getStart());
- }
-
- @Override
- public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
- // TODO Auto-generated method stub
-
- }
-
- });
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11