Android百度定位导航之基于百度地图移动获取位置和自动定位

  public class MainActivity extends Activity {

  public MyApplication app;

  static MapView mMapView = null;

  public MKMapViewListener mMapListener = null;

  MyLocationOverlay myLocationOverlay = null;

  // 定位相关

  LocationClient mLocClient;

  public NotifyLister mNotifyer = null;

  public MyLocationListenner myListener = new MyLocationListenner();

  LocationData locData = null;

  private MapController mMapController = null;

  static MKSearch mkSerach;

  Handler mHandler = new Handler() {

  public void handleMessage(android.os.Message msg) {

  Toast.makeText(MainActivity.this, "msg:" + msg.what,

  Toast.LENGTH_SHORT).show();

  };

  };

  static TextView showAddr;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  showAddr = (TextView) findViewById(R.id.showAddr);

  MyIcon mi = new MyIcon(this);

  //在屏幕中心点添加接我图标

  getWindow().addContentView(

  mi,

  new LayoutParams(LayoutParams.FILL_PARENT,

  LayoutParams.FILL_PARENT));

  MyIcon2 m2 = new MyIcon2(this);

  getWindow().addContentView(

  m2,

  new LayoutParams(LayoutParams.FILL_PARENT,

  LayoutParams.FILL_PARENT));

  mMapView = (MapView) findViewById(R.id.bmapsView);

  mMapController = mMapView.getController();

  initMapView();

  app = MyApplication.getInstance();

  mLocClient = new LocationClient(this);

  mLocClient.registerLocationListener(myListener);

  //搜索初始化

  mkSerach = new MKSearch();

  mkSerach.init(app.mBMapManager, new MKSearchListener() {

  @Override

  public void onGetWalkingRouteResult(MKWalkingRouteResult arg0,

  int arg1) {

  // TODO Auto-generated method stub

  }

  @Override

  public void onGetTransitRouteResult(MKTransitRouteResult arg0,

  int arg1) {

  // TODO Auto-generated method stub

  }

  @Override

  public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {

  // TODO Auto-generated method stub

  }

  @Override

  public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {

  // TODO Auto-generated method stub

  }

  @Override

  public void onGetPoiDetailSearchResult(int arg0, int arg1) {

  // TODO Auto-generated method stub

  }

  @Override

  public void onGetDrivingRouteResult(MKDrivingRouteResult arg0,

  int arg1) {

  // TODO Auto-generated method stub

  }

  @Override

  public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {

  // TODO Auto-generated method stub

  }

  @Override

  public void onGetAddrResult(MKAddrInfo info, int arg1) {

  showAddr.setText(info.strAddr);

  }

  });

  //设置地图相关

  LocationClientOption option = new LocationClientOption();

  option.setOpenGps(true);

  option.setCoorType("bd09ll");

  option.setScanSpan(300000);

  mLocClient.setLocOption(option);

  mLocClient.start();

  mMapView.getController().setZoom(16);

  mMapView.getController().enableClick(true);

  mMapView.displayZoomControls(true);

  mMapListener = new MKMapViewListener() {

  public void onMapMoveFinish() {

  }

  public void onClickMapPoi(MapPoi mapPoiInfo) {

  // TODO Auto-generated method stub

  String title = "";

  if (mapPoiInfo != null) {

  title = mapPoiInfo.strText;

  Toast.makeText(MainActivity.this, title, Toast.LENGTH_SHORT)

  .show();

  }

  }

  };

  mMapView.regMapViewListener(MyApplication.getInstance().mBMapManager,

  mMapListener);

  myLocationOverlay = new MyLocationOverlay(mMapView);

  locData = new LocationData();

  myLocationOverlay.setData(locData);

  mMapView.getOverlays().add(myLocationOverlay);

  myLocationOverlay.enableCompass();

  mMapView.refresh();

  }

  private void initMapView() {

  mMapView.setLongClickable(true);

  }

  /**

  * 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中

  */

  public class MyLocationListenner implements BDLocationListener {

  public void onReceiveLocation(BDLocation location) {

  if (location == null)

  return;

  locData.latitude = location.getLatitude();

  locData.longitude = location.getLongitude();

  locData.direction = 2.0f;

  locData.accuracy = location.getRadius();

  locData.direction = location.getDerect();

  Log.d("loctest",

  String.format("before: lat: %f lon: %f",

  location.getLatitude(), location.getLongitude()));

  myLocationOverlay.setData(locData);

  mMapView.refresh();

  mMapController

  .animateTo(new GeoPoint((int) (locData.latitude * 1e6),

  (int) (locData.longitude * 1e6)), mHandler

  .obtainMessage(1));

  }

  public void onReceivePoi(BDLocation poiLocation) {

  if (poiLocation == null) {

  return;

  }

  }

  }

  public class NotifyLister extends BDNotifyListener {

  public void onNotify(BDLocation mlocation, float distance) {

  }

  }

  @Override

  protected void onPause() {

  mMapView.onPause();

  super.onPause();

  }

  @Override

  protected void onResume() {

  mMapView.onResume();

  super.onResume();

  }

  @Override

  protected void onSaveInstanceState(Bundle outState) {

  super.onSaveInstanceState(outState);

  mMapView.onSaveInstanceState(outState);

  }

  @Override

  protected void onRestoreInstanceState(Bundle savedInstanceState) {

  super.onRestoreInstanceState(savedInstanceState);

  mMapView.onRestoreInstanceState(savedInstanceState);

  }

  public static void getPosition(GeoPoint g) {

  mkSerach.reverseGeocode(g);

  showAddr.setText("获取位置中...");

  }

  }