您当前的位置: Android导航SDK > 开发指南 > 路径规划和导航
路径规划和导航

简述

通过输入起点,终点或途经点,可以发起路线规划,并进入导航。算路支持多种坐标系(参考BNRoutePlanNode.CoordinateType),和多种偏好(参考RoutePlanPreference)

坐标说明

百度Android导航SDK接口的输入坐标支持GCJ02、WGS84、bd09ll、bd09mc,使用时请根据需要设置好相应参数即可,无需坐标转换。

相应的坐标系说明请参考坐标转换

发起算路

算路设置起、终点,算路偏好,是否模拟导航等参数,然后在回调函数中设置跳转至诱导。

 
private void routeplanToNavi(CoordinateType coType) {
    mCoordinateType = coType;
    if (!hasInitSuccess) {
        Toast.makeText(BNDemoMainActivity.this, "还未初始化!", Toast.LENGTH_SHORT).show();
    }
    // 权限申请
    if (android.os.Build.VERSION.SDK_INT >= 23) {
        // 保证导航功能完备
        if (!hasCompletePhoneAuth()) {
            if (!hasRequestComAuth) {
                hasRequestComAuth = true;
                this.requestPermissions(authComArr, authComRequestCode);
                return;
            } else {
                Toast.makeText(BNDemoMainActivity.this, "没有完备的权限!", Toast.LENGTH_SHORT).show();
            }
        }
 
    }
    BNRoutePlanNode sNode = null;
    BNRoutePlanNode eNode = null;
    switch (coType) {
        case GCJ02: {
            sNode = new BNRoutePlanNode(116.30142, 40.05087, "百度大厦", null, coType);
            eNode = new BNRoutePlanNode(116.39750, 39.90882, "北京天安门", null, coType);
            break;
        }
        case WGS84: {
            sNode = new BNRoutePlanNode(116.300821, 40.050969, "百度大厦", null, coType);
            eNode = new BNRoutePlanNode(116.397491, 39.908749, "北京天安门", null, coType);
            break;
        }
        case BD09_MC: {
            sNode = new BNRoutePlanNode(12947471, 4846474, "百度大厦", null, coType);
            eNode = new BNRoutePlanNode(12958160, 4825947, "北京天安门", null, coType);
            break;
        }
        case BD09LL: {
            sNode = new BNRoutePlanNode(116.30784537597782, 40.057009624099436, "百度大厦", null, coType);
            eNode = new BNRoutePlanNode(116.40386525193937, 39.915160800132085, "北京天安门", null, coType);
            break;
        }
        default:
            ;
        }
        if (sNode != null && eNode != null) {
            List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
            list.add(sNode);
            list.add(eNode);
            BaiduNaviManager.getInstance().launchNavigator(this, list, 1, true, new DemoRoutePlanListener(sNode));
        }
}
 
public class DemoRoutePlanListener implements RoutePlanListener {
 
    private BNRoutePlanNode mBNRoutePlanNode = null;
 
    public DemoRoutePlanListener(BNRoutePlanNode node) {
        mBNRoutePlanNode = node;
    }
 
    @Override
    public void onJumpToNavigator() {
        /*
         * 设置途径点以及resetEndNode会回调该接口
         */
 
        for (Activity ac : activityList) {
 
            if (ac.getClass().getName().endsWith("BNDemoGuideActivity")) {
 
                return;
            }
        }
        Intent intent = new Intent(BNDemoMainActivity.this, BNDemoGuideActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ROUTE_PLAN_NODE, (BNRoutePlanNode) mBNRoutePlanNode);
        intent.putExtras(bundle);
        startActivity(intent);
 
    }
 
    @Override
    public void onRoutePlanFailed() {
        // TODO Auto-generated method stub
        Toast.makeText(BNDemoMainActivity.this, "算路失败", Toast.LENGTH_SHORT).show();
    }
}

开始导航

创建诱导View,并接收回调事件。在activity生命周期内调用诱导BNRouteGuiderManager对应的生命周期函数。提供了通用的获取View的接口,并兼容之前的接口。

 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 
    BNDemoMainActivity.activityList.add(this);
    createHandler();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    }
    View view = null;
    if (useCommonInterface) {
        //使用通用接口
        mBaiduNaviCommonModule = NaviModuleFactory.getNaviModuleManager().getNaviCommonModule(
                NaviModuleImpl.BNaviCommonModuleConstants.ROUTE_GUIDE_MODULE, this,
                BNaviBaseCallbackModel.BNaviBaseCallbackConstants.CALLBACK_ROUTEGUIDE_TYPE, mOnNavigationListener);
        if(mBaiduNaviCommonModule != null) {
            mBaiduNaviCommonModule.onCreate();
            view = mBaiduNaviCommonModule.getView();
        }
 
    } else {
        //使用传统接口
        view = BNRouteGuideManager.getInstance().onCreate(this,mOnNavigationListener);
    }
 
 
    if (view != null) {
        setContentView(view);
    }
 
    Intent intent = getIntent();
    if (intent != null) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            mBNRoutePlanNode = (BNRoutePlanNode) bundle.getSerializable(BNDemoMainActivity.ROUTE_PLAN_NODE);
 
        }
    }
}
 
@Override
protected void onResume() {
    super.onResume();
    if(useCommonInterface) {
        if(mBaiduNaviCommonModule != null) {
            mBaiduNaviCommonModule.onResume();
        }
    } else {
        BNRouteGuideManager.getInstance().onResume();
    }
 
 
    if (hd != null) {
        hd.sendEmptyMessageAtTime(MSG_SHOW, 2000);
    }
}
 
protected void onPause() {
    super.onPause();
 
    if(useCommonInterface) {
        if(mBaiduNaviCommonModule != null) {
            mBaiduNaviCommonModule.onPause();
        }
    } else {
        BNRouteGuideManager.getInstance().onPause();
    }
 
};
 
@Override
protected void onDestroy() {
    super.onDestroy();
    if(useCommonInterface) {
        if(mBaiduNaviCommonModule != null) {
            mBaiduNaviCommonModule.onDestroy();
        }
    } else {
        BNRouteGuideManager.getInstance().onDestroy();
    }
    BNDemoMainActivity.activityList.remove(this);
 
}
 
@Override
protected void onStop() {
    super.onStop();
    if(useCommonInterface) {
        if(mBaiduNaviCommonModule != null) {
            mBaiduNaviCommonModule.onStop();
        }
    } else {
        BNRouteGuideManager.getInstance().onStop();
    }
 
}
 
@Override
public void onBackPressed() {
    if(useCommonInterface) {
        if(mBaiduNaviCommonModule != null) {
            mBaiduNaviCommonModule.onBackPressed(false);
        }
    } else {
        BNRouteGuideManager.getInstance().onBackPressed(false);
    }
}
 
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(useCommonInterface) {
        if(mBaiduNaviCommonModule != null) {
            mBaiduNaviCommonModule.onConfigurationChanged(newConfig);
        }
    } else {
        BNRouteGuideManager.getInstance().onConfigurationChanged(newConfig);
    }
 
};

导航中部分设置项的设置

private void initSetting(){
    BNaviSettingManager.setDayNightMode(BNaviSettingManager.DayNightMode.DAY_NIGHT_MODE_DAY);
    BNaviSettingManager.setShowTotalRoadConditionBar(BNaviSettingManager.PreViewRoadCondition.ROAD_CONDITION_BAR_SHOW_ON);
    BNaviSettingManager.setVoiceMode(BNaviSettingManager.VoiceMode.Veteran);	    
    BNaviSettingManager.setPowerSaveMode(BNaviSettingManager.PowerSaveMode.DISABLE_MODE);
    BNaviSettingManager.setRealRoadCondition(BNaviSettingManager.RealRoadCondition.NAVI_ITS_ON);
}

播报模式设置界面如下图:

voicemode.png

导航中重新设置终点并直接算路导航

导航中如果需要重新设置终点时,可以调用重置终点接口。

BNRouteGuideManager.getInstance().resetEndNodeInNavi(endNode);

强制退出导航

导航中需要强制退出时,调用下面接口,该接口将不会显示对话框。

BNRouteGuideManager.getInstance().forceQuitNaviWithoutDialog();

内部TTS播报状态获取

/**
 * 内部TTS播报状态回传handler
 */
private Handler ttsHandler = new Handler() {
    public void handleMessage(Message msg) {
        int type = msg.what;
        switch (type) {
            case BaiduNaviManager.TTSPlayMsgType.PLAY_START_MSG: {
                 showToastMsg("Handler : TTS play start");
                break;
            }
            case BaiduNaviManager.TTSPlayMsgType.PLAY_END_MSG: {
                 showToastMsg("Handler : TTS play end");
                break;
            }
            default :
                break;
        }
    }
};
 
/**
 * 内部TTS播报状态回调接口
 */
private BaiduNaviManager.TTSPlayStateListener ttsPlayStateListener = new BaiduNaviManager.TTSPlayStateListener() {
 
    @Override
    public void playEnd() {
//            showToastMsg("TTSPlayStateListener : TTS play end");
    }
 
    @Override
    public void playStart() {
//            showToastMsg("TTSPlayStateListener : TTS play start");
    }
};
 
public void showToastMsg(final String msg) {
    BNDemoMainActivity.this.runOnUiThread(new Runnable() {
 
        @Override
        public void run() {
            Toast.makeText(BNDemoMainActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });
}