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();
}
} 來自:http://blog.csdn.net/android_ls/article/details/8676771