承接上文https://blog.csdn.net/qq_41562408/article/details/82829938,本次我们来实现百度地图的导航功能实现。百度地图的导航功能对于地点的确定我感觉主要是通过纬经度确定地点然后进行导航,其导航模式可分为:步行导航、公交导航、骑行导航、驾驶导航。其实代码类似,本文主要就针对生活中较为常见的步行导航、公交导航以及驾驶导航进行介绍。
1、步行导航(其中对于项目创建以及基本的地图显示就不进行介绍,读者可就本人之前文章进行观看本文默认基本环境已经构建好):
核心代码为:
WalkingRouteOverlay overlay=new WalkingRouteOverlay(baiduMap); baiduMap.setOnMarkerClickListener(overlay); ListrouteLines=result.getRouteLines();//获取所有搜素路线,最优化路线在集合最前面 WalkingRouteLine drivingRouteLine=routeLines.get(0);//搜素结果覆盖的覆盖物 overlay.setData(drivingRouteLine); overlay.addToMap(); overlay.zoomToSpan();
(1)继承
implements OnGetRoutePlanResultListener
(2)添加变量
protected RoutePlanSearch routePlanSearch;
protected LatLng target1=new LatLng(39.547512,116.654764); protected LatLng target=new LatLng(39.527814,116.713442);
从自然公园到万达广场,在我们学校外出也就这两大娱乐场所了
(3)oncreat方法添加
routePlanSearch= RoutePlanSearch.newInstance(); routePlanSearch.setOnGetRoutePlanResultListener(this); routePlansearchInit();
之后敲入代码:
public void routePlansearchInit() { routePlanSearch.walkingSearch(getSearchParams()); } private WalkingRoutePlanOption getSearchParams() { WalkingRoutePlanOption params=new WalkingRoutePlanOption(); params.from(PlanNode.withLocation(target));//设置起点 params.to(PlanNode.withLocation(target1));//终点 return params; } @Override public void onGetWalkingRouteResult(WalkingRouteResult result) { WalkingRouteOverlay overlay=new WalkingRouteOverlay(baiduMap); baiduMap.setOnMarkerClickListener(overlay); ListrouteLines=result.getRouteLines();//获取所有搜素路线,最优化路线在集合最前面 WalkingRouteLine drivingRouteLine=routeLines.get(0);//搜素结果覆盖的覆盖物 overlay.setData(drivingRouteLine); overlay.addToMap(); overlay.zoomToSpan(); } @Override public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) { } @Override public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) { } @Override public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) { } @Override public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) { } @Override public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) { }
2、公交导航:
核心代码为:
TransitRouteOverlay overlay=new TransitRouteOverlay(baiduMap); baiduMap.setOnMarkerClickListener(overlay); ListrouteLines=result.getRouteLines();//获取所有搜素路线,最优化路线在集合最前面 TransitRouteLine drivingRouteLine=routeLines.get(0);//搜素结果覆盖的覆盖物 overlay.setData(drivingRouteLine); overlay.addToMap(); overlay.zoomToSpan();
导航代码都类似,所以就依据刚才的项目进行简单的修改便可,oncreat方法不用修改,其他的代码如下:
public void routePlansearchInit() { routePlanSearch.transitSearch(getSearchParams()); } private TransitRoutePlanOption getSearchParams() { TransitRoutePlanOption params=new TransitRoutePlanOption(); params.from(PlanNode.withLocation(target1));//设置起点 params.city("廊坊");//设置搜素城市 params.policy(TransitRoutePlanOption.TransitPolicy.EBUS_TIME_FIRST);//设置策略为时间优先 params.to(PlanNode.withLocation(target));//终点 return params; } @Override public void onGetWalkingRouteResult(WalkingRouteResult result) { } @Override public void onGetTransitRouteResult(TransitRouteResult result) { TransitRouteOverlay overlay=new TransitRouteOverlay(baiduMap); baiduMap.setOnMarkerClickListener(overlay); ListrouteLines=result.getRouteLines();//获取所有搜素路线,最优化路线在集合最前面 TransitRouteLine drivingRouteLine=routeLines.get(0);//搜素结果覆盖的覆盖物 overlay.setData(drivingRouteLine); overlay.addToMap(); overlay.zoomToSpan(); } @Override public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) { } @Override public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) { } @Override public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) { } @Override public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) { }
3、驾驶导航
核心代码为:
DrivingRouteOverlay overlay=new DrivingRouteOverlay(baiduMap); baiduMap.setOnMarkerClickListener(overlay); ListrouteLines=result.getRouteLines();//获取所有搜素路线,最优化路线在集合最前面 DrivingRouteLine drivingRouteLine=routeLines.get(0);//搜素结果覆盖的覆盖物 overlay.setData(drivingRouteLine); overlay.addToMap(); overlay.zoomToSpan();
只需在上一项目进行简单修改,oncreat方法不用修改,其他的代码如下:
public void routePlansearchInit() { routePlanSearch.drivingSearch(getSearchParams()); } private DrivingRoutePlanOption getSearchParams() { DrivingRoutePlanOption params=new DrivingRoutePlanOption(); Listnodes=new ArrayList (); params.from(PlanNode.withLocation(target1));//设置起点 params.to(PlanNode.withLocation(target));//终点 return params; } @Override public void onGetWalkingRouteResult(WalkingRouteResult result) { } @Override public void onGetTransitRouteResult(TransitRouteResult result) { } @Override public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) { } @Override public void onGetDrivingRouteResult(DrivingRouteResult result) { DrivingRouteOverlay overlay=new DrivingRouteOverlay(baiduMap); baiduMap.setOnMarkerClickListener(overlay); List routeLines=result.getRouteLines();//获取所有搜素路线,最优化路线在集合最前面 DrivingRouteLine drivingRouteLine=routeLines.get(0);//搜素结果覆盖的覆盖物 overlay.setData(drivingRouteLine); overlay.addToMap(); overlay.zoomToSpan(); } @Override public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) { } @Override public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) { }
基于百度地图的导航功能就算是实现了,其实代码大同小异。这里读者可以利用之前的文章调整起点与终点的坐标,改为输入式,也可以就定位功能确定当前位置坐标。
对于百度地图导航笔者技术有限,目前只能做到通过确定两点之间的纬经度来进行导航,我试过很多方法,想实现通过输入两点之间的地名来进行导航,这点还在探索当中,如果哪位朋友有办法能够告诉我,特别感谢,小弟邮箱[email protected]欢迎骚扰。