歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

百度地圖-獲取一條公交上的所有站點 獲取兩站直接的線路

百度地圖-獲取一條公交上的所有站點 獲取兩站直接的線路:

  1. mSearch = new MKSearch();// 搜索服務類   
  2.         mSearch.init(mapManager, new MKSearchListener() {  
  3.   
  4.             public void onGetPoiResult(MKPoiResult res, int type, int error) {  
  5.                 // 錯誤號可參考MKEvent中的定義   
  6.                 if (error != 0 || res == null) {  
  7.                     Toast.makeText(BusRouteSysActivity.this"抱歉,未找到結果",  
  8.                             Toast.LENGTH_LONG).show();  
  9.                     return;  
  10.                 }  
  11.   
  12.                 String allStations = null;  
  13.   
  14.                 MKPoiInfo curPoi = null;  
  15.                 int totalPoiNum = res.getNumPois();  
  16.                 for (int idx = 0; idx < totalPoiNum; idx++) {  
  17.                     Log.d("busline""the busline is " + idx);  
  18.                     curPoi = res.getPoi(idx);  
  19.                     allStations += curPoi.name;//把所有站點放到allStation   
  20.   
  21.                     Log.i("allStation", allStations);  
  22.                     if (2 == curPoi.ePoiType) {  
  23.                         break;  
  24.                     }  
  25.                 }  
  26.   
  27.                 mSearch.busLineSearch(mCityName, curPoi.uid);  
  28.             }  
  29.   
  30.             public void onGetTransitRouteResult(MKTransitRouteResult result,  
  31.                     int iError) {  
  32.                 if (result == null) {  
  33.                     return;  
  34.                 }  
  35.                 Log.d("liuyq""公交換乘方案數:" + result.getNumPlan());  
  36.   
  37.                 TransitOverlay routeOverlay = new TransitOverlay(  
  38.                         BusRouteSysActivity.this, mapView);  
  39.                 // 此處僅展示一個方案作為示例   
  40.                 routeOverlay.setData(result.getPlan(0));  
  41.                 mapView.getOverlays().clear();  
  42.                 mapView.getOverlays().add(routeOverlay);  
  43.                 mapView.invalidate();  
  44.   
  45.                 ResStringList = new ArrayList<String>();  
  46.                 for (int i = 0; i < result.getNumPlan(); i++) {  
  47.                     // 公交換乘方案詳情類   
  48.                     MKTransitRoutePlan routePlan = result.getPlan(i);  
  49.                     // 再根據MKTransitRoutePlan類提供的方法去獲取具體的換乘信息(省略)   
  50.                     int lineNum = routePlan.getNumLines();  
  51.   
  52.                     Log.d("1111111""方案" + (i + 1) + "包含的公交線路段數:" + lineNum);  
  53.   
  54.                     StringBuffer reStringBuffer = new StringBuffer("");// 結果句子   
  55.                     String keyword = "";// 先 再 最後   
  56.                     for (int j = 0; j < lineNum; j++) {  
  57.                         MKLine mkLine = routePlan.getLine(j);  
  58.   
  59.                         MKPoiInfo onstopInfo = mkLine.getGetOnStop();  
  60.                         MKPoiInfo offstopInfo = mkLine.getGetOffStop();  
  61.   
  62.                         String lineName = mkLine.getTitle();// 線路名   
  63.                         String startPointName = onstopInfo.name;// 起點名   
  64.                         String endPointName = offstopInfo.name;// 終點名   
  65.                         String endword = "";  
  66.                         if (j == 0) {  
  67.                             keyword = "先";  
  68.                         } else if (j == lineNum - 1) {  
  69.                             keyword = ",最後";  
  70.                             endword = "\n\n";  
  71.                         } else {  
  72.                             keyword = ",再";  
  73.                         }  
  74.   
  75.                         reStringBuffer.append(keyword + "從 " + startPointName  
  76.                                 + " 上車,乘坐" + lineName + "路,在 " + endPointName  
  77.                                 + "下車" + endword);  
  78.   
  79.                         Log.d(TAG, "第" + (j + 1) + "段起點名稱:" + startPointName);  
  80.                         Log.d(TAG, "第" + (j + 1) + "段終點名稱:" + endPointName);  
  81.                     }  
  82.                     // 如果不重復 就加入   
  83.                     if (!ResStringList.contains(reStringBuffer.toString())) {  
  84.                         ResStringList.add(reStringBuffer.toString());  
  85.                     }  
  86.                 }  
  87.                 allResults = "";  
  88.                 for (String s : ResStringList) {  
  89.                     System.out.println(s);  
  90.                     allResults += s;  
  91.                 }  
  92.                 mapView.getController().animateTo(result.getStart().pt);  
  93.             }  
  94.   
  95.             public void onGetDrivingRouteResult(MKDrivingRouteResult res,  
  96.                     int error) {  
  97.             }  
  98.   
  99.             public void onGetWalkingRouteResult(MKWalkingRouteResult res,  
  100.                     int error) {  
  101.             }  
  102.   
  103.             public void onGetBusDetailResult(MKBusLineResult result, int iError) {  
  104.                 if (iError != 0 || result == null) {  
  105.                     Toast.makeText(BusRouteSysActivity.this"抱歉,未找到結果",  
  106.                             Toast.LENGTH_LONG).show();  
  107.                     return;  
  108.                 }  
  109.                 allResults = "";  
  110.                 for (int i = 0; i < result.getBusRoute().getNumSteps(); i++) {  
  111.   
  112.                     allResults += result.getStation(i).getContent() + "-->";  
  113.                     textViewTop.setText(allResults);  
  114.                 }  
  115.   
  116.                 RouteOverlay routeOverlay = new RouteOverlay(  
  117.                         BusRouteSysActivity.this, mapView);  
  118.                 // 此處僅展示一個方案作為示例   
  119.                 routeOverlay.setData(result.getBusRoute());  
  120.                 mapView.getOverlays().clear();  
  121.                 mapView.getOverlays().add(routeOverlay);  
  122.                 mapView.invalidate();  
  123.                 mapView.getController().animateTo(  
  124.                         result.getBusRoute().getStart());  
  125.             }  
  126.   
  127.             @Override  
  128.             public void onGetAddrResult(MKAddrInfo arg0, int arg1) {  
  129.                 // TODO Auto-generated method stub   
  130.   
  131.             }  
  132.   
  133.         });  

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved