百度导航点击导航为什么不能调出导航界面
shejiayan88 发布于2021-05 浏览:2280 回复:0
0
收藏

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private MapView mMapView = null;
private BaiduMap mBaiduMap = null;
private Context context;

//定位相关
private double mLatitude;
private double mLongtitude;

//方向传感器
private MyOrientationListener mMyOrientationListener;
private float mCurrentX;
//自定义图标
private BitmapDescriptor mIconLocation;
private LocationClient mLocationClient;
public BDAbstractLocationListener myListener;
private LatLng mLastLocationData;
private boolean isFirstin = true;

// 路线规划相关
private RoutePlanSearch mSearch = null;


//导航相关
private static final String APP_FOLDER_NAME = "MyBNDTSDK-Api";
private String mSDCardPath = null;
private static final String[] authBaseArr = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION
};
private static final int authBaseRequestCode = 1;
private boolean hasInitSuccess = false;
static final String ROUTE_PLAN_NODE = "routePlanNode";
private BNRoutePlanNode mStartNode = null;
private LatLng mDestLocationData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
SDKInitializer.setCoordType(CoordType.BD09LL);
this.context = this;
mMapView = (MapView) findViewById(R.id.baiduMapView);
//获取地图控件引用
mBaiduMap = mMapView.getMap();
initMyLocation();
initPoutePlan();
initLongClick();
button();
//初始化导航相关
if (initDirs()) {
initNavi();
}
}
protected void onStart() {
super.onStart();
//开启定位
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted())
mLocationClient.start();
//开启方向传感器
mMyOrientationListener.start();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
//停止定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
//停止方向传感器
mMyOrientationListener.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
mSearch.destroy();
}
@Override
public void onClick(View v) {
SDKInitializer.initialize(getApplicationContext());
switch (v.getId()) {
case R.id.but_Loc: {
centerToMyLocation(mLatitude, mLongtitude);
break;
}
case R.id.but_RoutrPlan: {
StarRoute();
break;
}
case R.id.but_Navi: {
if(mDestLocationData == null)
{
Toast.makeText(MainActivity.this, "导航:长按设置目标地点", Toast.LENGTH_SHORT).show();
return;
}
routeplanToNavi();
break;
}
}
}

//按钮响应
private void button() {
//按钮
Button mbut_Loc = (Button) findViewById(R.id.but_Loc);
Button mbut_RoutrPlan = (Button) findViewById(R.id.but_RoutrPlan);
Button mbut_Navi = (Button) findViewById(R.id.but_Navi);
//按钮处理
mbut_Loc.setOnClickListener(this);
mbut_RoutrPlan.setOnClickListener(this);
mbut_Navi.setOnClickListener(this);
}

//定位
private class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//mapView 销毁后不在处理新接收的位置
if (location == null || mMapView == null){
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(mCurrentX).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
//设置自定义图标
MyLocationConfiguration config = new
MyLocationConfiguration(
MyLocationConfiguration.LocationMode.NORMAL, true, mIconLocation);
mBaiduMap.setMyLocationConfiguration(config);
//更新经纬度
mLatitude = location.getLatitude();
mLongtitude = location.getLongitude();
//设置起点
mLastLocationData = new LatLng(mLatitude, mLongtitude);
if (isFirstin) {
centerToMyLocation(location.getLatitude(), location.getLongitude());

if (location.getLocType() == BDLocation.TypeGpsLocation) {
// GPS定位结果
Toast.makeText(context, "定位:"+location.getAddrStr(), Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
// 网络定位结果
Toast.makeText(context, "定位:"+location.getAddrStr(), Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {
// 离线定位结果
Toast.makeText(context, "定位:"+location.getAddrStr(), Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeServerError) {
Toast.makeText(context, "定位:服务器错误", Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
Toast.makeText(context, "定位:网络错误", Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
Toast.makeText(context, "定位:手机模式错误,请检查是否飞行", Toast.LENGTH_SHORT).show();
}
isFirstin = false;
}
}
}
//初始化定位
private void initMyLocation() {
//缩放地图
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap.setMapStatus(msu);
//开启定位
mBaiduMap.setMyLocationEnabled(true);
//声明LocationClient类
mLocationClient = new LocationClient(this);
//通过LocationClientOption设置LocationClient相关参数
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setIsNeedAddress(true);//设置是否需要地址信息
option.setScanSpan(1000);
//设置locationClientOption
mLocationClient.setLocOption(option);
myListener = new MyLocationListener();
//注册监听函数
mLocationClient.registerLocationListener(myListener);
//初始化图标
mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps);
initOrientation();
//开始定位
mLocationClient.start();
}
//回到定位中心
private void centerToMyLocation(double latitude, double longtitude) {
mBaiduMap.clear();
mLastLocationData = new LatLng(latitude, longtitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(mLastLocationData);
mBaiduMap.animateMapStatus(msu);
}
//传感器
private void initOrientation() {
//传感器
mMyOrientationListener = new MyOrientationListener(context);
mMyOrientationListener.setOnOrientationListener(new MyOrientationListener.OnOrientationListener() {
@Override
public void onOrientationChanged(float x) {
mCurrentX = x;
}
});
}


//路线规划初始化
private void initPoutePlan() {
mSearch = RoutePlanSearch.newInstance();
mSearch.setOnGetRoutePlanResultListener(listener);
}
// 路线规划模块
public OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
@Override
public void onGetWalkingRouteResult(WalkingRouteResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(MainActivity.this, "路线规划:未找到结果,检查输入", Toast.LENGTH_SHORT).show();
//禁止定位
isFirstin = false;
}
assert result != null;
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
// 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
result.getSuggestAddrInfo();
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
mBaiduMap.clear();
Toast.makeText(MainActivity.this, "路线规划:搜索完成", Toast.LENGTH_SHORT).show();
WalkingRouteOverlay overlay = new WalkingRouteOverlay(mBaiduMap);
overlay.setData(result.getRouteLines().get(0));
overlay.addToMap();
overlay.zoomToSpan();
}
//禁止定位
isFirstin = false;
}
@Override
public void onGetTransitRouteResult(TransitRouteResult var1) {
}
@Override
public void onGetMassTransitRouteResult(MassTransitRouteResult var1) {
}
@Override
public void onGetDrivingRouteResult(DrivingRouteResult result) {
}
@Override
public void onGetIndoorRouteResult(IndoorRouteResult var1) {
}
@Override
public void onGetBikingRouteResult(BikingRouteResult var1) {
}
};
//开始规划
private void StarRoute() {
SDKInitializer.initialize(getApplicationContext());
// 设置起、终点信息
//PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", "西二旗地铁站");
//PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", "百度科技园");
LatLng startPoint = new LatLng(32.348519 , 119.406561);
LatLng endPoint = new LatLng(32.390444, 119.380289);
PlanNode stNode = PlanNode.withLocation(startPoint);
PlanNode enNode = PlanNode.withLocation(endPoint);

mSearch.walkingSearch((new WalkingRoutePlanOption())
.from(stNode)
.to(enNode));
}


//导航
//获取Sdcard目录
private String getSdcardDir() {
if (Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
return Environment.getExternalStorageDirectory().toString();
}
return null;
}
//初始化导航目录
private boolean initDirs() {
mSDCardPath = getSdcardDir();
if (mSDCardPath == null) {
return false;
}
File f = new File(mSDCardPath, APP_FOLDER_NAME);
if (!f.exists()) {
try {
f.mkdir();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
private boolean hasBasePhoneAuth() {
PackageManager pm = this.getPackageManager();
for (String auth : authBaseArr) {
if (pm.checkPermission(auth, this.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}


//初始化语音播报
private void initTTS() {
// 使用内置TTS
BaiduNaviManagerFactory.getTTSManager().initTTS(getApplicationContext(),
getSdcardDir(), APP_FOLDER_NAME, NormalUtils.getTTSAppID());

// 注册同步内置tts状态回调
BaiduNaviManagerFactory.getTTSManager().setOnTTSStateChangedListener(
new IBNTTSManager.IOnTTSPlayStateChangedListener() {
@Override
public void onPlayStart() {
Log.e("BNSDKDemo", "ttsCallback.onPlayStart");
}

@Override
public void onPlayEnd(String speechId) {
Log.e("BNSDKDemo", "ttsCallback.onPlayEnd");
}

@Override
public void onPlayError(int code, String message) {
Log.e("BNSDKDemo", "ttsCallback.onPlayError");
}
}
);
// 注册内置tts 异步状态消息
BaiduNaviManagerFactory.getTTSManager().setOnTTSStateChangedHandler(
new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
Log.e("BNSDKDemo", "ttsHandler.msg.what=" + msg.what);
}
}
);
}
//初始化导航
private void initNavi() {
// 申请权限
if (android.os.Build.VERSION.SDK_INT >= 25) {
if (!hasBasePhoneAuth()) {
this.requestPermissions(authBaseArr, authBaseRequestCode);
return;
}
}

BaiduNaviManagerFactory.getBaiduNaviManager().init(this,
mSDCardPath, APP_FOLDER_NAME, new IBaiduNaviManager.INaviInitListener() {

@Override
public void onAuthResult(int status, String msg) {
String result;
if (0 == status) {
result = "key校验成功!";
} else {
result = "key校验失败, " + msg;
}
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}

@Override
public void initStart() {
Toast.makeText(MainActivity.this, "导航引擎初始化开始", Toast.LENGTH_SHORT).show();
}

@Override
public void initSuccess() {
Toast.makeText(MainActivity.this, "导航引擎初始化成功", Toast.LENGTH_SHORT).show();
hasInitSuccess = true;
// 初始化tts
initTTS();
}

@Override
public void initFailed() {
Toast.makeText(MainActivity.this, "导航引擎初始化失败", Toast.LENGTH_SHORT).show();
}
});

}
//添加导航目的地图标
private void addDestInfoOverlay(LatLng latLng) {
mBaiduMap.clear();
OverlayOptions options = new MarkerOptions().position(latLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_geo))
.zIndex(5);
mBaiduMap.addOverlay(options);
}
//坐标转换
public static BDLocation bd2gcj(BDLocation loc) {
return LocationClient.getBDLocationInCoorType(loc,BDLocation.BDLOCATION_BD09LL_TO_GCJ02);
}
//导航算路
private void routeplanToNavi() {

final int coType = BNRoutePlanNode.CoordinateType.GCJ02;
if (!hasInitSuccess) {
Toast.makeText(MainActivity.this, "还未初始化!", Toast.LENGTH_SHORT).show();
}

BDLocation srcBdLocation = new BDLocation();
srcBdLocation.setLatitude(mLastLocationData.latitude);
srcBdLocation.setLongitude(mLastLocationData.longitude);
BDLocation srcGcj = bd2gcj(srcBdLocation);
BDLocation destBdLocation = new BDLocation();
destBdLocation.setLatitude(mDestLocationData.latitude);
destBdLocation.setLongitude(mDestLocationData.longitude);
BDLocation destGcj = bd2gcj(destBdLocation);

BNRoutePlanNode sNode = new BNRoutePlanNode(srcGcj.getLongitude(),srcGcj.getLatitude(),"我的地点",null,coType);
BNRoutePlanNode eNode = new BNRoutePlanNode(destGcj.getLongitude(),destGcj.getLatitude(),"目标地点",null,coType);


mStartNode = sNode;

List list = new ArrayList<>();
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(MainActivity.this, "导航:算路开始", Toast.LENGTH_SHORT)
.show();
break;
case IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_SUCCESS:
Toast.makeText(MainActivity.this, "导航:算路成功", Toast.LENGTH_SHORT)
.show();
break;
case IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_FAILED:
Toast.makeText(MainActivity.this, "导航:算路失败", Toast.LENGTH_SHORT)
.show();
break;
case IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_TO_NAVI:
Toast.makeText(MainActivity.this, "导航:算路成功准备进入导航", Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent(MainActivity.this,
DemoGuideActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable(ROUTE_PLAN_NODE, mStartNode);
intent.putExtras(bundle);
startActivity(intent);
break;
default:
// nothing
break;
}
}
});
}
//长按处理
private void initLongClick() {
mBaiduMap.setOnMapLongClickListener(new BaiduMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
Toast.makeText(context,"导航:设置目的地成功", Toast.LENGTH_LONG).show();
mDestLocationData = latLng;
addDestInfoOverlay(latLng);
}
});
}
}

收藏
点赞
0
个赞
TOP
切换版块