百度地图加强_搜索_路线检索_步行路线
一、步行路线 |
步行路线检索 walkingSearch RouteOverlay ①public int setTransitPolicy(int policy)
②public int walkingSearch (java.lang.String startCity, MKPlanNode start,java.lang.String endCity, MKPlanNode end)
|
核心代码: package cn.zengfansheng.mybaidumap.searchCar;
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.MKPlanNode;
import com.baidu.mapapi.search.MKRoute;
import com.baidu.mapapi.search.MKRoutePlan;
import com.baidu.mapapi.search.MKWalkingRouteResult;
/**
* 步行路线检索
*/
public class WalkingSearchDemo extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); walkingSearch(); 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 walkingSearch() { // 1、初始化
mkSearchListener = new MyMkSearchListener();
mkSearch.init(mBMapMan, mkSearchListener); // 2、搜索路线
String startCity = "北京市";
String endCity = "北京市";
MKPlanNode start = new MKPlanNode();
start.pt = geoPoint; MKPlanNode end = new MKPlanNode();
end.name = "金燕龙办公楼"; //int policy = mkSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);// 设置驾车的策略:最短距离
// int policy = mkSearch.setTransitPolicy(MKSearch.EBUS_WALK_FIRST);
// 步行不需要策略
mkSearch.walkingSearch(startCity, start, endCity, end);
}
/**
* 2、步行路线搜索结果的监听接口
*/
private class MyMkSearchListener extends BaseMkSearchListener {
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult result, int iError) {
super.onGetWalkingRouteResult(result, iError);
if (iError == 0 && result != null) {
RouteOverlay routeOverlay = new RouteOverlay(WalkingSearchDemo.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();
}
} |
结果: ① // 步行不需要策略 |
|