百度地图-获取一条公交上的所有站点 获取两站直接的线路


百度地图-获取一条公交上的所有站点 获取两站直接的线路:

  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.bkjia.com/topicnews.aspx?tid=11

相关内容