Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android百度地圖實戰:實現駕車、步行與公交的路徑規劃

編輯:Android 開發實例

前陣子公司項目比較繁忙,百度地圖這塊也就擱置了許久。本篇延續前面幾篇關於百度地圖的內容,廢話不多說,今天要聊的是路徑規劃——也就是如何使用百度地圖搜索駕車、步行和公交路線,並把它們標註在地圖上。本篇基於《Android百度地圖搜索服務之周邊檢索使用示例》,若有不清楚之處,歡迎回頭查看前面的幾篇博文。所謂路徑規劃,就是「從哪裡到哪裡」的線路規劃,例如:從上海市盛夏路益江路到陸家嘴的駕車、步行與公交路線。

一、從哪裡到哪裡

1、起始地點:

  1. // 上海市盛夏路益江路的GPS緯度經度值:121.637942,31.205889  
  2.         MKPlanNode start = new MKPlanNode();  
  3.         start.pt = new GeoPoint((int) (31.205889 * 1E6), (int) (121.637942 * 1E6)); 

2、目的地點:

  1. // 上海市陸家嘴的GPS緯度經度值: 121.509075,31.243319  
  2.         MKPlanNode end = new MKPlanNode();  
  3.         end.pt = new GeoPoint( (int) (31.243319 * 1E6), (int) (121.509075 * 1E6)); 

註:想獲取地點的GPS值,可以到https://api.map.baidu.com/lbsapi/getpoint/index.html查詢。要特別留意拿到的數值順序——查詢結果是「經度、緯度」(也就是說第一個是經度值,第二個是緯度值),而我們在地圖上查找或標註時使用的卻是「GPS緯度、經度值」,因此記得調整順序,否則在百度地圖上是顯示不出來的。

二、如何到達、怎樣到達

1、駕車線路:

駕乘檢索策略常量:時間優先

  1. mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);  
  2.  
  3.         mMKSearch.drivingSearch(null, start, null, end);  

駕乘檢索策略常量:較少費用

  1. mMKSearch.setDrivingPolicy(MKSearch.ECAR_FEE_FIRST);  
  2.  
  3.         mMKSearch.drivingSearch(null, start, null, end);  

駕乘檢索策略常量:最短距離

  1. mMKSearch.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST);  
  2.  
  3.         mMKSearch.drivingSearch(null, start, null, end);  

獲取結果並展示時,需要實現MKSearchListener接口中的onGetDrivingRouteResult方法:

  1. @Override 
  2.         public void onGetDrivingRouteResult(MKDrivingRouteResult result, int arg1) {  
  3.             if (result == null) 
  4.                 return; 
  5.            
  6.            
  7.            RouteOverlay routeOverlay = new RouteOverlay(BaiduMapRouteOverlayActivity.this, mMapView); 
  8.         
  9.            routeOverlay.setData(result.getPlan(0).getRoute(0)); 
  10.            mMapView.getOverlays().add(routeOverlay);  
  11.            mMapView.refresh();//刷新地圖  
  12.         
  13.       }  

2、步行線路:

  1. // 步行線路搜索  
  2.        mMKSearch.walkingSearch(null, start, null, end);  

獲取結果並展示時,需要實現MKSearchListener接口中的onGetWalkingRouteResult方法

  1. @Override 
  2.         public void onGetWalkingRouteResult(MKWalkingRouteResult result, int arg1) {  
  3.             // TODO Auto-generated method stub  
  4.             RouteOverlay routeOverlay = new RouteOverlay(BaiduMapRouteOverlayActivity.this, mMapView); 
  5.             routeOverlay.setData(result.getPlan(0).getRoute(0)); 
  6.            mMapView.getOverlays().add(routeOverlay);  
  7.            mMapView.refresh();//刷新地圖  
  8.         }  

3、公交線路:

公交線路的搜索方法為transitSearch(String city, MKPlanNode start, MKPlanNode end),其中city是待查公交線路所在的城市,start與end分別是起點和終點;獲取結果時,改為重寫onGetTransitRouteResult方法。

  1. // 公交線路搜索  
  2.   mMKSearch.transitSearch("上海市", start, end);  

獲取結果:

  1. @Override 
  2.         public void onGetTransitRouteResult(MKTransitRouteResult result, int arg1) {  
  3.             RouteOverlay routeOverlay = new RouteOverlay(BaiduMapRouteOverlayActivity.this, mMapView); 
  4.             routeOverlay.setData(result.getPlan(0).getRoute(0)); 
  5.            mMapView.getOverlays().add(routeOverlay);  
  6.            mMapView.refresh();//刷新地圖  
  7.  
  8.         }  

四、完整代碼

  1. package com.android.baidu.map;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.res.Configuration;  
  6. import android.os.Bundle;  
  7. import android.widget.Toast;  
  8.  
  9. import com.baidu.mapapi.BMapManager;  
  10. import com.baidu.mapapi.MKGeneralListener;  
  11. import com.baidu.mapapi.map.MKEvent;  
  12. import com.baidu.mapapi.map.MapController;  
  13. import com.baidu.mapapi.map.MapView;  
  14. import com.baidu.mapapi.map.RouteOverlay;  
  15. import com.baidu.mapapi.search.MKAddrInfo;  
  16. import com.baidu.mapapi.search.MKBusLineResult;  
  17. import com.baidu.mapapi.search.MKDrivingRouteResult;  
  18. import com.baidu.mapapi.search.MKPlanNode;  
  19. import com.baidu.mapapi.search.MKPoiResult;  
  20. import com.baidu.mapapi.search.MKSearch;  
  21. import com.baidu.mapapi.search.MKSearchListener;  
  22. import com.baidu.mapapi.search.MKSuggestionResult;  
  23. import com.baidu.mapapi.search.MKTransitRouteResult;  
  24. import com.baidu.mapapi.search.MKWalkingRouteResult;  
  25. import com.baidu.platform.comapi.basestruct.GeoPoint;  
  26.  
  27. public class BaiduMapRouteOverlayActivity extends Activity {  
  28.  
  29.     public static final String BAIDU_MAP_KEY = "07418AEC69BAAB7104C6230A6120C580DFFAEEBB";  
  30.  
  31.     private BMapManager mMapManager = null;  
  32.  
  33.     private MapView mMapView = null;  
  34.  
  35.     private MKSearch mMKSearch = null;  
  36.  
  37.     private Context mContext;  
  38.  
  39.     
  40.     @Override 
  41.     public void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.  
  44.         mContext = BaiduMapRouteOverlayActivity.this.getApplicationContext();  
  45.  
  46.         mMapManager = new BMapManager(getApplicationContext());  
  47.         mMapManager.init(BAIDU_MAP_KEY, new MKGeneralListener() {  
  48.  
  49.             @Override 
  50.             public void onGetNetworkState(int errorCode) {  
  51.                 if (errorCode == MKEvent.ERROR_NETWORK_CONNECT) {  
  52.                     Toast.makeText(mContext, "您的網絡出錯啦!", Toast.LENGTH_LONG) 
  53.                          .show();  
  54.                 }  
  55.            }  
  56.  
  57.             @Override 
  58.             public void onGetPermissionState(int errorCode) {  
  59.                if (errorCode == MKEvent.ERROR_PERMISSION_DENIED) {  
  60.                     
  61.                     Toast.makeText(mContext, 
  62.                             "請在 DemoApplication.java文件輸入正確的授權Key! 
  63.                          Toast.LENGTH_LONG).show();  
  64.                }  
  65.            }  
  66.       });  
  67.  
  68.        setContentView(R.layout.main);  
  69.  
  70.        mMapView = (MapView) this.findViewById(R.id.bmapsView);  
  71.        mMapView.setBuiltInZoomControls(true);  
  72.  
  73.        MapController mMapController = mMapView.getController();  
  74.        
  75.        GeoPoint geoPoint = new GeoPoint((int) (31.243319 * 1E6),  
  76.                 (int) (121.509075 * 1E6));  
  77.        mMapController.setCenter(geoPoint);  
  78.        mMapController.setZoom(12);  
  79.  
  80.        
  81.  
  82.        
  83.        MKPlanNode start = new MKPlanNode();  
  84.        start.pt = new GeoPoint((int) (31.205889 * 1E6),  
  85.                 (int) (121.637942 * 1E6));  
  86.  
  87.        
  88.        MKPlanNode end = new MKPlanNode();  
  89.        end.pt = new GeoPoint((int) (31.243319 * 1E6), (int) (121.509075 * 1E6));  
  90.  
  91.        mMKSearch = new MKSearch();  
  92.        mMKSearch.init(mMapManager, new MKSearchListener() {  
  93.  
  94.             
  95.             @Override 
  96.             public void onGetAddrResult(MKAddrInfo arg0, int arg1) {  
  97.                 
  98.  
  99.            }  
  100.  
  101.             @Override 
  102.             public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {  
  103.                 
  104.  
  105.            }  
  106.  
  107.             @Override 
  108.             public void onGetDrivingRouteResult(MKDrivingRouteResult result, 
  109.                     int arg1) {  
  110.                 if (result == null) 
  111.                     return;  
  112.  
  113.                 RouteOverlay routeOverlay = new RouteOverlay(  
  114.                         BaiduMapRouteOverlayActivity.this, mMapView);  
  115.                 routeOverlay.setData(result.getPlan(0).getRoute(0));  
  116.                 mMapView.getOverlays().add(routeOverlay);  
  117.             mMapView.refresh(); 
  118.  
  119.             }  
  120.  
  121.             @Override 
  122.             public void onGetPoiDetailSearchResult(int arg0, int arg1) {  
  123.                 
  124.  
  125.            }  
  126.  
  127.             @Override 
  128.             public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {  
  129.                 
  130.  
  131.            }  
  132.  
  133.             @Override 
  134.             public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {  
  135.                 
  136.  
  137.            }  
  138.  
  139.             @Override 
  140.             public void onGetTransitRouteResult(MKTransitRouteResult result, 
  141.                     int arg1) {  
  142.             RouteOverlay routeOverlay = new RouteOverlay(  
  143.                     BaiduMapRouteOverlayActivity.this, mMapView);  
  144.             routeOverlay.setData(result.getPlan(0).getRoute(0));  
  145.            mMapView.getOverlays().add(routeOverlay);  
  146.            mMapView.refresh(); 
  147.  
  148.         }  
  149.  
  150.             @Override 
  151.             public void onGetWalkingRouteResult(MKWalkingRouteResult result, 
  152.                     int arg1) {  
  153.                 
  154.             RouteOverlay routeOverlay = new RouteOverlay(  
  155.                     BaiduMapRouteOverlayActivity.this, mMapView);  
  156.             routeOverlay.setData(result.getPlan(0).getRoute(0));  
  157.            mMapView.getOverlays().add(routeOverlay);  
  158.            mMapView.refresh(); 
  159.         }  
  160.  
  161.       });  
  162.  
  163.        
  164.  
  165.        
  166.        /*
  167.         * 
  168.         * mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);  
  169.         * mMKSearch.drivingSearch(null, start, null, end);  
  170.         *  
  171.         * mMKSearch.setDrivingPolicy(MKSearch.ECAR_FEE_FIRST);  
  172.         * mMKSearch.drivingSearch(null, start, null, end);  
  173.         *  
  174.         * mMKSearch.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST);  
  175.         * mMKSearch.drivingSearch(null, start, null, end);  
  176.         */  
  177.  
  178.        
  179.        mMKSearch.walkingSearch(null, start, null, end);  
  180.  
  181.        
  182.        
  183.     }  
  184.  
  185.     @Override 
  186.     public void onConfigurationChanged(Configuration newConfig) {  
  187.         super.onConfigurationChanged(newConfig);  
  188.     }  
  189.  
  190.     @Override 
  191.     protected void onSaveInstanceState(Bundle outState) {  
  192.         super.onSaveInstanceState(outState);  
  193.         mMapView.onSaveInstanceState(outState);  
  194.     }  
  195.  
  196.     
  197.     @Override 
  198.     protected void onRestoreInstanceState(Bundle savedInstanceState) {  
  199.         super.onRestoreInstanceState(savedInstanceState);  
  200.         mMapView.onRestoreInstanceState(savedInstanceState);  
  201.     }  
  202.  
  203.     @Override 
  204.     protected void onResume() {  
  205.         mMapView.onResume();  
  206.  
  207.         if (mMapManager != null) {  
  208.             mMapManager.start();  
  209.         }  
  210.  
  211.         super.onResume();  
  212.     }  
  213.  
  214.     @Override 
  215.     protected void onPause() {  
  216.         mMapView.onPause();  
  217.         if (mMapManager != null) {  
  218.             mMapManager.stop();  
  219.         }  
  220.  
  221.         super.onPause();  
  222.     }  
  223.  
  224.     @Override 
  225.     protected void onDestroy() {  
  226.         mMapView.destroy();  
  227.         if (mMapManager != null) {  
  228.             mMapManager.destroy();  
  229.             mMapManager = null;  
  230.         }  
  231.         super.onDestroy();  
  232.     }  
  233.  
  234. } 

 

轉自:https://blog.csdn.net/android_ls/article/details/8676771

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved