Android百度地图——路径规划(驾车、步行和公交线路搜索)

前段时间公司项目比较忙,百度地图这块拖了好久了,这篇续前面几篇有关百度地图的。好了废话不多说,今天要聊的是有关路径规划的,如何使用百度地图搜索驾车、步行和公交路线并标注在地图上。这篇是基于Android百度地图——搜索服务之周边检索,有什么不清楚的请查看前面的几篇博文。路径规划:从那里到那里的线路规划,比如:从上海市盛夏路益江路到陆家嘴的驾车、步行和公交路线。

一、从那里到那里:

        1、起始地点:

 // 上海市盛夏路益江路的GPS纬度经度值:121.637942,31.205889 
        MKPlanNode start = new MKPlanNode();
        start.pt = new GeoPoint((int) (31.205889 * 1E6), (int) (121.637942 * 1E6));

        2、目的地点:

 // 上海市陆家嘴的GPS纬度经度值: 121.509075,31.243319 
        MKPlanNode end = new MKPlanNode();
        end.pt = new GeoPoint( (int) (31.243319 * 1E6), (int) (121.509075 * 1E6));

        注:获取地点的GPS值可以到http://api.map.baidu.com/lbsapi/getpoint/index.html查询,要注意拿到的值的顺序获取到的值是经纬度(也就是说第一个表示的是经度值,第二个表示的是纬度值)。而我们在地图上查找或标注时使用的顺序是GPS纬度经度值,因此记得调整顺序,不然在百度地图上就显示不出来。

二、如何到达,怎样到达:

        1、驾车线路:

            驾乘检索策略常量:时间优先

mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
        mMKSearch.drivingSearch(null, start, null, end);

            驾乘检索策略常量:较少费用

mMKSearch.setDrivingPolicy(MKSearch.ECAR_FEE_FIRST);
        mMKSearch.drivingSearch(null, start, null, end);

            驾乘检索策略常量:最短距离

mMKSearch.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST);
        mMKSearch.drivingSearch(null, start, null, end);

获取结果并展示时,需要实现MKSearchListener接口中的onGetDrivingRouteResult方法 :

    @Override
            public void onGetDrivingRouteResult(MKDrivingRouteResult result, int arg1) {
                if (result == null)
                    return;
                
                RouteOverlay routeOverlay = new RouteOverlay(BaiduMapRouteOverlayActivity.this, mMapView);   
                
                routeOverlay.setData(result.getPlan(0).getRoute(0)); 
                mMapView.getOverlays().add(routeOverlay);
                mMapView.refresh();//刷新地图
                
            }

        2、步行线路:

 // 步行线路搜索
        mMKSearch.walkingSearch(null, start, null, end);


获取结果并展示时,需要实现MKSearchListener接口中的onGetWalkingRouteResult方法

 @Override
            public void onGetWalkingRouteResult(MKWalkingRouteResult result, int arg1) {
                // TODO Auto-generated method stub
            	 RouteOverlay routeOverlay = new RouteOverlay(BaiduMapRouteOverlayActivity.this, mMapView);   
                 routeOverlay.setData(result.getPlan(0).getRoute(0)); 
                 mMapView.getOverlays().add(routeOverlay);
                 mMapView.refresh();//刷新地图
            }

 

        3、公交线路:

公交线路搜索的方法为transitSearch(String city, MKPlanNode start, MKPlanNode end),city:为待查公交线路所在城市,start和end分别是起点和终点;获取结果的方法改为重写onGetTransitRouteResult方法。

      // 公交线路搜索
        mMKSearch.transitSearch("上海市", start, end);

获取结果:

  @Override
            public void onGetTransitRouteResult(MKTransitRouteResult result, int arg1) {
            	 RouteOverlay routeOverlay = new RouteOverlay(BaiduMapRouteOverlayActivity.this, mMapView);   
                 routeOverlay.setData(result.getPlan(0).getRoute(0)); 
                 mMapView.getOverlays().add(routeOverlay);
                 mMapView.refresh();//刷新地图
                 
            }

四、完整代码:

package com.android.baidu.map;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.RouteOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPlanNode;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;

public class BaiduMapRouteOverlayActivity extends Activity {

	public static final String BAIDU_MAP_KEY = "07418AEC69BAAB7104C6230A6120C580DFFAEEBB";

	private BMapManager mMapManager = null;

	private MapView mMapView = null;

	private MKSearch mMKSearch = null;

	private Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mContext = BaiduMapRouteOverlayActivity.this.getApplicationContext();

		mMapManager = new BMapManager(getApplicationContext());
		mMapManager.init(BAIDU_MAP_KEY, new MKGeneralListener() {

			@Override
			public void onGetNetworkState(int errorCode) {
				if (errorCode == MKEvent.ERROR_NETWORK_CONNECT) {
					Toast.makeText(mContext, "您的网络出错啦!", Toast.LENGTH_LONG)
							.show();
				}
			}

			@Override
			public void onGetPermissionState(int errorCode) {
				if (errorCode == MKEvent.ERROR_PERMISSION_DENIED) {
					// 授权Key错误:
					Toast.makeText(mContext,
							"请在 DemoApplication.java文件输入正确的授权Key!",
							Toast.LENGTH_LONG).show();
				}
			}
		});

		setContentView(R.layout.main);

		mMapView = (MapView) this.findViewById(R.id.bmapsView);
		mMapView.setBuiltInZoomControls(true);

		MapController mMapController = mMapView.getController();
		// 上海市的GPS纬度经度值:31.243319,121.509075
		GeoPoint geoPoint = new GeoPoint((int) (31.243319 * 1E6),
				(int) (121.509075 * 1E6));
		mMapController.setCenter(geoPoint);
		mMapController.setZoom(12);

		// 检索从上海市盛夏路益江路到陆家嘴的驾车路线

		// 上海市盛夏路益江路的GPS纬度经度值:121.637942,31.205889
		MKPlanNode start = new MKPlanNode();
		start.pt = new GeoPoint((int) (31.205889 * 1E6),
				(int) (121.637942 * 1E6));

		// 上海市陆家嘴的GPS纬度经度值: 121.509075,31.243319
		MKPlanNode end = new MKPlanNode();
		end.pt = new GeoPoint((int) (31.243319 * 1E6), (int) (121.509075 * 1E6));

		mMKSearch = new MKSearch();
		mMKSearch.init(mMapManager, new MKSearchListener() {

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

			}

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

			}

			@Override
			public void onGetDrivingRouteResult(MKDrivingRouteResult result,
					int arg1) {
				if (result == null)
					return;

				RouteOverlay routeOverlay = new RouteOverlay(
						BaiduMapRouteOverlayActivity.this, mMapView);
				routeOverlay.setData(result.getPlan(0).getRoute(0));
				mMapView.getOverlays().add(routeOverlay);
				mMapView.refresh();// 刷新地图

			}

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

			}

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

			}

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

			}

			@Override
			public void onGetTransitRouteResult(MKTransitRouteResult result,
					int arg1) {
				RouteOverlay routeOverlay = new RouteOverlay(
						BaiduMapRouteOverlayActivity.this, mMapView);
				routeOverlay.setData(result.getPlan(0).getRoute(0));
				mMapView.getOverlays().add(routeOverlay);
				mMapView.refresh();// 刷新地图

			}

			@Override
			public void onGetWalkingRouteResult(MKWalkingRouteResult result,
					int arg1) {
				// TODO Auto-generated method stub
				RouteOverlay routeOverlay = new RouteOverlay(
						BaiduMapRouteOverlayActivity.this, mMapView);
				routeOverlay.setData(result.getPlan(0).getRoute(0));
				mMapView.getOverlays().add(routeOverlay);
				mMapView.refresh();// 刷新地图
			}

		});

		// 设置驾车路线搜索策略,时间优先、费用最少或距离最短

		/*
		 * // 驾乘检索策略常量:时间优先
		 * mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
		 * mMKSearch.drivingSearch(null, start, null, end);
		 * 
		 * // 驾乘检索策略常量:较少费用 mMKSearch.setDrivingPolicy(MKSearch.ECAR_FEE_FIRST);
		 * mMKSearch.drivingSearch(null, start, null, end);
		 * 
		 * // 驾乘检索策略常量:最短距离 mMKSearch.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST);
		 * mMKSearch.drivingSearch(null, start, null, end);
		 */

		// 步行线路搜索
		mMKSearch.walkingSearch(null, start, null, end);

		// 公交线路搜索
		// mMKSearch.transitSearch("上海市", start, end);

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mMapView.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mMapView.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onResume() {
		mMapView.onResume();

		if (mMapManager != null) {
			mMapManager.start();
		}

		super.onResume();
	}

	@Override
	protected void onPause() {
		mMapView.onPause();
		if (mMapManager != null) {
			mMapManager.stop();
		}
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		mMapView.destroy();
		if (mMapManager != null) {
			mMapManager.destroy();
			mMapManager = null;
		}
		super.onDestroy();
	}

}


 

你可能感兴趣的:(android,百度地图,路径规划,驾车步行和公交线路搜索)