百度地图加强_搜索_路线检索_驾车路线
一、驾车路线 |
一、类public class MKSearch 搜索服务. RouteOverlay 用于位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索 1、public int drivingSearch ( java.lang.String startCity, MKPlanNode start, java.lang.String endCity, MKPlanNode end) 异步函数,返回结果在MKSearchListener里的onGetDrivingRouteResult方法通知
2、public int setTransitPolicy(int policy) //这里是设置步行和公交路线规划策略
3、public int setDrivingPolicy(int policy)//这里是设置驾车路线规划策略,不能用setTransitPolicy()
4、public int drivingSearch (java.lang.String startCity,MKPlanNode start, java.lang.String endCity,MKPlanNode end, java.util.List<MKWpNode> wpNodes)
|
二、 public class MKPlanNode 路线结点信息类 public java.lang.String name
public GeoPoint pt
|
三、public class RouteOverlay 使用SDK内置图标及展现方式展示路线信息 通过setdata接口设置路线数据 |
四、public class MKDrivingRouteResult 驾乘路线搜索结果类。 由MKSearchListener的onGetDrivingRouteResult方法获得 ①public int getNumPlan() 获取方案数目
②public MKRoutePlan getPlan(int index) 获取第index个方案
|
五、public class MKRoutePlan 此类表示一条驾车或步行出行方案 public MKRoute getRoute(int index)
|
六、public class MKWpNode 路线途经点数据结构 途经点可用两种模式表达: |
核心代码: package cn.zengfansheng.mybaidumap.searchCar;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.widget.Toast;
import cn.zengfansheng.mybaidumap.BaseActivity;
import cn.zengfansheng.mybaidumap.BaseMKMapViewListener;
import cn.zengfansheng.mybaidumap.BaseMkSearchListener;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.RouteOverlay;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPlanNode;
import com.baidu.mapapi.search.MKRoute;
import com.baidu.mapapi.search.MKRoutePlan;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKWpNode;
import com.baidu.platform.comapi.basestruct.GeoPoint;
/**
* 驾车路线检索
*/
public class DrivingSearchDemo extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); drivingSearch(); mapController.enableClick(true);
mMapView.regMapViewListener(mBMapMan, new BaseMKMapViewListener() {
@Override
public void onClickMapPoi(MapPoi mapPoiInfo) {
super.onClickMapPoi(mapPoiInfo);
Toast.makeText(getApplicationContext(), mapPoiInfo.strText,
Toast.LENGTH_SHORT).show();
}
});
}
/**
* 1、驾车路线 检索从天安门到百度大厦的驾车路线
*/
private void drivingSearch() { // 1、初始化
mkSearchListener = new MyMkSearchListener();
mkSearch.init(mBMapMan, mkSearchListener); // 2、搜索路线
String startCity = "北京市";
MKPlanNode start = new MKPlanNode();
start.pt = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)); String endCity = "北京市";
MKPlanNode end = new MKPlanNode();
end.pt = new GeoPoint(40057031, 116307852); // 设置搜索策略,默认是时间最少策略,一定要在搜索之前进行设置
// mkSearch.setTransitPolicy(MKSearch.ECAR_TIME_FIRST);//注意驾车策略是setDrivingPolicy,而不是setTransitPolicy
int policy = mkSearch.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST);// 设置驾车的策略:最短距离
System.out.println("policy:" + policy); //drivingSearch(java.lang.String startCity,MKPlanNode start,java.lang.String endCity,MKPlanNode end)
// mkSearch.drivingSearch(startCity, start, endCity, end);// 如果MKPlanNode设置的是pt(startCity和endCity可以省略不写,为null)
List<MKWpNode> list = new ArrayList<MKWpNode>();
MKWpNode mkWpNode = new MKWpNode();
mkWpNode.city = "北京";
mkWpNode.name = "北京大学";
list.add(mkWpNode);
mkWpNode = new MKWpNode();
mkWpNode.city = "北京";
mkWpNode.name = "北京西站"; mkSearch.drivingSearch(startCity, start, endCity, end, list);
}
/**
* 2、搜索结果的监听接口
*/
private class MyMkSearchListener extends BaseMkSearchListener {
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult result,int iError) {
super.onGetDrivingRouteResult(result, iError);
if (iError == 0 && result != null) {
RouteOverlay routeOverlay = new RouteOverlay(DrivingSearchDemo.this, mMapView);
// 设置数据
Toast.makeText(getApplicationContext(), "驾车路线:"+result.getNumPlan(), Toast.LENGTH_SHORT).show();
int numPlan = result.getNumPlan();// 获取方案数目
if (numPlan > 0) {
MKRoutePlan plan = result.getPlan(0);// 获取第index个方案
MKRoute mkRoute = plan.getRoute(0);
routeOverlay.setData(mkRoute);
// 获取集合并添加数据到集合中去
mMapView.getOverlays().add(routeOverlay);
// 刷新MapView
mMapView.refresh();
}
} else {
Toast.makeText(getApplicationContext(), "无结果",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onPause() {
super.onPause();
mkSearch.destory();
}
} |
结果: ①最短时间: ECAR_TIME_FIRST ②距离最短:ECAR_DIS_FIRST ③途经北京西站,北京大学 |