算路、选路及上传路线,为了保持司机端和客户端路线一致,该部分接口由司乘同显导航SDK提供。
若乘客端业务需要实现乘客端选择算路路线,乘客端需要集成司乘同显导航SDK。
示例代码如下:
// 设置起点位置BNRoutePlanNode sNode = new BNRoutePlanNode(116.43370,39.92144, "起点", null, BNRoutePlanNode.CoordinateType.GCJ02);// 设置终点位置BNRoutePlanNode eNode = new BNRoutePlanNode( 116.30654,40.04162, "终点", null, BNRoutePlanNode.CoordinateType.GCJ02);List<BNRoutePlanNode> list = new ArrayList<>();list.add(sNode);list.add(eNode);BaiduNaviManagerFactory.getRoutePlanManager().routeplan(list,IBNRoutePlanManager.RoutePlanPreference.ROUTE_PLAN_PREFERENCE_DEFAULT,null,new Handler(Looper.getMainLooper()) {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_START:Toast.makeText(getApplicationContext(), "算路开始", Toast.LENGTH_SHORT).show();break;case IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_SUCCESS:Toast.makeText(getApplicationContext(), "算路成功", Toast.LENGTH_SHORT).show();// 通过handler移除算路请求BaiduNaviManagerFactory.getRoutePlanManager().removeRequestByHandler(this);break;case IBNRoutePlanManager.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 = new BNOrderInfo();info.startNode = new BNRoutePlanNode(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, new IBNRequestListener() {@Overridepublic void onRequestResult(final int i, String s, final String s1) {if (getActivity() == null) {return;}getActivity().runOnUiThread(new Runnable() {@Overridepublic void run() {if (i != 200) {Toast.makeText(getActivity(), "上传失败", Toast.LENGTH_LONG).show();return;}try {JSONObject object = new JSONObject(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();}}});}});
选路页面的生命周期管理
示例代码:
@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle 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;}@Overridepublic void onPause() {super.onPause();BaiduNaviManagerFactory.getMapManager().onPause();BaiduNaviManagerFactory.getRouteResultManager().onPause();}@Overridepublic void onResume() {super.onResume();BaiduNaviManagerFactory.getMapManager().onResume();BaiduNaviManagerFactory.getRouteResultManager().onResume();}@Overridepublic void onDestroy() {super.onDestroy();BaiduNaviManagerFactory.getMapManager().detach(mLayout);BaiduNaviManagerFactory.getRouteResultManager().onDestroy();}
上一篇
下一篇
本篇文章对您是否有帮助?