Andoid百度地图显示方向

    private BaiduMap mBaiduMap = null;
    private MapView mMapView = null;
    private LocationClient mLocClient = null;
    private MyLocationConfiguration.LocationMode mCurrentMode;
    private Boolean isFirstLoc = true;
    private BitmapDescriptor mCurrentMarker=null;

    private float[] accelerometerValues=new float[3];
    private float[] magneticFieldValues=new float[3];
    private float[] values=new float[3];
    private float[] rotate=new float[9];
    private float[] degree=new float[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);

        SensorManager sm = (SensorManager) getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
        SensorEventListener sensorEventListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
                    accelerometerValues=event.values;
                }
                if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD){
                    magneticFieldValues=event.values;
                }
                else
                {
                    return;
                }

                SensorManager.getRotationMatrix(rotate, null, accelerometerValues, magneticFieldValues);
                SensorManager.getOrientation(rotate, values);

                //rotate
                degree[0] = values[0]/3.1415926f*180;
                //up down
                degree[1] = values[1]/3.1415926f*180;
                //left right
                degree[2] = values[2]/3.1415926f*180;
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }
        };

        sm.registerListener(sensorEventListener,sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(sensorEventListener,sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_NORMAL);

        // init baiduMap
        mMapView = (MapView) findViewById(R.id.dbmapView);
        mBaiduMap = mMapView.getMap();

        // set parameters
        mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING;
        mCurrentMarker=null;
        mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));

        // allow loaction
        mBaiduMap.setMyLocationEnabled(true);
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(new MyLocationListenner());
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        option.setCoorType("bd09ll");
        option.setScanSpan(1000);
        mLocClient.setLocOption(option);
        mLocClient.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    public class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null || mMapView == null)
                return;

            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    .direction(degree[0])
                    .latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);

            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                mBaiduMap.animateMapStatus(u);
            }
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

*