標籤:android 百度地圖 路線規劃
目前百度地圖SDK所整合的檢索服務包括:POI檢索、公交資訊查詢、線路規劃、地理編碼、線上建議查詢、短串分享。
上篇部落格講解了POI檢索和線上建議查詢,這篇部落格將講解經常用到的線路規劃。
在講解代碼之前先上張:
好了!現在我們上代碼,來實現上面的功能(代碼中都做了相應的註解)
路線規劃檢索有三種檢索:駕車,步行,公交車!三種實現的步驟基本類似,下面我們就拿一種來做解析(公交車)。
1.首先我們要執行個體化路線規劃檢索的執行個體
// 初始化搜尋模組,註冊事件監聽 mSearch = RoutePlanSearch.newInstance(); mSearch.setOnGetRoutePlanResultListener(this);
2. 建立公交線路規劃檢索監聽者
@Overridepublic void onGetTransitRouteResult(TransitRouteResult result) {if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {Toast.makeText(MainActivity.this, "抱歉,未找到結果", Toast.LENGTH_SHORT).show();}if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {// 起終點或途經點地址有岐義,通過以下介面擷取建議查詢資訊// result.getSuggestAddrInfo()return;}if (result.error == SearchResult.ERRORNO.NO_ERROR) {nodeIndex = -1;pre_next_id.setVisibility(View.VISIBLE);route = result.getRouteLines().get(0);TransitRouteOverlay overlay = new MyTransitRouteOverlay(mBaiduMap);mBaiduMap.setOnMarkerClickListener(overlay);routeOverlay = overlay;overlay.setData(result.getRouteLines().get(0));overlay.addToMap();overlay.zoomToSpan();}}
3.檢索起、終點資訊
/** * 發起路線規劃搜尋樣本 * * @param v */public void Search_RoutePlan_Process(View v) {// 重設瀏覽節點的路線資料route = null;pre_next_id.setVisibility(View.INVISIBLE);mBaiduMap.clear();// 處理搜尋按鈕響應EditText editSt = (EditText) findViewById(R.id.start);EditText editEn = (EditText) findViewById(R.id.end);// 設定起終點資訊,對於tranist search 來說,城市名無意義PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", editSt.getText().toString());PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", editEn.getText().toString());// 實際使用中請對起點終點城市進行正確的設定if (v.getId() == R.id.drive) {mSearch.drivingSearch((new DrivingRoutePlanOption()).from(stNode).to(enNode));} else if (v.getId() == R.id.transit) {mSearch.transitSearch((new TransitRoutePlanOption()).from(stNode).city("北京").to(enNode));} else if (v.getId() == R.id.walk) {mSearch.walkingSearch((new WalkingRoutePlanOption()).from(stNode).to(enNode));}}
4.節點瀏覽的實現(就是上一站和下一站)
/** * 節點瀏覽樣本 * * @param v */public void nodeClick(View v) {if (route == null || route.getAllStep() == null) {return;}if (nodeIndex == -1 && v.getId() == R.id.pre) {return;}// 設定節點索引if (v.getId() == R.id.next) {if (nodeIndex < route.getAllStep().size() - 1) {nodeIndex++;} else {return;}} else if (v.getId() == R.id.pre) {if (nodeIndex > 0) {nodeIndex--;} else {return;}}// 擷取節結果資訊LatLng nodeLocation = null;String nodeTitle = null;Object step = route.getAllStep().get(nodeIndex);if (step instanceof DrivingRouteLine.DrivingStep) {nodeLocation = ((DrivingRouteLine.DrivingStep) step).getEntrace().getLocation();nodeTitle = ((DrivingRouteLine.DrivingStep) step).getInstructions();} else if (step instanceof WalkingRouteLine.WalkingStep) {nodeLocation = ((WalkingRouteLine.WalkingStep) step).getEntrace().getLocation();nodeTitle = ((WalkingRouteLine.WalkingStep) step).getInstructions();} else if (step instanceof TransitRouteLine.TransitStep) {nodeLocation = ((TransitRouteLine.TransitStep) step).getEntrace().getLocation();nodeTitle = ((TransitRouteLine.TransitStep) step).getInstructions();}if (nodeLocation == null || nodeTitle == null) {return;}// 移動節點至中心mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(nodeLocation));// show popuppopupText = new TextView(MainActivity.this);popupText.setBackgroundResource(R.drawable.popup);popupText.setTextColor(0xFF000000);popupText.setText(nodeTitle);mBaiduMap.showInfoWindow(new InfoWindow(popupText, nodeLocation, 0));}
5.擷取開始和結束座標
private class MyTransitRouteOverlay extends TransitRouteOverlay {public MyTransitRouteOverlay(BaiduMap baiduMap) {super(baiduMap);}@Overridepublic BitmapDescriptor getStartMarker() {//擷取開始座標if (useDefaultIcon) {return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);}return null;}@Overridepublic BitmapDescriptor getTerminalMarker() {//擷取結束座標if (useDefaultIcon) {return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);}return null;}}
6.釋放檢索執行個體;
mSearch.destroy();
好了!關於公交車路線檢索講解完啦!駕車和步行的和公交車路線的差不多,想瞭解的可以下載源碼查看!
本篇部落格大家覺得有什麼新的想法和建議可以在評論區評論,謝謝!
源碼
Android 百度地圖 SDK v3_3_0 (六) ---駕車、步行、大眾運輸路線搜尋