只说步骤
大牛们看着不要笑就好
主页面的布局里面就一个button
<Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/start_nav2_btn"android:textSize="18sp"android:padding="20dp"android:layout_gravity="center_horizontal"android:text="使用百度地图坐标一键导航"/>
第一步 :开始配置工程,需要的包都导进去;
带TTS的SDK
在新建的Android工程里的assets目录添加BaiduNaviSDK_Resource_vX_X_X.png和channel文件,在libs目录添加BaiduNaviSDK_vX_X_X.jar、android_api_1.1_forsdk.jar、galaxy_mini.jar、galaxy_mini_lbs.jar、lbsCoreSDK.jar,在libs/armeabi目录添加libapp_BaiduNaviApplib_vX_X.so、libejTTS.so、libCNPackage.so、liblocnaviSDK.so。
不带TTS的SDK
在新建的Android工程里的assets目录添加BaiduNaviSDK_Resource_vX_X_X.png和channel文件,在libs目录添加BaiduNaviSDK_vX_X_X.jar、android_api_1.1_forsdk.jar、galaxy_mini.jar、galaxy_mini_lbs.jar、lbsCoreSDK.jar,在libs/armeabi目录添加libapp_BaiduNaviApplib_vX_X.so、liblocnaviSDK.so。
注意事项
对于android_api_1.1_forsdk.jar,它是百度移动统计SDK的部分,如果开发者同样也使用了百度移动统计SDK,并且添加android_api_1.1_forsdk.jar到工程时候发生代码冲突,此时应该把android_api_1.1_forsdk.jar去除掉。
第二步:在Manifest中添加使用权限及Android版本支持;
导航需要的权限如下:
<!--使用导航功能所需权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCES_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
注意在AndroidManifest.xml中application标签下添加如下代码,”com.baidu.lbsapi.API_KEY”中放入开发者申请的的API Key , 以及service。
<meta-data android:name="com.baidu.lbsapi.API_KEY"
android:value="bEDE2Ut8zsrZGa4p9caq4LNU" /> //更换你的ak
<service android:name="com.baidu.navi.location.f" android:enabled="true"/>
第三步:配置导航页activity,这不是主页面
再新建一个activity,在Manifest中加入导航页的声明
<activity android:name=".BNavigatorActivity"
android:configChanges="orientation|screenSize|keyboard|keyboardHidden"/>
编写导航activity的代码 , ps:如果你懒得copy , 直接把百度demo里面的 BNavigatorActivity 复制到你的工程中
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //创建NmapView MapGLSurfaceView nMapView = BaiduNaviManager.getInstance().createNMapView(this); //创建导航视图 View navigatorView = BNavigator.getInstance().init(BNavigatorActivity.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() { // TODO Auto-generated method stub } })); } 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){ finish(); }else if(IBNavigatorListener.PAGE_JUMP_WHEN_ROUTE_PLAN_FAIL == jumpTiming){ finish(); } } @Override public void notifyGPSStatusData(int arg0) { // TODO Auto-generated method stub } @Override public void notifyLoacteData(LocData arg0) { // TODO Auto-generated method stub } @Override public void notifyNmeaData(String arg0) { // TODO Auto-generated method stub } @Override public void notifySensorData(SensorData arg0) { // TODO Auto-generated method stub } @Override public void notifyStartNav() { // TODO Auto-generated method stub BaiduNaviManager.getInstance().dismissWaitProgressDialog(); } @Override public void notifyViewModeChanged(int arg0) { // TODO Auto-generated method stub } };@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(); }
第四步:在主页面输入起始点以及终点 ,并发起导航
1,先在onCreate方法中
BaiduNaviManager.getInstance().initEngine(this, getSdcardDir(),mNaviEngineInitListener, new LBSAuthManagerListener() {@Overridepublic void onAuthResult(int status, String msg) {String str = null;if (0 == status) {str = "key校验成功!";} else {str = "key校验失败, " + msg;}Toast.makeText(RouteGuideDemo.this, str,Toast.LENGTH_LONG).show();}});findViewById(R.id.start_nav2_btn).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {if (mViaPoints.size() == 0) {launchNavigator();} }});
2,继续在主页面copy一下代码
/*** 指定导航起终点启动GPS导航.起终点可为多种类型坐标系的地理坐标。* 前置条件:导航引擎初始化成功*/private void launchNavigator(){//这里给出一个起终点示例,实际应用中可以通过POI检索、外部POI来源等方式获取起终点坐标BNaviPoint startPoint = new BNaviPoint(116.307854,40.055878,"百度大厦", BNaviPoint.CoordinateType.BD09_MC);BNaviPoint endPoint = new BNaviPoint(116.403875,39.915168,"北京天安门", BNaviPoint.CoordinateType.BD09_MC);BaiduNaviManager.getInstance().launchNavigator(this,startPoint, //起点(可指定坐标系)endPoint, //终点(可指定坐标系)NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME, //算路方式true, //真实导航BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, //在离线策略new OnStartNavigationListener() { //跳转监听@Overridepublic void onJumpToNavigator(Bundle configParams) {Intent intent = new Intent(RouteGuideDemo.this, BNavigatorActivity.class);intent.putExtras(configParams);startActivity(intent);}@Overridepublic void onJumpToDownloader() {}});}
private boolean mIsEngineInitSuccess = false;
private NaviEngineInitListener mNaviEngineInitListener = new NaviEngineInitListener() {public void engineInitSuccess() {mIsEngineInitSuccess = true;}public void engineInitStart() {}public void engineInitFail() {}};private String getSdcardDir() {if (Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {return Environment.getExternalStorageDirectory().toString();}return null;}
至于起始点以及终点是要经纬度的 , launchNavigator() 直接把这个方法的经纬度换成起始点的就好
/*** 指定导航起终点启动GPS导航.起终点可为多种类型坐标系的地理坐标。* 前置条件:导航引擎初始化成功*/private void launchNavigator2(){//这里给出一个起终点示例,实际应用中可以通过POI检索、外部POI来源等方式获取起终点坐标BNaviPoint startPoint = new BNaviPoint(116.307854,40.055878,"百度大厦", BNaviPoint.CoordinateType.BD09_MC);BNaviPoint endPoint = new BNaviPoint(116.403875,39.915168,"北京天安门", BNaviPoint.CoordinateType.BD09_MC);BaiduNaviManager.getInstance().launchNavigator(this,startPoint, //起点(可指定坐标系)endPoint, //终点(可指定坐标系)NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME, //算路方式true, //真实导航BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, //在离线策略new OnStartNavigationListener() { //跳转监听@Overridepublic void onJumpToNavigator(Bundle configParams) {Intent intent = new Intent(RouteGuideDemo.this, BNavigatorActivity.class);intent.putExtras(configParams);startActivity(intent);}@Overridepublic void onJumpToDownloader() {}});}private NaviEngineInitListener mNaviEngineInitListener = new NaviEngineInitListener() {public void engineInitSuccess() {mIsEngineInitSuccess = true;}public void engineInitStart() {}public void engineInitFail() {}};private String getSdcardDir() {if (Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {return Environment.getExternalStorageDirectory().toString();}return null;}