百度地图的路线规划还是比较容易的,需要注意的就是实现OnGetRoutePlanResultListener接口。
会重写四个方法
void onGetWalkingRouteResult(WalkingRouteResult var1);
void onGetTransitRouteResult(TransitRouteResult var1);
void onGetDrivingRouteResult(DrivingRouteResult var1);
void onGetBikingRouteResult(BikingRouteResult var1);
没看懂没关系,下边我会一一解说
1.
void onGetWalkingRouteResult(WalkingRouteResult var1);
这个方法是用来写不行路线规划的,var1则是百度返回的路线数据了
2.
void onGetTransitRouteResult(TransitRouteResult var1);
这个则是乘车路线,包含公交,地铁,不行,以及以后的飞机,火车等,想想,我的哥,好厉害呦
3.
void onGetDrivingRouteResult(DrivingRouteResult var1);
这个是自驾路线
4.
void onGetBikingRouteResult(BikingRouteResult var1);
这个方法就是咱们经常说的绿色环保无污染的交通工具自行车了
当然还有需要注意的就是MyTransitRouteOverlay和MyWalkingRouteOverlay这两个类,这分别是自定义的,运用的是自己的起点和终点的图片,当然,如果你想用系统的,我也拦不住你,对不撒
private class MyWalkingRouteOverlay extends WalkingRouteOverlay {
public MyWalkingRouteOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public BitmapDescriptor getStartMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
}
return null;
}
@Override
public BitmapDescriptor getTerminalMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
}
return null;
}
}
private class MyTransitRouteOverlay extends TransitRouteOverlay {
public MyTransitRouteOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public BitmapDescriptor getStartMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
}
return null;
}
@Override
public BitmapDescriptor getTerminalMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
}
return null;
}
}
activity类
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.RouteLine;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.BikingRouteResult;
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.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.demo.demo_location.mapapi.overlayutil.DrivingRouteOverlay;
import com.demo.demo_location.mapapi.overlayutil.TransitRouteOverlay;
import com.demo.demo_location.mapapi.overlayutil.WalkingRouteOverlay;
import java.util.List;
/**
* Created by 李博 on 2016/9/22.
*/
public class RouteLineActivity extends Activity implements
View.OnClickListener, BaiduMap.OnMapClickListener,
OnGetRoutePlanResultListener{
private Button btn_walk;
private Button btn_car;
private Button btn_bus;
private MapView mMapView;
private EditText et_start;
private EditText et_end;
private BaiduMap mBaiduMap;
//搜索服务类
private RoutePlanSearch mSearch;
//起点
private PlanNode start;
//终点
private PlanNode end;
private boolean useDefaultIcon = false;
private RouteLine route;
private TransitRouteResult nowResult = null;
private DrivingRouteResult nowResultd = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.soso_layout);
btn_walk = (Button) findViewById(R.id.btn_walk);
btn_car = (Button) findViewById(R.id.btn_car);
btn_bus = (Button) findViewById(R.id.btn_bus);
et_end = (EditText) findViewById(R.id.et_end);
et_start = (EditText) findViewById(R.id.et_start);
//地图初始化
mMapView = (MapView) findViewById(R.id.mapView);
mBaiduMap = mMapView.getMap();
//点击事件
btn_bus.setOnClickListener(this);
btn_car.setOnClickListener(this);
btn_walk.setOnClickListener(this);
//地图点击事件
mBaiduMap.setOnMapClickListener(this);
//初始化搜索模块
mSearch = RoutePlanSearch.newInstance();
//搜索返回结果的事件 回调
mSearch.setOnGetRoutePlanResultListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_bus:
setBtn_bus();
break;
case R.id.btn_car:
setBtn_car();
break;
case R.id.btn_walk:
setBtn_walk();
break;
}
}
/**步行*/
private void setBtn_walk(){ //设置起点
start = PlanNode.withCityNameAndPlaceName("北京", et_start.getText().toString());
end = PlanNode.withCityNameAndPlaceName("北京", et_end.getText().toString());
if(et_end.getText().toString().isEmpty() && et_start.getText().toString().isEmpty()){
Toast.makeText(getApplicationContext(), "起点和终点不能为空", Toast.LENGTH_SHORT).show();
} else {
mSearch.walkingSearch(new WalkingRoutePlanOption()
.from(start).to(end));
}
}
/**自驾*/
private void setBtn_car(){
//设置起点
start = PlanNode.withCityNameAndPlaceName("北京", et_start.getText().toString());
end = PlanNode.withCityNameAndPlaceName("北京", et_end.getText().toString());
if(et_end.getText().toString().isEmpty() && et_start.getText().toString().isEmpty()){
Toast.makeText(getApplicationContext(), "起点和终点不能为空", Toast.LENGTH_SHORT).show();
} else {
mSearch.drivingSearch((new DrivingRoutePlanOption()).from(start).to(end));
}
}
/**公交*/
private void setBtn_bus(){
//设置起点
start = PlanNode.withCityNameAndPlaceName("北京", et_start.getText().toString());
end = PlanNode.withCityNameAndPlaceName("北京", et_end.getText().toString());
if(et_end.getText().toString().isEmpty() && et_start.getText().toString().isEmpty()){
Toast.makeText(getApplicationContext(), "起点和终点不能为空", Toast.LENGTH_SHORT).show();
} else {
mSearch.transitSearch((new TransitRoutePlanOption())
.from(start).city("北京").to(end));
}
}
@Override
public void onGetWalkingRouteResult(WalkingRouteResult result) {
if(result == null || result.error != SearchResult.ERRORNO.NO_ERROR){
Toast.makeText(getApplicationContext(), "未找到结果", Toast.LENGTH_SHORT).show();
}
if(result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR){
// 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
// result.getSuggestAddrInfo()
return ;
}
if(result.error == SearchResult.ERRORNO.NO_ERROR){
route = result.getRouteLines().get(0);
// TransitOverlay是baidu map api提供的用于在地图上显示公交换乘路线的Overlay
WalkingRouteOverlay option = new MyWalkingRouteOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(option);
option.setData(result.getRouteLines().get(0));
option.addToMap();
option.zoomToSpan();
}
}
@Override
public void onGetTransitRouteResult(final TransitRouteResult result) {
if(result == null || result.error != SearchResult.ERRORNO.NO_ERROR){
Toast.makeText(getApplicationContext(), "未找到结果", Toast.LENGTH_SHORT).show();
}
if(result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR){
// 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
// result.getSuggestAddrInfo()
return ;
}
if(result.error == SearchResult.ERRORNO.NO_ERROR){
//线路条数 多于一条
if(result.getRouteLines().size() > 1){
MyTransitDlg myTransitDlg = new MyTransitDlg(RouteLineActivity.this, result.getRouteLines(),
RouteLineAdapter.Type.TRANSIT_ROUTE);
myTransitDlg.setOnItemInDlgClickLinster(new OnItemInDlgClickListener() {
@Override
public void onItemClick(int position) {
TransitRouteOverlay overlay = new MyTransitRouteOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result.getRouteLines().get(position));
overlay.addToMap();
overlay.zoomToSpan();
}
});
myTransitDlg.show();
//线路只有一条
} else if(result.getRouteLines().size() == 1){
route = result.getRouteLines().get(0);
TransitRouteOverlay overlay = new TransitRouteOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result.getRouteLines().get(0));
overlay.addToMap();
overlay.zoomToSpan();
//没有线路
} else {
return ;
}
}
}
@Override
public void onGetDrivingRouteResult(final DrivingRouteResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(getApplicationContext(), "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
// 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
// result.getSuggestAddrInfo()
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
//线路多于一条
if(result.getRouteLines().size() >1){
nowResultd = result;
MyTransitDlg myTransitDlg = new MyTransitDlg(RouteLineActivity.this,
result.getRouteLines(), RouteLineAdapter.Type.DRIVING_ROUTE);
myTransitDlg.setOnItemInDlgClickLinster(new OnItemInDlgClickListener() {
@Override
public void onItemClick(int position) {
route = nowResultd.getRouteLines().get(position);
DrivingRouteOverlay overlay = new DrivingRouteOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(overlay);
overlay.setData(nowResultd.getRouteLines().get(position));
overlay.addToMap();
overlay.zoomToSpan();
}
});
myTransitDlg.show();
} else if(result.getRouteLines().size() == 1){
route = result.getRouteLines().get(0);
DrivingRouteOverlay overlay = new DrivingRouteOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result.getRouteLines().get(0));
overlay.addToMap();
overlay.zoomToSpan();
} else {
return ;
}
}
}
@Override
public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
}
@Override
public void onMapClick(LatLng latLng) {
}
@Override
public boolean onMapPoiClick(MapPoi mapPoi) {
return false;
}
private class MyTransitRouteOverlay extends TransitRouteOverlay {
public MyTransitRouteOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public BitmapDescriptor getStartMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
}
return null;
}
@Override
public BitmapDescriptor getTerminalMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
}
return null;
}
}
private class MyWalkingRouteOverlay extends WalkingRouteOverlay {
public MyWalkingRouteOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public BitmapDescriptor getStartMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
}
return null;
}
@Override
public BitmapDescriptor getTerminalMarker() {
if (useDefaultIcon) {
return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
}
return null;
}
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
mSearch.destroy();
mMapView.onDestroy();
super.onDestroy();
}
// 响应DLg中的List item 点击
interface OnItemInDlgClickListener {
public void onItemClick( int position);
}
// 供路线选择的Dialog
class MyTransitDlg extends Dialog {
private List extends RouteLine> mtransitRouteLines;
private ListView transitRouteList;
private RouteLineAdapter mTransitAdapter;
OnItemInDlgClickListener onItemInDlgClickListener;
public MyTransitDlg(Context context, int theme) {
super(context, theme);
}
public MyTransitDlg(Context context, List< ? extends RouteLine> transitRouteLines, RouteLineAdapter.Type
type) {
this( context, 0);
mtransitRouteLines = transitRouteLines;
mTransitAdapter = new RouteLineAdapter( context, mtransitRouteLines , type);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transit_dialog);
transitRouteList = (ListView) findViewById(R.id.transitList);
transitRouteList.setAdapter(mTransitAdapter);
transitRouteList.setOnItemClickListener( new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
onItemInDlgClickListener.onItemClick( position);
dismiss();
}
});
}
public void setOnItemInDlgClickLinster( OnItemInDlgClickListener itemListener) {
onItemInDlgClickListener = itemListener;
}
}
}
因为乘车路线如果是多条的话是一个listview,所以,适配器在所难免了
adapter类
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.baidu.mapapi.search.core.RouteLine;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import java.util.List;
public class RouteLineAdapter extends BaseAdapter {
private List extends RouteLine> routeLines;
private LayoutInflater layoutInflater;
private Type mtype;
public RouteLineAdapter(Context context, List routeLines, Type type) {
this.routeLines = routeLines;
layoutInflater = LayoutInflater.from( context);
mtype = type;
}
@Override
public int getCount() {
return routeLines.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
NodeViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.activity_transit_item, null);
holder = new NodeViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.transitName);
holder.lightNum = (TextView) convertView.findViewById(R.id.lightNum);
holder.dis = (TextView) convertView.findViewById(R.id.dis);
convertView.setTag(holder);
} else {
holder = (NodeViewHolder) convertView.getTag();
}
switch (mtype) {
case TRANSIT_ROUTE:
holder.name.setText("路线" + (position + 1));
int time = routeLines.get(position).getDuration();
if ( time / 3600 == 0 ) {
holder.lightNum.setText( "大约需要:" + time / 60 + "分钟" );
} else {
holder.lightNum.setText( "大约需要:" + time / 3600 + "小时" + (time % 3600) / 60 + "分钟" );
}
holder.dis.setText("距离大约是:" + routeLines.get(position).getDistance());
break;
case DRIVING_ROUTE:
DrivingRouteLine drivingRouteLine = (DrivingRouteLine) routeLines.get(position);
holder.name.setText( "线路" + (position + 1));
holder.lightNum.setText( "红绿灯数:" + drivingRouteLine.getLightNum());
holder.dis.setText("拥堵距离为:" + drivingRouteLine.getCongestionDistance() + "米");
break;
default:
}
return convertView;
}
private class NodeViewHolder {
private TextView name;
private TextView lightNum;
private TextView dis;
}
public enum Type {
TRANSIT_ROUTE, // 公交
DRIVING_ROUTE // 驾车
}
}
主布局
如果在出错的话,那就是图片了,自己百度找两张就可以了
但是问题来了,本菜在百度上找了半天,也没有找到公交路线详情,没办法,只能去百度地图API找了,结果百度给我的是
返回类型 | 方法 |
---|---|
MKPlanNode | getEnd()
|
int | getNumPlan()
|
MKTransitRoutePlan | getPlan(int index)
|
MKPlanNode | getStart()
|
MKRouteAddrResult | getAddrResult()
|
百度地图给的需要写城市和地址两个,我要的是输入一个地址就可以定位的啊,反正我还没找到呢,有建议的,请保留,不服,你顺着点线爬过来打我啊。
以后更新路线规划并显示出详细的路线规划信息!!