[android]代码库
Button mBtnDrive = null; // 驾车搜索
Button mBtnTransit = null; // 公交搜索
Button mBtnWalk = null; // 步行搜索
MapView mMapView = null; // 地图View
MKSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.routeplan);
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
if (app.mBMapMan == null) {
app.mBMapMan = new BMapManager(getApplication());
app.mBMapMan.init(app.mStrKey, new BMapApiDemoApp.MyGeneralListener());
}
app.mBMapMan.start();
// 如果使用地图SDK,请初始化地图Activity
super.initMapActivity(app.mBMapMan);
mMapView = (MapView)findViewById(R.id.bmapView);
mMapView.setBuiltInZoomControls(true);
//设置在缩放动画过程中也显示overlay,默认为不绘制
mMapView.setDrawOverlayWhenZooming(true);
// 初始化搜索模块,注册事件监听
mSearch = new MKSearch();
mSearch.init(app.mBMapMan, new MKSearchListener(){
@Override
public void onGetPoiDetailSearchResult(int type, int error) {
}
public void onGetDrivingRouteResult(MKDrivingRouteResult res,
int error) {
// 错误号可参考MKEvent中的定义
if (error != 0 || res == null) {
Toast.makeText(RoutePlan.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
return;
}
RouteOverlay routeOverlay = new RouteOverlay(RoutePlan.this, mMapView);
// 此处仅展示一个方案作为示例
routeOverlay.setData(res.getPlan(0).getRoute(0));
mMapView.getOverlays().clear();
mMapView.getOverlays().add(routeOverlay);
mMapView.invalidate();
mMapView.getController().animateTo(res.getStart().pt);
}
public void onGetTransitRouteResult(MKTransitRouteResult res,
int error) {
if (error != 0 || res == null) {
Toast.makeText(RoutePlan.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
return;
}
TransitOverlay routeOverlay = new TransitOverlay (RoutePlan.this, mMapView);
// 此处仅展示一个方案作为示例
routeOverlay.setData(res.getPlan(0));
mMapView.getOverlays().clear();
mMapView.getOverlays().add(routeOverlay);
mMapView.invalidate();
mMapView.getController().animateTo(res.getStart().pt);
}
public void onGetWalkingRouteResult(MKWalkingRouteResult res,
int error) {
if (error != 0 || res == null) {
Toast.makeText(RoutePlan.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
return;
}
RouteOverlay routeOverlay = new RouteOverlay(RoutePlan.this, mMapView);
// 此处仅展示一个方案作为示例
routeOverlay.setData(res.getPlan(0).getRoute(0));
mMapView.getOverlays().clear();
mMapView.getOverlays().add(routeOverlay);
mMapView.invalidate();
mMapView.getController().animateTo(res.getStart().pt);
}
public void onGetAddrResult(MKAddrInfo res, int error) {
}
public void onGetPoiResult(MKPoiResult res, int arg1, int arg2) {
}
public void onGetBusDetailResult(MKBusLineResult result, int iError) {
}
@Override
public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub
}
});
// 设定搜索按钮的响应
mBtnDrive = (Button)findViewById(R.id.drive);
mBtnTransit = (Button)findViewById(R.id.transit);
mBtnWalk = (Button)findViewById(R.id.walk);
OnClickListener clickListener = new OnClickListener(){
public void onClick(View v) {
SearchButtonProcess(v);
}
};
mBtnDrive.setOnClickListener(clickListener);
mBtnTransit.setOnClickListener(clickListener);
mBtnWalk.setOnClickListener(clickListener);
}
void SearchButtonProcess(View v) {
// 处理搜索按钮响应
EditText editSt = (EditText)findViewById(R.id.start);
EditText editEn = (EditText)findViewById(R.id.end);
// 对起点终点的name进行赋值,也可以直接对坐标赋值,赋值坐标则将根据坐标进行搜索
MKPlanNode stNode = new MKPlanNode();
stNode.name = editSt.getText().toString();
MKPlanNode enNode = new MKPlanNode();
enNode.name = editEn.getText().toString();
// 实际使用中请对起点终点城市进行正确的设定
if (mBtnDrive.equals(v)) {
mSearch.drivingSearch("广州", stNode, "广州", enNode);
} else if (mBtnTransit.equals(v)) {
mSearch.transitSearch("广州", stNode, enNode);
} else if (mBtnWalk.equals(v)) {
mSearch.walkingSearch("北京", stNode, "北京", enNode);
}
}
@Override
protected void onPause() {
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
app.mBMapMan.stop();
super.onPause();
}
@Override
protected void onResume() {
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
app.mBMapMan.start();
super.onResume();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
[代码运行效果截图]
[源代码打包下载]