版权声明:本文为博主老胡(laohu)原创文章,未经允许不得转载(欢迎访问个人独立博客:ittiger.cn)
-
首先要先在AndroidManifest.xml中注册导航服务
<service Android:name="com.baidu.navi.location.f" android:enabled="true"></service>
-
添加相关的权限,我也不知道哪些是必须的,我直接把Demo里面的相关权限配置全部拉过来了
-
在导航之前要先调用
BaiduNaviManager.getInstance().initEngine
对导航引擎进行初始化。可以通过NaviEngineInitListener
接口检查导航引擎初始化是否成功,同时通过LBSAuthManagerListener
接口对API-Key进行Key校验是否成功,status结果为0代表key校验成功。 -
必须将Demo中assert目录下的两个文件
BaiduNaviSDK_Resource_v1_0_0.png
,channel
拷贝到自己项目的assert目录下去,否则导航引擎无法初始化成功。 -
在导航引擎初始化成功后,调用导航功能前调用
BaiduNaviManager.getInstance().checkEngineStatus(context)
检测导航引擎是否初始化成功。 -
调用
BaiduNaviManager.getInstance().launchNavigator
开始导航
相关代码如下:
//导航
public static void launchNavi(final Activity activity, List<BNaviPoint> points ){
if(points.size() < 2) {
Toast.makeText(activity, "数据不合法,暂时无法导航", Toast.LENGTH_SHORT).show();
return;
}
BaiduNaviManager.getInstance().launchNavigator(activity,
points, //路线点列表
NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME, //算路方式
false, //真实导航
BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, //在离线策略
new OnStartNavigationListener() { //跳转监听
@Override
public void onJumpToNavigator(Bundle configParams) {
Intent intent = new Intent(activity, NaviBdMapActivity.class);
intent.putExtras(configParams);
activity.startActivityForResult(intent,REQUESTCODE_NAVI_FINISH);
}
@Override
public void onJumpToDownloader() {
}
}
);
}
//初始化引擎
public static void initNaviEngine(final Activity activity) {
BaiduNaviManager.getInstance().initEngine(activity, Utils.getRootPath(),
new SPMNaviEngineInitListener(), new LBSAuthManagerListener() {
@Override
public void onAuthResult(int status, String msg) {
String str = null;
if (0 == status) {
str = "key校验成功!";
} else {
str = "key校验失败, " + msg;
}
}
}
);
}
private static class SPMNaviEngineInitListener implements NaviEngineInitListener {
public void engineInitSuccess() {
Log.i("Navi", "导航初始化成功");
}
public void engineInitStart() {
}
public void engineInitFail() {
Log.i("Navi", "导航初始化失败");
}
};
`
public class NaviBdMapActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 创建NmapView
if (Build.VERSION.SDK_INT < 14) {
BaiduNaviManager.getInstance().destroyNMapView();
}
MapGLSurfaceView nMapView = BaiduNaviManager.getInstance().createNMapView(this);
// 创建导航视图
View navigatorView = BNavigator.getInstance().init(
NaviBdMapActivity.this, getIntent().getExtras(),
nMapView);
// 填充视图
setContentView(navigatorView);
BNavigator.getInstance().setListener(mBNavigatorListener);
BNavigator.getInstance().startNav();
// 初始化TTS. 开发者也可以使用独立TTS模块,不用使用导航SDK提供的TTS
BNTTSPlayer.initPlayer();
// 设置TTS播放回调
BNavigatorTTSPlayer.setTTSPlayerListener(new IBNTTSPlayerListener() {
@Override
public int playTTSText(String arg0, int arg1) {
// 开发者可以使用其他TTS的API
return BNTTSPlayer.playTTSText(arg0, arg1);
}
@Override
public void phoneHangUp() {
// 手机挂断
}
@Override
public void phoneCalling() {
// 通话中
}
@Override
public int getTTSState() {
// 开发者可以使用其他TTS的API,
return BNTTSPlayer.getTTSState();
}
});
BNRoutePlaner.getInstance().setObserver(
new RoutePlanObserver(this, new IJumpToDownloadListener() {
@Override
public void onJumpToDownloadOfflineData() {
}
}));
}
private IBNavigatorListener mBNavigatorListener = new IBNavigatorListener() {
@Override
public void onYawingRequestSuccess() {
// TODO 偏航请求成功
}
@Override
public void onYawingRequestStart() {
// TODO 开始偏航请求
}
@Override
public void onPageJump(int jumpTiming, Object arg) {
// TODO 页面跳转回调
if (IBNavigatorListener.PAGE_JUMP_WHEN_GUIDE_END == jumpTiming) {
finishActivity();
} else if (IBNavigatorListener.PAGE_JUMP_WHEN_ROUTE_PLAN_FAIL == jumpTiming) {
finishActivity();
}
}
@Override
public void notifyGPSStatusData(int arg0) {
}
@Override
public void notifyLoacteData(LocData arg0) {
}
@Override
public void notifyNmeaData(String arg0) {
}
@Override
public void notifySensorData(SensorData arg0) {
}
@Override
public void notifyStartNav() {
BaiduNaviManager.getInstance().dismissWaitProgressDialog();
}
@Override
public void notifyViewModeChanged(int arg0) {
}
};
@Override
public void onResume() {
BNavigator.getInstance().resume();
super.onResume();
BNMapController.getInstance().onResume();
};
@Override
public void onPause() {
BNavigator.getInstance().pause();
super.onPause();
BNMapController.getInstance().onPause();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
BNavigator.getInstance().onConfigurationChanged(newConfig);
super.onConfigurationChanged(newConfig);
}
public void onBackPressed() {
BNavigator.getInstance().onBackPressed();
}
@Override
public void onDestroy() {
BNavigator.destory();
BNRoutePlaner.getInstance().setObserver(null);
super.onDestroy();
}
public void finishActivity() {
finish();
overridePendingTransition(R.anim.in_from_right,R.anim.out_to_left);
}
}