百度地图之八查询某一路公交路线详细搜索

公交路线详细搜索

如何检索某一城市的公交路线图

实战案例:检索北京市公交路线717的路线

主要代码:

import com.baidu.mapapi.BMapManager;

import com.baidu.mapapi.MKAddrInfo;

import com.baidu.mapapi.MKBusLineResult;

import com.baidu.mapapi.MKDrivingRouteResult;

import com.baidu.mapapi.MKGeneralListener;

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.MapView;

import com.baidu.mapapi.RouteOverlay;

import com.baidu.mapapi.TransitOverlay;



import android.os.Bundle;

import android.widget.Toast;



public class MainActivity extends MapActivity {



    // 添加百度地图的相关控件

    private MapView mapView;

    // 加载地图的引擎

    private BMapManager bMapManager;

    // 百度地图的key

    private String KeyString = "8C426377B3526B6BF3672C9BA3BB544D1E8289CD";

    // 用于位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索等

    private MKSearch mkSearch;



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mapView = (MapView) this.findViewById(R.id.bmapView);

        bMapManager = new BMapManager(MainActivity.this);

        // 必须要加载key

        bMapManager.init(KeyString, new MKGeneralListener() {



            public void onGetPermissionState(int arg0) {

                

            }



            public void onGetNetworkState(int arg0) {

                if (arg0 == 300) {

                    Toast.makeText(MainActivity.this, "输入的key有错,请核实!!", 1).show();

                }

            }

        });

        this.initMapActivity(bMapManager);

        //实例化地图的查询类

        mkSearch = new MKSearch();

        mkSearch.init(bMapManager, new MySearchListener());

        //城市名,关键字

        mkSearch.poiSearchInCity("北京", "300");



    }



    public class MySearchListener implements MKSearchListener {



        public void onGetAddrResult(MKAddrInfo arg0, int arg1) {

            // TODO Auto-generated method stub



        }



        public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {

            // TODO Auto-generated method stub

            if(arg0==null||arg1!=0){

                Toast.makeText(MainActivity.this, "对不起,未找到结果!!", 1).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());

        }



        public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {

            // TODO Auto-generated method stub

            

        }



        /**

         * 获得公交线路图的Uid,并且根据此UID来发起公交线路详情的检索

         */

        public void onGetPoiResult(MKPoiResult result, int type, int iError) {

            // TODO Auto-generated method stub

            if(result==null||iError!=0){

                Toast.makeText(MainActivity.this, "对不起,未找到结果", 1).show();

                return;

            }

            //定义公交线路图的描述类

            MKPoiInfo mkPoiInfo = null;

            int totalPoiNum  = result.getNumPois();//获得本地检索的公交线路图的总站数

            for(int i = 0;i<totalPoiNum;i++){

                mkPoiInfo = result.getPoi(i);

                //2 公交线路

                if(mkPoiInfo.ePoiType==2){

                    break;

                }

            }

            mkSearch.busLineSearch("北京", mkPoiInfo.uid);

        }



        public void onGetRGCShareUrlResult(String arg0, int arg1) {

            // TODO Auto-generated method stub



        }



        public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {

            // TODO Auto-generated method stub



        }



        public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {

            // TODO Auto-generated method stub

            if(arg0==null){

                return;

            }

            TransitOverlay transiOverlay = new TransitOverlay(MainActivity.this,mapView);

            transiOverlay.setData(arg0.getPlan(0));

            mapView.getOverlays().add(transiOverlay);

            mapView.invalidate();

        }



        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() {

        return false;

    }



}

百度地图之八查询某一路公交路线详细搜索

你可能感兴趣的:(百度地图)