在第二关我们说了如何查询公交路线图,但前提你得知道公交是几路公交!而一般人知道某个地点,就想去,而不是通过几路公交来查询!所以这关我们来说说如果查询两个地点之间的路线规划【1.骑行路线2.驾车路线3.公交路线4.步行路线】
//路径规划搜索接口 routePlanSearch = RoutePlanSearch.newInstance(); routePlanSearch.setOnGetRoutePlanResultListener(this);
/** * 2.各种路线规划的查询 */ case R.id.btn_search_biking: //骑行路线 routePlanSearch.bikingSearch(new BikingRoutePlanOption().from(fromPlanNode).to(toPlanNode)); layoutBusSearch.setVisibility(View.GONE); break; case R.id.btn_search_car: //驾车路线 routePlanSearch.drivingSearch(new DrivingRoutePlanOption().from(fromPlanNode).to(toPlanNode)); layoutBusSearch.setVisibility(View.GONE); break; case R.id.btn_search_bus: //换乘公共交通工具的路线 routePlanSearch.transitSearch(new TransitRoutePlanOption().city(etCity.getText().toString()).from(fromPlanNode).to(toPlanNode)); layoutBusSearch.setVisibility(View.GONE); break; case R.id.btn_search_walk: //步行路线 routePlanSearch.walkingSearch(new WalkingRoutePlanOption().from(fromPlanNode).to(toPlanNode)); layoutBusSearch.setVisibility(View.GONE); break;
//公交路线覆盖物,并设置路线的点击事件 overlay = new BusLineOverlay(mBaiduMap); //骑行覆盖物添加并监听 bikingOverlay = new BikingRouteOverlay(mBaiduMap); //驾车覆盖物添加并监听 drivingOverlay= new DrivingRouteOverlay(mBaiduMap); //换乘覆盖物添加并监听 transOverlay = new TransitRouteOverlay(mBaiduMap); //步行覆盖物添加并监听 walkingOverlay = new WalkingRouteOverlay(mBaiduMap); //TODO: 添加各种路线覆盖物的点击事件 mBaiduMap.setOnMarkerClickListener(overlay); mBaiduMap.setOnMarkerClickListener(bikingOverlay); mBaiduMap.setOnMarkerClickListener(drivingOverlay); mBaiduMap.setOnMarkerClickListener(transOverlay); mBaiduMap.setOnMarkerClickListener(walkingOverlay);b.然后根据不同的回调,把覆盖物添加到地图上面
/** * 以下四个方法是路线规划搜索监听后的回调 * a.步行回调 * @param walkingRouteResult */ @Override public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) { if (walkingRouteResult == null || walkingRouteResult.error != SearchResult.ERRORNO.NO_ERROR) { T.showLong(this, "抱歉,未找到结果"); return; } L.i("线路条数:" + walkingRouteResult.getRouteLines().size()); ListrouteLines = walkingRouteResult.getRouteLines(); mBaiduMap.clear(); walkingOverlay.removeFromMap(); walkingOverlay.setData(routeLines.get(0)); walkingOverlay.addToMap(); walkingOverlay.zoomToSpan(); } /** * b.换乘路线结果回调 * @param transitRouteResult */ @Override public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) { if (transitRouteResult == null || transitRouteResult.error != SearchResult.ERRORNO.NO_ERROR) { T.showLong(this, "抱歉,未找到结果"); return; } L.i("线路条数:" + transitRouteResult.getRouteLines().size()); List routeLines = transitRouteResult.getRouteLines(); mBaiduMap.clear(); transOverlay.removeFromMap(); transOverlay.setData(routeLines.get(0)); transOverlay.addToMap(); transOverlay.zoomToSpan(); } /** * c.驾车路线结果回调 * * @param drivingRouteResult */ @Override public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) { if (drivingRouteResult == null || drivingRouteResult.error != SearchResult.ERRORNO.NO_ERROR) { T.showLong(this, "抱歉,未找到结果"); return; } L.i("线路条数:" + drivingRouteResult.getRouteLines().size()); List routeLines = drivingRouteResult.getRouteLines(); mBaiduMap.clear(); drivingOverlay.removeFromMap(); drivingOverlay.setData(routeLines.get(0)); drivingOverlay.addToMap(); drivingOverlay.zoomToSpan(); } /** * d.骑行路线结果回调 * * @param bikingRouteResult */ @Override public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) { if (bikingRouteResult == null || bikingRouteResult.error != SearchResult.ERRORNO.NO_ERROR) { T.showLong(this, "抱歉,未找到结果"); return; } L.i("线路条数:" + bikingRouteResult.getRouteLines().size()); List routeLines = bikingRouteResult.getRouteLines(); mBaiduMap.clear(); bikingOverlay.removeFromMap(); bikingOverlay.setData(routeLines.get(0)); bikingOverlay.addToMap(); bikingOverlay.zoomToSpan(); }
package cq.cake.chwl.ui;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.baidu.mapapi.overlayutil.BikingRouteOverlay;
import com.baidu.mapapi.overlayutil.BusLineOverlay;
import com.baidu.mapapi.overlayutil.DrivingRouteOverlay;
import com.baidu.mapapi.overlayutil.TransitRouteOverlay;
import com.baidu.mapapi.overlayutil.WalkingRouteOverlay;
import com.baidu.mapapi.search.busline.BusLineResult;
import com.baidu.mapapi.search.busline.BusLineSearch;
import com.baidu.mapapi.search.busline.BusLineSearchOption;
import com.baidu.mapapi.search.busline.OnGetBusLineSearchResultListener;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.route.BikingRouteLine;
import com.baidu.mapapi.search.route.BikingRoutePlanOption;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteLine;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteLine;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cq.cake.base.BaseToolbarMapActivity;
import cq.cake.chwl.R;
import cq.cake.util.L;
import cq.cake.util.T;
/**
* 公交路线地图
* 1.首先从城市检索中查询出公交路线类型的PoiInfo
* 2.更具PoiInfo的类型筛选出是公交路线的检索,并添加到集合中
* 3.用BusLineSearch分别查询,集合中数据
*/
public class BusLineMapActivity extends BaseToolbarMapActivity implements OnGetBusLineSearchResultListener, OnGetPoiSearchResultListener, OnGetRoutePlanResultListener {
@BindView(R.id.et_start_bus_station)
EditText etStartBusStation;
@BindView(R.id.et_end_bus_station)
EditText etEndBusStation;
@BindView(R.id.btn_search_car)
Button btnSearchCar;
@BindView(R.id.btn_search_bus)
Button btnSearchBus;
@BindView(R.id.btn_search_walk)
Button btnSearchWalk;
@BindView(R.id.layout_bus_search)
LinearLayout layoutBusSearch;
@BindView(R.id.et_station_city)
EditText etStationCity;
@BindView(R.id.et_bus_num)
EditText etBusNum;
@BindView(R.id.layout_bus_station)
LinearLayout layoutBusStation;
@BindView(R.id.et_city)
EditText etCity;
@BindView(R.id.btn_search_biking)
Button btnSearchBiking;
@BindView(R.id.btn_station_search)
Button btnStationSearch;
@BindView(R.id.btn_station_pre)
Button btnStationPre;
@BindView(R.id.btn_station_next)
Button btnStationNext;
@BindView(R.id.layout_station_pre_next)
LinearLayout layoutStationPreNext;
private PoiSearch poiSearch;//检索接口
private BusLineSearch busLineSearch;//公交线路查询接口
private RoutePlanSearch routePlanSearch;//路径规划搜索接口
private List busLines = new ArrayList<>();
private int uidPostion = 0;//当前选中第几条路线
private BusLineOverlay overlay; // 公交路线绘制对象
private BikingRouteOverlay bikingOverlay;
private DrivingRouteOverlay drivingOverlay;
private TransitRouteOverlay transOverlay;
private WalkingRouteOverlay walkingOverlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus_line_map);
ButterKnife.bind(this);
init();
onEvent();
}
private void onEvent() {
}
public void init() {
toolbar.setLogo(R.mipmap.bus_128);
//检索查询初始化并监听
poiSearch = PoiSearch.newInstance();
poiSearch.setOnGetPoiSearchResultListener(this);
//路线查询初始化并监听
busLineSearch = BusLineSearch.newInstance();
busLineSearch.setOnGetBusLineSearchResultListener(this);
//路径规划搜索接口
routePlanSearch = RoutePlanSearch.newInstance();
routePlanSearch.setOnGetRoutePlanResultListener(this);
//公交路线覆盖物,并设置路线的点击事件
overlay = new BusLineOverlay(mBaiduMap);
//骑行覆盖物添加并监听
bikingOverlay = new BikingRouteOverlay(mBaiduMap);
//驾车覆盖物添加并监听
drivingOverlay= new DrivingRouteOverlay(mBaiduMap);
//换乘覆盖物添加并监听
transOverlay = new TransitRouteOverlay(mBaiduMap);
//步行覆盖物添加并监听
walkingOverlay = new WalkingRouteOverlay(mBaiduMap);
//TODO: 添加各种路线覆盖物的点击事件
mBaiduMap.setOnMarkerClickListener(overlay);
mBaiduMap.setOnMarkerClickListener(bikingOverlay);
mBaiduMap.setOnMarkerClickListener(drivingOverlay);
mBaiduMap.setOnMarkerClickListener(transOverlay);
mBaiduMap.setOnMarkerClickListener(walkingOverlay);
}
/**
* 【1】首先检索查询
*
* @param poiResult
*/
@Override
public void onGetPoiResult(PoiResult poiResult) {
if (poiResult == null || poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
T.showShort(this, "抱歉,未找到结果");
return;
}
if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) {
busLines.clear();
List allPoi = poiResult.getAllPoi();
if (allPoi == null || allPoi.size() == 0) return;
for (int i = 0; i < allPoi.size(); i++) {
PoiInfo poiInfo = allPoi.get(i);
L.i("类型:" + poiInfo.type);
//如果是公交类型的路线,就把它的UID添加到集合中
if (poiInfo.type == PoiInfo.POITYPE.BUS_LINE) {
busLines.add(poiInfo.uid);
}
}
if (busLines.size() == 0) return;
uidPostion = 0;
T.showLong(this, "发现" + busLines.size() + "条线路!");
busLineSearch.searchBusLine(new BusLineSearchOption().city(etStationCity.getText().toString()).uid(busLines.get(uidPostion)));
}
}
/**
* 【2】从检索的结果中筛选出公交路线
* 并将其添加到地图上
* @param busLineResult
*/
@Override
public void onGetBusLineResult(BusLineResult busLineResult) {
if (busLineResult == null || busLineResult.error != SearchResult.ERRORNO.NO_ERROR) {
T.showLong(this, "抱歉,未找到结果");
return;
}
mBaiduMap.clear();
overlay.removeFromMap();
overlay.setData(busLineResult);
overlay.addToMap();
overlay.zoomToSpan();
layoutStationPreNext.setVisibility(View.VISIBLE);
}
@Override
public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
}
@Override
public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_bus_map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
layoutBusSearch.setVisibility(layoutBusSearch.isShown() ? View.GONE : View.VISIBLE);
break;
case R.id.menu2:
layoutBusStation.setVisibility(layoutBusStation.isShown() ? View.GONE : View.VISIBLE);
break;
}
return true;
}
@OnClick({R.id.btn_station_search,R.id.btn_station_pre,R.id.btn_station_next,R.id.btn_search_biking, R.id.btn_search_car, R.id.btn_search_bus, R.id.btn_search_walk})
public void doClicks(View view) {
//路径规划中的出行节点信息,出行节点包括:起点,终点,途经点
PlanNode fromPlanNode = PlanNode.withCityNameAndPlaceName(etCity.getText().toString(), etStartBusStation.getText().toString());
PlanNode toPlanNode = PlanNode.withCityNameAndPlaceName(etCity.getText().toString(), etEndBusStation.getText().toString());
switch (view.getId()) {
/**
* 1.公交线路相关的查询
*/
case R.id.btn_station_search:
poiSearch.searchInCity(new PoiCitySearchOption().city(etStationCity.getText().toString()).keyword(etBusNum.getText().toString()));
break;
case R.id.btn_station_pre:
if (uidPostion > 0) {
uidPostion--;
searchBusLine();
}
layoutBusStation.setVisibility(View.GONE);
break;
case R.id.btn_station_next:
if (uidPostion < (busLines.size() - 1)) {
uidPostion++;
searchBusLine();
}
layoutBusStation.setVisibility(View.GONE);
break;
/**
* 2.各种路线规划的查询
*/
case R.id.btn_search_biking:
//骑行路线
routePlanSearch.bikingSearch(new BikingRoutePlanOption().from(fromPlanNode).to(toPlanNode));
layoutBusSearch.setVisibility(View.GONE);
break;
case R.id.btn_search_car:
//驾车路线
routePlanSearch.drivingSearch(new DrivingRoutePlanOption().from(fromPlanNode).to(toPlanNode));
layoutBusSearch.setVisibility(View.GONE);
break;
case R.id.btn_search_bus:
//换乘公共交通工具的路线
routePlanSearch.transitSearch(new TransitRoutePlanOption().city(etCity.getText().toString()).from(fromPlanNode).to(toPlanNode));
layoutBusSearch.setVisibility(View.GONE);
break;
case R.id.btn_search_walk:
//步行路线
routePlanSearch.walkingSearch(new WalkingRoutePlanOption().from(fromPlanNode).to(toPlanNode));
layoutBusSearch.setVisibility(View.GONE);
break;
}
}
private void searchBusLine() {
L.i("当前查询的路线==" + uidPostion);
busLineSearch.searchBusLine(new BusLineSearchOption().city(etStationCity.getText().toString()).uid(busLines.get(uidPostion)));
}
@Override
protected void onDestroy() {
super.onDestroy();
poiSearch.destroy();
busLineSearch.destroy();
}
/**
* 以下四个方法是路线规划搜索监听后的回调
* a.步行回调
* @param walkingRouteResult
*/
@Override
public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
if (walkingRouteResult == null || walkingRouteResult.error != SearchResult.ERRORNO.NO_ERROR) {
T.showLong(this, "抱歉,未找到结果");
return;
}
L.i("线路条数:" + walkingRouteResult.getRouteLines().size());
List routeLines = walkingRouteResult.getRouteLines();
mBaiduMap.clear();
walkingOverlay.removeFromMap();
walkingOverlay.setData(routeLines.get(0));
walkingOverlay.addToMap();
walkingOverlay.zoomToSpan();
}
/**
* b.换乘路线结果回调
* @param transitRouteResult
*/
@Override
public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
if (transitRouteResult == null || transitRouteResult.error != SearchResult.ERRORNO.NO_ERROR) {
T.showLong(this, "抱歉,未找到结果");
return;
}
L.i("线路条数:" + transitRouteResult.getRouteLines().size());
List routeLines = transitRouteResult.getRouteLines();
mBaiduMap.clear();
transOverlay.removeFromMap();
transOverlay.setData(routeLines.get(0));
transOverlay.addToMap();
transOverlay.zoomToSpan();
}
/**
* c.驾车路线结果回调
*
* @param drivingRouteResult
*/
@Override
public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
if (drivingRouteResult == null || drivingRouteResult.error != SearchResult.ERRORNO.NO_ERROR) {
T.showLong(this, "抱歉,未找到结果");
return;
}
L.i("线路条数:" + drivingRouteResult.getRouteLines().size());
List routeLines = drivingRouteResult.getRouteLines();
mBaiduMap.clear();
drivingOverlay.removeFromMap();
drivingOverlay.setData(routeLines.get(0));
drivingOverlay.addToMap();
drivingOverlay.zoomToSpan();
}
/**
* d.骑行路线结果回调
*
* @param bikingRouteResult
*/
@Override
public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
if (bikingRouteResult == null || bikingRouteResult.error != SearchResult.ERRORNO.NO_ERROR) {
T.showLong(this, "抱歉,未找到结果");
return;
}
L.i("线路条数:" + bikingRouteResult.getRouteLines().size());
List routeLines = bikingRouteResult.getRouteLines();
mBaiduMap.clear();
bikingOverlay.removeFromMap();
bikingOverlay.setData(routeLines.get(0));
bikingOverlay.addToMap();
bikingOverlay.zoomToSpan();
}
}