百度地图方向定位

来源:互联网 发布:git ignore mac 编辑:程序博客网 时间:2024/06/06 01:57
private MyOrientationListener mMyOrientationListener;private float mCurrentX;  //方向偏移
private BitmapDescriptor mBitmapDescriptor;

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);       //方向传感器监听函数    mMyOrientationListener=new MyOrientationListener(this);
    mMyOrientationListener.setOnOrientationListener(new MyOrientationListener.OnOrientationListener() {
    @Override    public void onOrientationChanged(float x) {        mCurrentX=x;    }});
}
protected void onStart() {    super.onStart();        //开启方向传感器        mMyOrientationListener.start();    }
/** *  百度地图定位监听 */    public class MyLocationListener implements BDLocationListener {    @Override    public void onReceiveLocation(BDLocation location) {        //Receive Location        if (location.getCity()!=null&&!location.getCity().equals("")){                mLatitude = location.getLatitude();  //                mLongitude = location.getLongitude();            MyLocationData locData = new MyLocationData.Builder()                    .accuracy(location.getRadius())                    // 此处设置开发者获取到的方向信息,顺时针0-360                    .direction(mCurrentX).latitude(mLatitude)                    .longitude(mLongitude).build();          // 设置定位数据            mMapView.getMap().setMyLocationData(locData);            initLocalMark();                  }    }
  
    /**     * 初始化当前定位标记     */    public  void initLocalMark(){
       //定位标志图形
        mBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.mylocal);
MyLocationConfiguration config=new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL,true,mBitmapDescriptor); mMapView.getMap().setMyLocationConfigeration(config);
    }
@Overrideprotected void onStop() {    super.onStop();    mMapView.getMap().setMyLocationEnabled(false);    //结束定位    if(mLocationClient.isStarted()){        mLocationClient.stop();        //关闭方向传感器        mMyOrientationListener.stop();    }}
public class MyOrientationListener implements SensorEventListener{   private SensorManager mSensorManager;   private Context mContext;   private Sensor mSensor;   private float lastX;   public MyOrientationListener(Context context)   {      this.mContext = context;   }   @SuppressWarnings("deprecation")   public void start()   {      mSensorManager = (SensorManager) mContext            .getSystemService(Context.SENSOR_SERVICE);      if (mSensorManager != null)      {         mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);      }      if (mSensor != null)      {         mSensorManager.registerListener(this, mSensor,               SensorManager.SENSOR_DELAY_UI);      }   }   public void stop()   {      mSensorManager.unregisterListener(this);   }   @Override   public void onAccuracyChanged(Sensor arg0, int arg1)   {      // TODO Auto-generated method stub   }   @SuppressWarnings(   { "deprecation" })   @Override   public void onSensorChanged(SensorEvent event)   {      if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)      {         float x = event.values[SensorManager.DATA_X];         if (Math.abs(x - lastX) > 1.0)         {            if (mOnOrientationListener != null)            {               mOnOrientationListener.onOrientationChanged(x);            }         }         lastX = x;      }   }   private OnOrientationListener mOnOrientationListener;   public void setOnOrientationListener(         OnOrientationListener mOnOrientationListener)   {      this.mOnOrientationListener = mOnOrientationListener;   }   public interface OnOrientationListener   {      void onOrientationChanged(float x);   }}







0 0
原创粉丝点击