实现步骤
- 首先要先在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
开始导航。
关键代码
相关代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 public static void launchNavi(final Activity activity, List 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() {
}
}
);
}
/**
* 初始化导航引擎
* Author: hyl
* Time: 2015-7-9下午4:56:18
* @param activity
*/
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);
}
}
原创文章,转载请出处注明。
下面是我的个人公众号,欢迎关注交流