百度地图加强_搜索_路线检索_公交换乘路线
|
是点到点,而不是公交专线 TransitOverlay 覆盖物 |
核心代码: 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.TransitOverlay;
import com.baidu.mapapi.search.MKPlanNode;
import com.baidu.mapapi.search.MKTransitRoutePlan;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;
/**
* 公交路线换乘查询
*/
public class TransitSearchDemo extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
transitSearch();
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 transitSearch() {
// 1、初始化
mkSearchListener = new MyMkSearchListener();
mkSearch.init(mBMapMan, mkSearchListener);
// 2、搜索路线
MKPlanNode start = new MKPlanNode();
start.pt = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));
MKPlanNode end = new MKPlanNode();
end.pt = new GeoPoint(40057031, 116307852);
mkSearch.transitSearch("北京", start, end);
}
/**
* 2、搜索结果的监听接口
*/
private class MyMkSearchListener extends BaseMkSearchListener {
@Override
public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {
super.onGetTransitRouteResult(result, iError);
if (iError == 0 && result != null) {
// int getNumPlan()
// 返回方案数目
// MKTransitRoutePlan getPlan(int index)
// 返回第index个方案
Toast.makeText(getApplicationContext(), "公交路线:"+result.getNumPlan()+"条", Toast.LENGTH_SHORT).show();
int numPlan = result.getNumPlan();
if (numPlan>0) {
for (int i = 0; i < numPlan; i++) {
TransitOverlay transitOverlay = new TransitOverlay(TransitSearchDemo.this, mMapView);
MKTransitRoutePlan mkTransitRoutePlan = result.getPlan(i);//获取每一个方案
transitOverlay.setData(mkTransitRoutePlan);
// 设置数据
mMapView.getOverlays().add(transitOverlay);
}
mMapView.refresh();
}
} else {
Toast.makeText(getApplicationContext(), "无结果",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onPause() {
super.onPause();
mkSearch.destory();
}
} |
结果: |