Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 百度地圖之公交線路詳情搜索

百度地圖之公交線路詳情搜索

編輯:關於Android編程

 
package com.njupt.bmap_helloworld;  
  
import com.baidu.mapapi.BMapManager;  
import com.baidu.mapapi.GeoPoint;  
import com.baidu.mapapi.MKAddrInfo;  
import com.baidu.mapapi.MKBusLineResult;  
import com.baidu.mapapi.MKDrivingRouteResult;  
import com.baidu.mapapi.MKGeneralListener;  
import com.baidu.mapapi.MKPlanNode;  
import com.baidu.mapapi.MKPoiInfo;  
import com.baidu.mapapi.MKPoiResult;  
import com.baidu.mapapi.MKSearch;  
import com.baidu.mapapi.MKSearchListener;  
import com.baidu.mapapi.MKSuggestionResult;  
import com.baidu.mapapi.MKTransitRouteResult;  
import com.baidu.mapapi.MKWalkingRouteResult;  
import com.baidu.mapapi.MapActivity;  
import com.baidu.mapapi.MapController;  
import com.baidu.mapapi.MapView;  
import com.baidu.mapapi.PoiOverlay;  
import com.baidu.mapapi.RouteOverlay;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
import android.widget.Toast;  
  
public class MainActivity extends MapActivity {  
  
    private MapView mapView;//百度地圖的相關控件  
    private BMapManager bMapManager;//加載地圖的引擎  
    private String keyString = "04763B1D8C925195965E838C6E59DB767DAE4BA7";//百度地圖的key  
    private MapController mapController;//在百度地圖上添加一些控件,比如放大或縮小控件  
      
    private MKSearch mkSearch;  
    private MKPlanNode start;  
    private MKPlanNode end;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        mapView = (MapView) findViewById(R.id.bmapView);  
        bMapManager = new BMapManager(MainActivity.this);  
          
        //必須要加載key  
        bMapManager.init(keyString, new MKGeneralListener() {  
              
            @Override  
            public void onGetPermissionState(int arg0) {  
                if(arg0 == 300){  
                    Toast.makeText(MainActivity.this, "輸入的key有誤,請核實...", 1).show();  
                }  
            }  
              
            @Override  
            public void onGetNetworkState(int arg0) {  
                // TODO Auto-generated method stub  
                  
            }  
        });  
          
        initMapActivity(bMapManager);  
        mapView.setBuiltInZoomControls(true);//表示可以設置縮放功能  
        mkSearch = new MKSearch();  
        mkSearch.init(bMapManager, new MySearchListener());  
        /** 
         * 根據中心點、半徑與檢索詞發起周邊檢索 
         * 第一個是查詢的名稱,第二個參數是構建一個GeoPoint點,第三個參數是查詢的半徑 
         */  
//      mkSearch.poiSearchNearBy("清華大學", new GeoPoint((int)(39.915*1E6), (int)(116.404*1E6)), 5000);  
          
        //第一個參數:城市名.;第二個參數:公交線路名  
        mkSearch.poiSearchInCity("北京", "300");  
    }  
  
    public class MySearchListener implements MKSearchListener{  
  
        @Override  
        public void onGetAddrResult(MKAddrInfo arg0, int arg1) {  
            // TODO Auto-generated method stub  
              
        }  
  
        //獲取公交線路的詳情  
        @Override  
        public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {  
            if(arg0 == null || arg1 != 0){  
                Toast.makeText(MainActivity.this, "對不起,未找到結果!", 0).show();  
                return ;  
            }  
              
            RouteOverlay routeOverlay = new RouteOverlay(MainActivity.this, mapView);  
            routeOverlay.setData(arg0.getBusRoute());//獲得公交線路圖  
            mapView.getOverlays().clear();//清楚公交站點的標識  
            mapView.getOverlays().add(routeOverlay);  
            mapView.invalidate();  
            //以動畫形式描述公交站點  
            mapView.getController().animateTo(arg0.getBusRoute().getStart());  
        }  
  
        @Override  
        public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {  
            // TODO Auto-generated method stub  
              
        }  
  
        //獲取公交線路的UID,並根據此UID來發起公交線路詳情的檢索  
        @Override  
        public void onGetPoiResult(MKPoiResult result, int type, int iError) {  
            if(result == null || iError != 0){  
                return ;  
            }  
              
            //定義一個公交線路描述類  
            MKPoiInfo mkPoiInfo = null;  
            int totalPoiNum = result.getNumPois();//獲取本地檢索公交線路的總站點數  
            for(int i = 0 ; i < totalPoiNum ; ++i){  
                mkPoiInfo = result.getPoi(i);  
                if(mkPoiInfo.ePoiType == 2){//如果該站點是公交站點  
                    break;  
                }  
            }  
              
            mkSearch.busLineSearch("北京", mkPoiInfo.uid);  
        }  
  
        @Override  
        public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {  
            // TODO Auto-generated method stub  
              
        }  
  
        @Override  
        public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {  
            // TODO Auto-generated method stub  
              
        }  
  
        @Override  
        public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {  
            // TODO Auto-generated method stub  
              
        }  
          
    }  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
          
        if(bMapManager != null){  
            bMapManager.destroy();  
            bMapManager = null;  
        }  
    }  
      
    @Override  
    protected void onResume() {  
        super.onResume();  
          
        if(bMapManager != null){  
            bMapManager.start();  
        }  
    }  
      
    @Override  
    protected void onPause() {  
        super.onPause();  
          
        if(bMapManager != null){  
            bMapManager.stop();  
        }  
    }  
    @Override  
    protected boolean isRouteDisplayed() {  
        // TODO Auto-generated method stub  
        return false;  
    }  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);  
        return true;  
    }  
  
}  

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved