步行导航
1、导航SDK配置
(一)导航SDK核心集成
现将onsdk_all.arr和NaviTis.arr复制libs里面
有两种方法可以在Android studio引用.arr包
方法一:
导航SDK核心已经打包为一个独立的aar,命名为onsdk_all.aar,可以将其以aar module引入集成APP project,引入后,即可引用导航SDK相关类进行开发。
File->new Moudle->Import .jar/.arr Package
选择包的路径
然后在添加对该moudle的引用
implementation project(':onsdk_all')
方法二:
在app目录下的gradle中添加如下代码:
android{
repositories {
jcenter()
maven { url 'https://maven.google.com' }
flatDir {
dirs 'libs'
}
}
}
dependencies {
implementation(name: 'onsdk_all', ext: 'aar')
}
以上两种方法都可以成功引用.arr包
2、导航SDK依赖库集成
将下载的BaiduNavi_AndroidSDK复制到libs目录,并添加引用
3、导航SDK自带TTS集成
导航SDK自带TTS能力已经打包为一个独立的aar,命名为NaviTts.aar,可以将其以aar module引入集成APP project,引入后,可以利用导航SDK提供的TTS接口进行初始化。当不需要导航SDK自带TTS时,可以不进行集成,此时需要利用导航SDK支持外部TTS的方式进行TTS能力初始化。方法同导航SDK配置类似
4、此时可以build一下,会报Annotation processors must be explicitly declared now. 这个错误
在app的build.gradle文件中添加includeCompileClasspath参数:
android {
defaultConfig {
...
javaCompileOptions{
annotationProcessorOptions {
includeCompileClasspath true
}
}
}
...
}
错误解决,在运行一次
会报包冲突这个错,具体解决方法看下一篇文章
5、SDK和TTS授权申请配置
SDK之前已经配置过了,AppID其实就是应用编号,AK那里可以看到
6、导航方式界面的布局
<!--
~ Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<Button
android:id="@+id/wgsNaviBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="国际经纬度坐标" />
<Button
android:id="@+id/gcjNaviBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="国测局坐标" />
<Button
android:id="@+id/bdmcNaviBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="百度墨卡托坐标" />
<Button
android:id="@+id/mDb06llNaviBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="百度经纬度坐标" />
<Button
android:id="@+id/mGotoSettingsBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="导航设置" />
</LinearLayout>
7、引擎初始化
BaiduNaviManagerFactory.getBaiduNaviManager().init(this,
mSDCardPath, APP_FOLDER_NAME, new IBaiduNaviManager.INaviInitListener() {
@Override
public void onAuthResult(int status, String msg) {
Log.v("zjzz",msg);
String result;
if (0 == status) {
result = "key校验成功!";
} else {
result = "key校验失败, " + msg;
}
Toast.makeText(DemoMainActivity.this, result, Toast.LENGTH_LONG).show();
}
@Override
public void initStart() {
Toast.makeText(DemoMainActivity.this, "百度导航引擎初始化开始", Toast.LENGTH_SHORT).show();
}
@Override
public void initSuccess() {
Toast.makeText(DemoMainActivity.this, "百度导航引擎初始化成功", Toast.LENGTH_SHORT).show();
hasInitSuccess = true;
// 初始化tts
initTTS();
}
@Override
public void initFailed() {
String a;
a="aaaa";
Toast.makeText(DemoMainActivity.this, "百度导航引擎初始化失败", Toast.LENGTH_SHORT).show();
}
});
8、发起算路
private void routeplanToNavi(final int coType) {
if (!hasInitSuccess) {
Toast.makeText(DemoMainActivity.this, "还未初始化!", Toast.LENGTH_SHORT).show();
}
BNRoutePlanNode sNode = new BNRoutePlanNode(116.30142, 40.05087, "百度大厦", "百度大厦", coType);
BNRoutePlanNode eNode = new BNRoutePlanNode(116.39750, 39.90882, "北京天安门", "北京天安门", coType);
switch (coType) {
case BNRoutePlanNode.CoordinateType.GCJ02: {
sNode = new BNRoutePlanNode(116.30142, 40.05087, "百度大厦", "百度大厦", coType);
eNode = new BNRoutePlanNode(116.39750, 39.90882, "北京天安门", "北京天安门", coType);
break;
}
case BNRoutePlanNode.CoordinateType.WGS84: {
sNode = new BNRoutePlanNode(116.300821, 40.050969, "百度大厦", "百度大厦", coType);
eNode = new BNRoutePlanNode(116.397491, 39.908749, "北京天安门", "北京天安门", coType);
break;
}
case BNRoutePlanNode.CoordinateType.BD09_MC: {
sNode = new BNRoutePlanNode(12947471, 4846474, "百度大厦", "百度大厦", coType);
eNode = new BNRoutePlanNode(12958160, 4825947, "北京天安门", "北京天安门", coType);
break;
}
case BNRoutePlanNode.CoordinateType.BD09LL: {
sNode = new BNRoutePlanNode(116.30784537597782, 40.057009624099436, "百度大厦", "百度大厦", coType);
eNode = new BNRoutePlanNode(116.40386525193937, 39.915160800132085, "北京天安门", "北京天安门", coType);
break;
}
default:
break;
}
mStartNode = sNode;
List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
list.add(sNode);
list.add(eNode);
BaiduNaviManagerFactory.getRoutePlanManager().routeplanToNavi(
list,
IBNRoutePlanManager.RoutePlanPreference.ROUTE_PLAN_PREFERENCE_DEFAULT,
null,
new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_START:
Toast.makeText(DemoMainActivity.this, "算路开始", Toast.LENGTH_SHORT)
.show();
break;
case IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_SUCCESS:
Toast.makeText(DemoMainActivity.this, "算路成功", Toast.LENGTH_SHORT)
.show();
break;
case IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_FAILED:
Toast.makeText(DemoMainActivity.this, "算路失败", Toast.LENGTH_SHORT)
.show();
break;
case IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_TO_NAVI:
Toast.makeText(DemoMainActivity.this, "算路成功准备进入导航", Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent(DemoMainActivity.this,
TTSDemoGuideActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable(ROUTE_PLAN_NODE, mStartNode);
intent.putExtras(bundle);
startActivity(intent);
break;
default:
// nothing
break;
}
}
});
}
9、开始导航
新建诱导Activity,创建诱导View,开始导航
mRouteGuideManager = BaiduNaviManagerFactory.getRouteGuideManager();
View view = mRouteGuideManager.onCreate(this, mOnNavigationListener);
if (view != null) {
setContentView(view);
}
10、正确管理WalkNavigateHelper对象生命周期。
@Override
protected void onStart() {
super.onStart();
mRouteGuideManager.onStart();
}
@Override
protected void onResume() {
super.onResume();
mRouteGuideManager.onResume();
// 自定义图层
showOverlay();
}
protected void onPause() {
super.onPause();
mRouteGuideManager.onPause();
}
@Override
protected void onStop() {
super.onStop();
mRouteGuideManager.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mRouteGuideManager.onDestroy(false);
EventHandler.getInstance().disposeDialog();
}
@Override
public void onBackPressed() {
mRouteGuideManager.onBackPressed(false, true);
}
11、运行工程,可以成功进行导航