歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 百度地圖-獲取一條公交上的所有站點 獲取兩站直接的線路

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

日期:2017/3/1 10:22:20   编辑:Linux編程

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

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

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

Copyright © Linux教程網 All Rights Reserved