导航

BNavigatorActivity

package com.baidu.navi.sdkdemo;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import com.baidu.navisdk.BNaviModuleManager;
import com.baidu.navisdk.BaiduNaviManager;
import com.baidu.navisdk.comapi.mapcontrol.BNMapController;
import com.baidu.navisdk.comapi.routeplan.BNRoutePlaner;
import com.baidu.navisdk.comapi.tts.BNTTSPlayer;
import com.baidu.navisdk.comapi.tts.BNavigatorTTSPlayer;
import com.baidu.navisdk.comapi.tts.IBNTTSPlayerListener;
import com.baidu.navisdk.model.datastruct.LocData;
import com.baidu.navisdk.model.datastruct.SensorData;
import com.baidu.navisdk.ui.routeguide.BNavigator;
import com.baidu.navisdk.ui.routeguide.IBNavigatorListener;
import com.baidu.navisdk.ui.widget.RoutePlanObserver;
import com.baidu.navisdk.ui.widget.RoutePlanObserver.IJumpToDownloadListener;
import com.baidu.nplatform.comapi.map.MapGLSurfaceView;
public class BNavigatorActivity extends Activity{
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  //创建NmapView
  if (Build.VERSION.SDK_INT < 14) {
            BaiduNaviManager.getInstance().destroyNMapView();
        }
  MapGLSurfaceView nMapView = BaiduNaviManager.getInstance().createNMapView(this);
  
  //创建导航视图
  View navigatorView = BNavigator.getInstance().init(BNavigatorActivity.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() {
    // TODO Auto-generated method stub
    
   }
  }));
  
 }
 
 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){
             finish();
         }else if(IBNavigatorListener.PAGE_JUMP_WHEN_ROUTE_PLAN_FAIL == jumpTiming){
          finish();
         }
        }
  @Override
  public void notifyGPSStatusData(int arg0) {
   // TODO Auto-generated method stub
   
  }
  @Override
  public void notifyLoacteData(LocData arg0) {
   // TODO Auto-generated method stub
   
  }
  @Override
  public void notifyNmeaData(String arg0) {
   // TODO Auto-generated method stub
   
  }
  @Override
  public void notifySensorData(SensorData arg0) {
   // TODO Auto-generated method stub
   
  }
  @Override
  public void notifyStartNav() {
   // TODO Auto-generated method stub
   BaiduNaviManager.getInstance().dismissWaitProgressDialog();
  }
  @Override
  public void notifyViewModeChanged(int arg0) {
   // TODO Auto-generated method stub
   
  }
        
    };
     
 @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();
    }
}

DemoMainActivity

 package com.baidu.navi.sdkdemo;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.baidu.lbsapi.auth.LBSAuthManagerListener;
import com.baidu.navi.sdkdemo.R;
import com.baidu.navisdk.BNaviEngineManager.NaviEngineInitListener;
import com.baidu.navisdk.BaiduNaviManager;
import com.baidu.navisdk.util.verify.BNKeyVerifyListener;
public class DemoMainActivity extends ListActivity {
 /** SDK验证密钥 */
    private final static String ACCESS_KEY = "NX4p2vcBz1rq5SWGh6OSA2Ux";//"LVUOeu2yWl5uHwG6zewGwN0m";
 private static final String CATEGORY_SDK_DEMO = "android.intent.category.BAIDUNAVISDK_DEMO";
 //NX4p2vcBz1rq5SWGh6OSA2Ux sdkdemotest.keystore的sha1值
 private boolean mIsEngineInitSuccess = false;
 
 public void onCreate(Bundle saveInstance) {
  super.onCreate(saveInstance);
 
  setContentView(R.layout.activity_main);
  
  //初始化导航引擎
//  BaiduNaviManager.getInstance().initEngine(this, getSdcardDir(),
//          mNaviEngineInitListener, ACCESS_KEY, mKeyVerifyListener);
        BaiduNaviManager.getInstance().initEngine(this, getSdcardDir(),
                mNaviEngineInitListener, new LBSAuthManagerListener() {
                    @Override
                    public void onAuthResult(int status, String msg) {
                        String str = null;
                        if (0 == status) {
                            str = "key校验成功!";
                        } else {
                            str = "key校验失败, " + msg;
                        }
                        Toast.makeText(DemoMainActivity.this, str,
                                Toast.LENGTH_LONG).show();
                    }
                });
  //创建Demo视图
  initViews();
 }
 private String getSdcardDir() {
  if (Environment.getExternalStorageState().equalsIgnoreCase(
    Environment.MEDIA_MOUNTED)) {
   return Environment.getExternalStorageDirectory().toString();
  }
  return null;
 }
 private NaviEngineInitListener mNaviEngineInitListener = new NaviEngineInitListener() {
  public void engineInitSuccess() {
   mIsEngineInitSuccess = true;
  }
  public void engineInitStart() {
  }
  public void engineInitFail() {
  }
 };
 
    private BNKeyVerifyListener mKeyVerifyListener = new BNKeyVerifyListener() {
  
  @Override
  public void onVerifySucc() {
   // TODO Auto-generated method stub
   Toast.makeText(DemoMainActivity.this, "key校验成功", Toast.LENGTH_LONG).show();
  }
  
  @Override
  public void onVerifyFailed(int arg0, String arg1) {
   // TODO Auto-generated method stub
   Toast.makeText(DemoMainActivity.this, "key校验失败", Toast.LENGTH_LONG).show();
  }
 };
 private void initViews() {
  setListAdapter(new SimpleAdapter(this, getListData(),
    android.R.layout.simple_list_item_1, new String[] { "title" },
    new int[] { android.R.id.text1 }));
  getListView().setTextFilterEnabled(true);
 }
 private ArrayList<Map<String, Object>> getListData() {
  ArrayList<Map<String, Object>> maps = new ArrayList<Map<String, Object>>(
    0);
  Intent intent = new Intent(Intent.ACTION_MAIN, null);
  intent.addCategory(CATEGORY_SDK_DEMO);
  PackageManager pm = getPackageManager();
  List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
  int size = resolveInfos.size();
  for (int i = 0; i < size; i++) {
   ResolveInfo info = resolveInfos.get(i);
   Map<String, Object> item = new HashMap<String, Object>(2);
   CharSequence labelSeq = info.loadLabel(pm);
   item.put("title", labelSeq != null ? labelSeq.toString()
     : info.activityInfo.name);
   item.put(
     "intent",
     activityIntent(
       info.activityInfo.applicationInfo.packageName,
       info.activityInfo.name));
   maps.add(item);
  }
  return maps;
 }
 private Intent activityIntent(String pkg, String componentName) {
  Intent result = new Intent();
  result.setClassName(pkg, componentName);
  return result;
 }
 @SuppressLint("ShowToast")
 @SuppressWarnings("unchecked")
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  boolean isInitSuccess = BaiduNaviManager.getInstance().checkEngineStatus(getApplicationContext());
  if(!isInitSuccess){
   return ;
  }
  Map<String, Object> map = (Map<String, Object>) l
    .getItemAtPosition(position);
  Intent intent = (Intent) map.get("intent");
  startActivity(intent);
 }
 @Override
 public void onDestroy() {
  super.onDestroy();
 }
 @Override
 public void onBackPressed() {
  System.exit(0);
  android.os.Process.killProcess(android.os.Process.myUid());
 }
 @Override
 public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
 }
 
 
}

你可能感兴趣的:(导航)