Android百度地图开发(五)公交线路详情搜索、多条线路显示

一、公交线路详情检索

获取公交线路的详情主要分来两步,1.获取公交线路的Uid,2.通过Uid获取公交线路详情。

1.获取公交线路的Uid:

/*
         * 获得公交线路图的Uid,并且根据系Uid发起公交线路详情的检索
         * 
         * @see
         * com.baidu.mapapi.MKSearchListener#onGetPoiResult(com.baidu.mapapi
         * .MKPoiResult, int, int)
         */
        @Override
        public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub
            if (arg0 == null || arg2 != 0) {
                Toast.makeText(MainActivity.this, "未找到结果", 1);
                return;
            }
            // 定义一个公交线路描述类
            MKPoiInfo mkPoiInfo = null;
            // 获得本次就爱你所的公交线路的总站点
            int totalPoiNum = arg0.getNumPois();
            for (int i = 0; i < totalPoiNum; i++) {
                mkPoiInfo = arg0.getPoi(i);
                if (mkPoiInfo.ePoiType == 2) {
                    break;
                }
            }
            mkSearch.busLineSearch("开封", mkPoiInfo.uid);
        }

2.获取公交线路详情:

/*
         * 得到公交线路图的详情
         * 
         * @see
         * com.baidu.mapapi.MKSearchListener#onGetBusDetailResult(com.baidu.
         * mapapi.MKBusLineResult, int)
         */
        @Override
        public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
            // TODO Auto-generated method stub
            if (arg0 == null || arg1 != 0) {
                Toast.makeText(MainActivity.this, "未找到结果", 1);
                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());
        }

查询时,只需提供城市名和线路

mkSearch.poiSearchInCity("开封", "3");

完整代码:

package com.arlen.android.baidumap08;

import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

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;

public class MainActivity extends MapActivity {

    private BMapManager bMapManager;
    private MapView mapView;
    private String key = "43D220AAB0DEC976D2EABC83CFC9EEE52BCCCA7A";
    private MKSearch mkSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bMapManager = new BMapManager(MainActivity.this);
        bMapManager.init(key, new MKGeneralListener() {

            @Override
            public void onGetPermissionState(int arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onGetNetworkState(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        initMapActivity(bMapManager);
        mapView = (MapView) findViewById(R.id.bmapView);
        mkSearch = new MKSearch();
        mkSearch.init(bMapManager, new MySearchListener());

        mkSearch.poiSearchInCity("开封", "3");
    }

    private class MySearchListener implements MKSearchListener {

        @Override
        public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
            // TODO Auto-generated method stub

        }

        /*
         * 得到公交线路图的详情
         * 
         * @see
         * com.baidu.mapapi.MKSearchListener#onGetBusDetailResult(com.baidu.
         * mapapi.MKBusLineResult, int)
         */
        @Override
        public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
            // TODO Auto-generated method stub
            if (arg0 == null || arg1 != 0) {
                Toast.makeText(MainActivity.this, "未找到结果", 1);
                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

        }

        @Override
        public void onGetPoiDetailSearchResult(int arg0, int arg1) {
            // TODO Auto-generated method stub

        }

        /*
         * 获得公交线路图的Uid,并且根据系Uid发起公交线路详情的检索
         * 
         * @see
         * com.baidu.mapapi.MKSearchListener#onGetPoiResult(com.baidu.mapapi
         * .MKPoiResult, int, int)
         */
        @Override
        public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub
            if (arg0 == null || arg2 != 0) {
                Toast.makeText(MainActivity.this, "未找到结果", 1);
                return;
            }
            // 定义一个公交线路描述类
            MKPoiInfo mkPoiInfo = null;
            // 获得本次就爱你所的公交线路的总站点
            int totalPoiNum = arg0.getNumPois();
            for (int i = 0; i < totalPoiNum; i++) {
                mkPoiInfo = arg0.getPoi(i);
                if (mkPoiInfo.ePoiType == 2) {
                    break;
                }
            }
            mkSearch.busLineSearch("开封", mkPoiInfo.uid);
        }

        @Override
        public void onGetRGCShareUrlResult(String arg0, int arg1) {
            // TODO Auto-generated method stub

        }

        @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() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (bMapManager != null) {
            bMapManager.destroy();
            bMapManager = null;
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (bMapManager != null) {
            bMapManager.start();
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (bMapManager != null) {
            bMapManager.stop();
        }
    }

    @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;
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

运行效果:

二、多条线路显示

在同一个地图上显示多条导航线路,也只需要分两步完成,

1.定义起始点个终点

        start = new MKPlanNode();
        start.pt = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));
        end = new MKPlanNode();
        end.pt = new GeoPoint(40057031, 116307852);

        mkSearch.transitSearch("北京", start, end);

2.将所有的线路详情循环显示在地图上

        @Override
        public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
            // TODO Auto-generated method stub
            if (arg0 == null) {
                return;
            }
            int planNum = arg0.getNumPlan();// 获取所有的驾车路线
            for (int i = 0; i < planNum; i++) {
                TransitOverlay overlay = new TransitOverlay(MainActivity.this,
                        mapView);
                overlay.setData(arg0.getPlan(i));
                mapView.getOverlays().add(overlay);
            }
            mapView.invalidate();
            mapView.getController().animateTo(arg0.getStart().pt);
        }

运行效果:

posted @ 2013-11-12 23:36  若。只如初见  阅读(2892)  评论(0编辑  收藏  举报