赞
踩
// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
SDKInitializer.setAgreePrivacy(this,true);
// 默认本地个性化地图初始化方法
SDKInitializer.initialize(Utils.getContext());
//自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
//包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
SDKInitializer.setCoordType(CoordType.BD09LL);
LocationClient.setAgreePrivacy(true);
private WalkNavigateHelper helper = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initPermissions(); // 初始化权限
setContentView(R.layout.activity_main); // 填充视图
initView(); // 初始化地图
// 点击按钮开始导航
ImageButton imageButton = findViewById(R.id.nav_btn);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 初始化步行导航
startBikeNavi();
}
});
}
// 初始化权限
private void initPermissions() {
if (Build.VERSION.SDK_INT >= 23) {
ArrayList<String> permissionsList = new ArrayList<>();
String[] permissions = {
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
for (String perm : permissions) {
if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(perm)) {
permissionsList.add(perm);
// 进入到这里代表没有权限.
}
if (!permissionsList.isEmpty()) {
String[] strings = new String[permissionsList.size()];
requestPermissions(permissionsList.toArray(strings), 0);
}
}
}
}
/**
* @Description: 引擎初始化
* @Author: LY
*/
private void startBikeNavi() {
Log.d("=========", "startBikeNavi");
try {
helper = WalkNavigateHelper.getInstance();
helper.initNaviEngine(this, new IWEngineInitListener() {
@Override
public void engineInitSuccess() {
Log.d("=========", "BikeNavi engineInitSuccess引擎初始化成功");
startWebNavi();
}
@Override
public void engineInitFail() {
Log.d("=========", "BikeNavi engineInitFail");
BikeNavigateHelper.getInstance().unInitNaviEngine();
}
});
} catch (Exception e) {
Log.d("=========", "startBikeNavi Exception");
e.printStackTrace();
}
}
/**
* @Description: 启动百度地图导航(Web)
* @Author: LY
*/
private void startWebNavi() {
//起终点位置
LatLng walkStartNode = new LatLng(40.047416, 116.312143);
LatLng walkEndNode = new LatLng(40.048424, 116.313513);
WalkRouteNodeInfo startNode = new WalkRouteNodeInfo();
startNode.setLocation(walkStartNode);
WalkRouteNodeInfo endNode = new WalkRouteNodeInfo();
endNode.setLocation(walkEndNode);
// 官网
//构造WalkNaviLaunchParam
WalkNaviLaunchParam walkParam = new WalkNaviLaunchParam().startNodeInfo(startNode).endNodeInfo(endNode);
//获取WalkNavigateHelper实例 //发起算路
helper.routePlanWithRouteNode(walkParam, new IWRoutePlanListener() {
@Override
public void onRoutePlanStart() {
Log.d("=======", "WalkNavi onRoutePlanStart开始步行导航");
}
@Override
public void onRoutePlanSuccess() {
Log.d("========", "onRoutePlanSuccess算路成功");
Intent intent = new Intent();
intent.setClass(getContext(), WNaviGuideActivity.class);
startActivity(intent);
}
@Override
public void onRoutePlanFail(WalkRoutePlanError error) {
Log.d("Walk", "WalkNavi onRoutePlanFail");
}
});
}
package com.example.locationapp;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.baidu.mapapi.walknavi.WalkNavigateHelper;
import com.baidu.mapapi.walknavi.adapter.IWNaviStatusListener;
import com.baidu.mapapi.walknavi.adapter.IWRouteGuidanceListener;
import com.baidu.mapapi.walknavi.adapter.IWTTSPlayer;
import com.baidu.mapapi.walknavi.model.RouteGuideKind;
import com.baidu.platform.comapi.walknavi.WalkNaviModeSwitchListener;
import com.baidu.platform.comapi.walknavi.widget.ArCameraView;
public class WNaviGuideActivity extends Activity {
private final static String TAG = WNaviGuideActivity.class.getSimpleName();
private WalkNavigateHelper mNaviHelper;
private boolean isPreSPEAKtotal = true;
private String orient = "";
@Override
protected void onDestroy() {
super.onDestroy();
mNaviHelper.quit();
}
@Override
protected void onResume() {
super.onResume();
mNaviHelper.resume();
}
@Override
protected void onPause() {
super.onPause();
mNaviHelper.pause();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x001) {
ChangeState();
handler.sendEmptyMessageDelayed(0x001, 45000);
}
if (msg.what == 0x002) {
handler.sendEmptyMessageDelayed(0x002, 30000);
}
if (msg.what == 0x003) {
startActivity(new Intent(WNaviGuideActivity.this, MainActivity.class));
}
}
};
private void ChangeState() {
isPreSPEAKtotal = true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNaviHelper = WalkNavigateHelper.getInstance();
try {
View view = mNaviHelper.onCreate(WNaviGuideActivity.this);
if (view != null) {
setContentView(view);
}
} catch (Exception e) {
e.printStackTrace();
}
//设置步行导航状态监听
mNaviHelper.setWalkNaviStatusListener(new IWNaviStatusListener() {
@Override
public void onWalkNaviModeChange(int mode, WalkNaviModeSwitchListener listener) {
Log.d("======", "onWalkNaviModeChange : " + mode);
mNaviHelper.switchWalkNaviMode(WNaviGuideActivity.this, mode, listener);
}
/* @Description: 这个是在退出导航时自动调用的方法,在这里要把对象进行释放,避免空对象的产生
* @Author: LiY ue
*/
@Override
public void onNaviExit() {
Log.d("======", "onNaviExit");
handler.removeMessages(0x001);
handler.removeMessages(0x002);
handler.removeMessages(0x003);
}
});
/**
* 诱导文本回调
* @param s 诱导文本
* @param b 是否抢先播报
* @return
*/
mNaviHelper.setTTsPlayer(new IWTTSPlayer() {
@Override
public int playTTSText(final String s, boolean b) {
Log.d(TAG, "tts: " + s);
return 0;
}
});
boolean startResult = mNaviHelper.startWalkNavi(WNaviGuideActivity.this);
Log.e(TAG, "startWalkNavi result : " + startResult);
//设置路线指引监听
mNaviHelper.setRouteGuidanceListener(this, new
IWRouteGuidanceListener() {
/**
* @Description: 诱导图标更改方法回调
* @Author: LY
*/
@Override
public void onRouteGuideIconUpdate(Drawable icon) {
}
@Override
public void onRouteGuideKind(RouteGuideKind routeGuideKind) {
Log.d("======", "onRouteGuideKind: " + routeGuideKind);
if (routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_PassRoad_Left || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_PassRoad_Right || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_Right_PassRoad_Front || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_Right_PassRoad_UTurn)
if (routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Front || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Left || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Left_Front || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Right || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Right_Back || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Right_Front){
Log.d("========", "onRouteGuideKind: aaaaaa");
}
}
/**
* @Description: 诱导信息
*/
@Override
public void onRoadGuideTextUpdate(CharSequence charSequence, CharSequence
charSequence1) {
Log.d(TAG, "onRoadGuideTextUpdate charSequence=: " + charSequence + " charSequence1 = : " +
charSequence1);
orient = charSequence.toString() + charSequence1.toString();
}
/**
*
* @Description: 总剩余距离回调
*/
@Override
public void onRemainDistanceUpdate(CharSequence charSequence) {
Log.d(TAG, "onRemainDistanceUpdate: charSequence = :" + charSequence);
if (isPreSPEAKtotal) {
}
}
/**
*
* @Description: 总剩余时间回调
* @Author: LY
*/
@Override
public void onRemainTimeUpdate(CharSequence charSequence) {
Log.d(TAG, "onRemainTimeUpdate: charSequence = :" + charSequence);
if (isPreSPEAKtotal) {
isPreSPEAKtotal = false;
}
}
@Override
public void onGpsStatusChange(CharSequence charSequence, Drawable drawable) {
Log.d(TAG, "onGpsStatusChange: charSequence = :" + charSequence);
}
@Override
public void onRouteFarAway(CharSequence charSequence, Drawable drawable) {
Log.d(TAG, "onRouteFarAway: charSequence = :" + charSequence);
}
/**
* @Description: 偏航规划中
*/
@Override
public void onRoutePlanYawing(CharSequence charSequence, Drawable drawable) {
Log.d(TAG, "onRoutePlanYawing: charSequence = :" + charSequence);
}
/**
*
* @Description: 重新算路成功
*/
@Override
public void onReRouteComplete() {
}
/**
*
* @Description: 到达目的地回调
*/
@Override
public void onArriveDest() {
handler.sendEmptyMessageDelayed(0x003, 6000);
}
@Override
public void onIndoorEnd(Message msg) {
}
@Override
public void onFinalEnd(Message msg) {
}
@Override
public void onVibrate() {
}
});
handler.sendEmptyMessage(0x001);
handler.sendEmptyMessage(0x002);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == ArCameraView.WALK_AR_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(WNaviGuideActivity.this, "没有相机权限,请打开后重试", Toast.LENGTH_SHORT).show();
} else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mNaviHelper.startCameraAndSetMapView(WNaviGuideActivity.this);
}
}
}
}
<activity android:name=".WNaviGuideActivity">
</activity>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。