1

I want to get true and magnetic heading of compass in android.

I have read many tutorials but not give my required output.

It gives just heading.

My code is here...

public class MainActivity extends Activity implements SensorEventListener {


// device sensor manager
private SensorManager mSensorManager;

TextView tvHeading;

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


    // TextView that will tell the user what degree is he heading
    tvHeading = (TextView) findViewById(R.id.tvHeading);

    // initialize your android device sensor capabilities
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}

@Override
protected void onResume() {
    super.onResume();

    // for the system's orientation sensor registered listeners
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause() {
    super.onPause();

    // to stop the listener and save battery
    mSensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {

    // get the angle around the z-axis rotated
    float degree = Math.round(event.values[0]);

    tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // not in use
}
}

This code give accurate value for a compass.

How can i modify this code for getting magnetic heading and true heading?

what is missing here?

Any one can help me.

Thanks

Vanarajan
  • 539
  • 1
  • 4
  • 19
NadeemYousaf
  • 233
  • 1
  • 3
  • 12
  • This is too board, voting to close. You need a LOT more than this to get true (I assume that you mean geographic) north. You need to know your current location and the magnetic deviation for that location. – Simon Jan 30 '14 at 11:09
  • @simon Can you suggest some example? – NadeemYousaf Jan 30 '14 at 11:12
  • http://stackoverflow.com/questions/2808811/determining-north-using-the-android-phone – Simon Jan 30 '14 at 11:17

0 Answers0