Example usage for android.hardware SensorManager MAGNETIC_FIELD_EARTH_MAX

List of usage examples for android.hardware SensorManager MAGNETIC_FIELD_EARTH_MAX

Introduction

In this page you can find the example usage for android.hardware SensorManager MAGNETIC_FIELD_EARTH_MAX.

Prototype

float MAGNETIC_FIELD_EARTH_MAX

To view the source code for android.hardware SensorManager MAGNETIC_FIELD_EARTH_MAX.

Click Source Link

Document

Maximum magnetic field on Earth's surface

Usage

From source file:com.spoiledmilk.ibikecph.navigation.SMRouteNavigationMapFragment.java

public void onSensorChanged(SensorEvent event) {

    if (event.sensor == mAccelerometer) {
        accValues.put(event.values);//from w  ww  . ja v a2  s  .c  o m
        // System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
        mLastAccelerometerSet = true;
    } else if (event.sensor == mMagnetometer) {
        if (Math.sqrt(event.values[0] * event.values[0] + event.values[1] * event.values[1]
                + event.values[2] * event.values[2]) > SensorManager.MAGNETIC_FIELD_EARTH_MAX * 1.5) {
            return;
        }
        if (event.values.length < 3) {
            return;
        }
        magValues.put(event.values);
        // System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
        mLastMagnetometerSet = true;
    }

    if (mLastAccelerometerSet && mLastMagnetometerSet) {
        SensorManager.getRotationMatrix(mR, null, accValues.get(), magValues.get());
        // SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_X,
        // SensorManager.AXIS_Z, remapMatrix);
        SensorManager.getOrientation(mR, mOrientation); // remapMatrx
        if (SMLocationManager.getInstance().hasValidLocation()) {
            Location currentLoc = SMLocationManager.getInstance().getLastValidLocation();
            float azimuth = -(float) Math.toDegrees(mOrientation[0]);
            final GeomagneticField geoField = new GeomagneticField(
                    Double.valueOf(currentLoc.getLatitude()).floatValue(),
                    Double.valueOf(currentLoc.getLongitude()).floatValue(),
                    Double.valueOf(currentLoc.getAltitude()).floatValue(), System.currentTimeMillis());
            azimuth += geoField.getDeclination(); // converts magnetic north
            // into true north
            // LOG.d("azimuth = " + azimuth);
            compassOrientation = azimuth;// - bearing;
            // if (Math.abs(locationOverlay.compassOrientation - compassOrientation) > 5) {
            // locationOverlay.compassOrientation = compassOrientation;
            // mapView.invalidate();
            // }
            // }
            // }
        }
    }
}