首先我有一个MainActivity.java。和一个RoutSearch.java。我想实现从RoutSearch 输入公交路线后跳转回MainActivity进行路线的显示。
RoutSearch.java代码:
package cn.vaecer.biye.search;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.RouteOverlay;
import com.baidu.mapapi.TransitOverlay;
import cn.vaecer.biye.MainActivity;
import cn.vaecer.biye.R;
import cn.vaecer.biye.SearchActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RoutSearch extends Activity {
private Button back;
private Button routSearch;
private EditText routNumber;
private BMapManager bMapManager;// 地图引擎
private MKSearch mkSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.routsearch);
back = (Button) findViewById(R.id.back);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(RoutSearch.this, SearchActivity.class);
RoutSearch.this.finish();
}
});
mkSearch = new MKSearch();
mkSearch.init(bMapManager, new MyListener());
routNumber = (EditText) findViewById(R.id.routnumber);// 输入框初始化
routSearch = (Button) findViewById(R.id.routsearchgo);
String routString = routNumber.getText().toString();
mkSearch.poiSearchInCity("福州", routString);
routSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Bundle bundle = new Bundle();
// // 保存要查询路线进bundle
// bundle.putString("rout", routString);
// intent.putExtras(bundle);
Intent intent = new Intent(RoutSearch.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
}
public class MyListener implements MKSearchListener {
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
if (arg0 == null) {
return;
}
TransitOverlay transitOverlay = new TransitOverlay(RoutSearch.this,
MainActivity.mapView);
transitOverlay.setData(arg0.getPlan(0));
MainActivity.mapView.getOverlays().add(transitOverlay);
MainActivity.mapView.invalidate();
}
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
if (arg0 == null || arg1 != 0) {
Toast.makeText(RoutSearch.this, "对不起,未找到您需要的线路!", 1).show();
return;
}
RouteOverlay routeOverlay = new RouteOverlay(RoutSearch.this,
MainActivity.mapView);
routeOverlay.setData(arg0.getBusRoute());
MainActivity.mapView.getOverlays().clear();
MainActivity.mapView.getOverlays().add(routeOverlay);
MainActivity.mapView.invalidate();
MainActivity.mapView.getController().animateTo(
arg0.getBusRoute().getStart());
}
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onGetPoiDetailSearchResult(int arg0, int arg1) {
// TODO Auto-generated method stub
}
// 获得公交线路图的UID,根據此UID發起公交線路圖的詳情
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
if (arg0 == null || arg2 != 0) {
Toast.makeText(RoutSearch.this, "对不起,未能查询到您需要的公交线路!", 1).show();
return;
}
MKPoiInfo mkPoiInfo = null;
int totalPoiNum = arg0.getNumPois();// 获得总站点
for (int i = 0; i < totalPoiNum; i++) {
mkPoiInfo = arg0.getPoi(i);
if (mkPoiInfo.ePoiType == 2) {
break;
}
}
mkSearch.busLineSearch("福州", mkPoiInfo.uid);
}
public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
}
}
由于MainActivity代码过多,就不贴出,在其中我没有做任何的关于线路搜索的代码,只是将mapView设置成public static 的形式。