百度地图之九如何在一个地图上显示多条导航路线

如何在一个地图上显示多条导航路线

实战案例:检索北京天安门驾车到百度大厦有多少条路线图

package com.android.android_baidu_mystranit;

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.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.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;
    
    //定义一个起始点,终点
    private MKPlanNode start;
    private MKPlanNode end;

    @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());
        //给起始点构建一个坐标
        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);
        

    }

    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

        }

        public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
            // TODO Auto-generated method stub
        }

        public void onGetPoiResult(MKPoiResult result, int type, int iError) {
            // TODO Auto-generated method stub
        }

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

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

}

 

百度地图之九如何在一个地图上显示多条导航路线_第1张图片


你可能感兴趣的:(百度地图之九如何在一个地图上显示多条导航路线)