算路、选路及上传路线,为了保持司机端和客户端路线一致,该部分接口由司乘同显导航sdk提供。
若乘客端业务需要实现乘客端选择算路路线,乘客端需要集成司乘同显导航sdk。
示例代码如下:
// 设置起点位置bnrouteplannode snode =newbnrouteplannode(116.43370,39.92144,"起点",null,bnrouteplannode.coordinatetype.gcj02);// 设置终点位置bnrouteplannode enode =newbnrouteplannode(116.30654,40.04162,"终点",null,bnrouteplannode.coordinatetype.gcj02);list<bnrouteplannode> list =newarraylist<>();list.add(snode);list.add(enode);baidunavimanagerfactory.getrouteplanmanager().routeplan(list,ibnrouteplanmanager.routeplanpreference.route_plan_preference_default,null,newhandler(looper.getmainlooper()){@overridepublicvoidhandlemessage(message msg){switch(msg.what){caseibnrouteplanmanager.msg_navi_route_plan_start:toast.maketext(getapplicationcontext(),"算路开始",toast.length_short).show();break;caseibnrouteplanmanager.msg_navi_route_plan_success:toast.maketext(getapplicationcontext(),"算路成功",toast.length_short).show();// 通过handler移除算路请求baidunavimanagerfactory.getrouteplanmanager().removerequestbyhandler(this);break;caseibnrouteplanmanager.msg_navi_route_plan_failed:toast.maketext(getapplicationcontext(),"算路失败",toast.length_short).show();// 通过handler移除算路请求baidunavimanagerfactory.getrouteplanmanager().removerequestbyhandler(this);break;default:// nothingbreak;}}});
发起算路后,在算路成功的回调里,能够获取3条路线信息,每一条路线信息包含路线长度、路线驾车时间、路线红绿灯个数等信息。以下介绍重要方法调用,完整代码请参考司机端demo中demorouteresultfragment中的使用。
示例代码如下:
bundle bundle =(bundle) msg.obj;mrouteplanitems = bundle.getparcelablearraylist(bnavicommonparams.bnrouteinfokey.info_tab);if(mrouteplanitems !=null){if(mrouteplanitems.size()>0&& mrouteplanitems.get(0)!=null){string puslabelname = mrouteplanitems.get(0).getpuslabelname();// 路线驾车时间int time =(int)(mrouteplanitems.get(0).getpasstime()/60);// 路线长度int lenth =(int)(mrouteplanitems.get(0).getlength()/1000.0f);}if(mrouteplanitems.size()>1&& mrouteplanitems.get(1)!=null){string puslabelname = mrouteplanitems.get(0).getpuslabelname();// 路线驾车时间int time =(int)(mrouteplanitems.get(0).getpasstime()/60);// 路线长度int lenth =(int)(mrouteplanitems.get(0).getlength()/1000.0f);}if(mrouteplanitems.size()>2&& mrouteplanitems.get(2)!=null){string puslabelname = mrouteplanitems.get(0).getpuslabelname();// 路线驾车时间int time =(int)(mrouteplanitems.get(0).getpasstime()/60);// 路线长度int lenth =(int)(mrouteplanitems.get(0).getlength()/1000.0f);}}
根据回调的3条路线数据中的路线id指定要选择的路线
示例代码:
// 根据路线id选择路线baidunavimanagerfactory.getrouteresultmanager().selectroute(mrouteplanitems.get(0).getroutid());
确定使用选中的路线并上传到服务端,用于司机端发起算路导航。
示例代码:
bnorderinfo info =newbnorderinfo();info.startnode=newbnrouteplannode(116.45932,39.91885,"起点",null,bnrouteplannode.coordinatetype.gcj02);info.pickupnode=((synccustomdemo)getactivity()).msnode;info.endnode=((synccustomdemo)getactivity()).menode;info.orderid= morderid;info.driverid="3333";info.companyserverid="88888888";info.orderstate=bnorderstate.send_passenger;baidunavimanagerfactory.getrouteresultmanager().uploadrouteinfo(info,newibnrequestlistener(){@overridepublicvoidonrequestresult(final int i,string s, final string s1){if(getactivity()==null){return;}getactivity().runonuithread(newrunnable(){@overridepublicvoidrun(){if(i !=200){toast.maketext(getactivity(),"上传失败",toast.length_long).show();return;}try{jsonobject object =newjsonobject(s1);if(object.getint("status")!=0){toast.maketext(getactivity(),"上传失败",toast.length_long).show();}else{toast.maketext(getactivity(),"上传成功",toast.length_long).show();}}catch(jsonexception e){e.printstacktrace();}}});}});
选路页面的生命周期管理
示例代码:
@overridepublicviewoncreateview(@nonnulllayoutinflater inflater, @nullableviewgroup container,@nullablebundle savedinstancestate){baidunavimanagerfactory.getrouteresultmanager().oncreate(getactivity());view view = inflater.inflate(r.layout.fragment_route_result, container,false);mlayout = view.findviewbyid(r.id.container);baidunavimanagerfactory.getmapmanager().attach(mlayout);return view;}@overridepublicvoidonpause(){super.onpause();baidunavimanagerfactory.getmapmanager().onpause();baidunavimanagerfactory.getrouteresultmanager().onpause();}@overridepublicvoidonresume(){super.onresume();baidunavimanagerfactory.getmapmanager().onresume();baidunavimanagerfactory.getrouteresultmanager().onresume();}@overridepublicvoidondestroy(){super.ondestroy();baidunavimanagerfactory.getmapmanager().detach(mlayout);baidunavimanagerfactory.getrouteresultmanager().ondestroy();}
上一篇
下一篇
本篇文章对您是否有帮助?