RouteGuideDemo
GPS导航Demo public class RouteGuideDemo extends Activity{ private BNaviPoint mStartPoint = new BNaviPoint(116.30142, 40.05087, "百度大厦", BNaviPoint.CoordinateType.GCJ02); private BNaviPoint mEndPoint = new BNaviPoint(116.39750, 39.90882, "北京天安门", BNaviPoint.CoordinateType.GCJ02); private List<BNaviPoint> mViaPoints = new ArrayList<BNaviPoint>(); private Button mBtnAddViaPoint; public void onCreate(Bundle savedInstanceState){ setContentView(R.layout.activity_routeguide); Button btnStartNavigation = (Button)findViewById(R.id.button_navigation); btnStartNavigation.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mViaPoints.size() == 0) { launchNavigator(); } else { launchNavigatorViaPoints(); } } }); findViewById(R.id.start_nav2_btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (mViaPoints.size() == 0) { launchNavigator2(); } else { launchNavigatorViaPoints(); } } }); mBtnAddViaPoint = (Button) findViewById(R.id.add_via_btn); mBtnAddViaPoint.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { addViaPoint(); } }); } private void addViaPoint() { EditText viaXEditText = (EditText) findViewById(R.id.et_via_x); EditText viaYEditText = (EditText) findViewById(R.id.et_via_y); double latitude = 0, longitude = 0; try { latitude = Integer.parseInt(viaXEditText.getText().toString()); longitude = Integer.parseInt(viaYEditText.getText().toString()); } catch (NumberFormatException e) { e.printStackTrace(); } BNaviPoint viaPoint = new BNaviPoint(longitude/1e5, latitude/1e5, "途经点" + (mViaPoints.size()+1)); mViaPoints.add(viaPoint); Toast.makeText(this, "已添加途经点:" + viaPoint.getName(), Toast.LENGTH_SHORT).show(); if (mViaPoints.size() >= 3) { mBtnAddViaPoint.setEnabled(false); } } } private void launchNavigator2(){ 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() { //跳转监听 @Override public void onJumpToNavigator(Bundle configParams) { Intent intent = new Intent(RouteGuideDemo.this, BNavigatorActivity.class); intent.putExtras(configParams); startActivity(intent); } @Override public void onJumpToDownloader() { } }); } private void launchNavigatorViaPoints(){ List<BNaviPoint> points = new ArrayList<BNaviPoint>(); BaiduNaviManager.getInstance().launchNavigator(this, points, //路线点列表 NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME, //算路方式 true, //真实导航 BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, //在离线策略 new OnStartNavigationListener() { //跳转监听 RoutePlanDemo
package com.baidu.navi.sdkdemo; public class RoutePlanDemo extends Activity { private RoutePlanModel mRoutePlanModel = null; private MapGLSurfaceView mMapView = null; public void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); setContentView(R.layout.activity_routeplan); findViewById(R.id.online_calc_btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startCalcRoute(NL_Net_Mode.NL_Net_Mode_OnLine); } }); findViewById(R.id.simulate_btn).setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { startNavi(false); } }); findViewById(R.id.real_btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { PreferenceHelper.getInstance(getApplicationContext()) .putBoolean(SettingParams.Key.SP_TRACK_LOCATE_GUIDE, false); startNavi(true); } }); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onPause() { super.onPause(); BNRoutePlaner.getInstance().setRouteResultObserver(null); ((ViewGroup) (findViewById(R.id.mapview_layout))).removeAllViews(); BNMapController.getInstance().onPause(); } @Override public void onResume() { super.onResume(); initMapView(); ((ViewGroup) (findViewById(R.id.mapview_layout))).addView(mMapView); BNMapController.getInstance().onResume(); } private void initMapView() { if (Build.VERSION.SDK_INT < 14) { BaiduNaviManager.getInstance().destroyNMapView(); } mMapView = BaiduNaviManager.getInstance().createNMapView(this); BNMapController.getInstance().setLevel(14); BNMapController.getInstance().setLayerMode( LayerMode.MAP_LAYER_MODE_BROWSE_MAP); updateCompassPosition(); BNMapController.getInstance().locateWithAnimation( (int) (113.97348 * 1e5), (int) (22.53951 * 1e5)); } private void updateCompassPosition(){ int screenW = this.getResources().getDisplayMetrics().widthPixels; BNMapController.getInstance().resetCompassPosition( screenW - ScreenUtil.dip2px(this, 30), ScreenUtil.dip2px(this, 126), -1); } private void startCalcRoute(int netmode) { EditText startXEditText = (EditText) findViewById(R.id.et_start_x); EditText startYEditText = (EditText) findViewById(R.id.et_start_y); EditText endXEditText = (EditText) findViewById(R.id.et_end_x); EditText endYEditText = (EditText) findViewById(R.id.et_end_y); int sX = 0, sY = 0, eX = 0, eY = 0; try { sX = Integer.parseInt(startXEditText.getText().toString()); sY = Integer.parseInt(startYEditText.getText().toString()); eX = Integer.parseInt(endXEditText.getText().toString()); eY = Integer.parseInt(endYEditText.getText().toString()); } catch (Exception e) { e.printStackTrace(); } RoutePlanNode startNode = new RoutePlanNode(sX, sY, RoutePlanNode.FROM_MAP_POINT, "华侨城", "华侨城"); RoutePlanNode endNode = new RoutePlanNode(eX, eY, RoutePlanNode.FROM_MAP_POINT, "滨海苑", "滨海苑"); ArrayList<RoutePlanNode> nodeList = new ArrayList<RoutePlanNode>(2); nodeList.add(startNode); nodeList.add(endNode); BNRoutePlaner.getInstance().setObserver(new RoutePlanObserver(this, null)); BNRoutePlaner.getInstance().setCalcMode(NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME); BNRoutePlaner.getInstance().setRouteResultObserver(mRouteResultObserver); boolean ret = BNRoutePlaner.getInstance().setPointsToCalcRoute( nodeList,NL_Net_Mode.NL_Net_Mode_OnLine); if(!ret){ Toast.makeText(this, "规划失败", Toast.LENGTH_SHORT).show(); } } private void startNavi(boolean isReal) { if (mRoutePlanModel == null) { Toast.makeText(this, "请先算路!", Toast.LENGTH_LONG).show(); return; } RoutePlanNode startNode = mRoutePlanModel.getStartNode(); RoutePlanNode endNode = mRoutePlanModel.getEndNode(); if (null == startNode || null == endNode) { return; } int calcMode = BNRoutePlaner.getInstance().getCalcMode(); Bundle bundle = new Bundle(); bundle.putInt(BNavConfig.KEY_ROUTEGUIDE_VIEW_MODE, BNavigator.CONFIG_VIEW_MODE_INFLATE_MAP); bundle.putInt(BNavConfig.KEY_ROUTEGUIDE_CALCROUTE_DONE, BNavigator.CONFIG_CLACROUTE_DONE); bundle.putInt(BNavConfig.KEY_ROUTEGUIDE_START_X, startNode.getLongitudeE6()); bundle.putInt(BNavConfig.KEY_ROUTEGUIDE_START_Y, startNode.getLatitudeE6()); bundle.putInt(BNavConfig.KEY_ROUTEGUIDE_END_X, endNode.getLongitudeE6()); bundle.putInt(BNavConfig.KEY_ROUTEGUIDE_END_Y, endNode.getLatitudeE6()); bundle.putString(BNavConfig.KEY_ROUTEGUIDE_START_NAME, mRoutePlanModel.getStartName(this, false)); bundle.putString(BNavConfig.KEY_ROUTEGUIDE_END_NAME, mRoutePlanModel.getEndName(this, false)); bundle.putInt(BNavConfig.KEY_ROUTEGUIDE_CALCROUTE_MODE, calcMode); if (!isReal) { bundle.putInt(BNavConfig.KEY_ROUTEGUIDE_LOCATE_MODE, RGLocationMode.NE_Locate_Mode_RouteDemoGPS); } else { bundle.putInt(BNavConfig.KEY_ROUTEGUIDE_LOCATE_MODE, RGLocationMode.NE_Locate_Mode_GPS); } Intent intent = new Intent(RoutePlanDemo.this, BNavigatorActivity.class); intent.putExtras(bundle); startActivity(intent); } private IRouteResultObserver mRouteResultObserver = new IRouteResultObserver() { @Override public void onRoutePlanYawingSuccess() { } @Override public void onRoutePlanYawingFail() { } @Override public void onRoutePlanSuccess() { BNMapController.getInstance().setLayerMode( LayerMode.MAP_LAYER_MODE_ROUTE_DETAIL); mRoutePlanModel = (RoutePlanModel) NaviDataEngine.getInstance() .getModel(ModelName.ROUTE_PLAN); } @Override public void onRoutePlanFail() { } @Override public void onRoutePlanCanceled() { } @Override public void onRoutePlanStart() { } };}