2

I'm trying to make a compass that points at a specific Longitude/Latitude, but I don't know how this is made, so I'll appreciate any help :)

This is the compass. What do I've to add/change so that I can decide the Longitude/Latitude?

public class MainActivity extends Activity implements SensorEventListener {

private ImageView mPointer;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mMagnetometer;
private float[] mLastAccelerometer = new float[3];
private float[] mLastMagnetometer = new float[3];
private boolean mLastAccelerometerSet = false;
private boolean mLastMagnetometerSet = false;
private float[] mR = new float[9];
private float[] mOrientation = new float[3];
private float mCurrentDegree = 0f;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mPointer = (ImageView) findViewById(R.id.pointer);
}

protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME);
}

protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this, mAccelerometer);
    mSensorManager.unregisterListener(this, mMagnetometer);
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor == mAccelerometer) {
        System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
        mLastAccelerometerSet = true;
    } else if (event.sensor == mMagnetometer) {
        System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
        mLastMagnetometerSet = true;
    }
    if (mLastAccelerometerSet && mLastMagnetometerSet) {
        SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
        SensorManager.getOrientation(mR, mOrientation);
        float azimuthInRadians = mOrientation[0];
        float azimuthInDegress = (float)(Math.toDegrees(azimuthInRadians)+360)%360;
        RotateAnimation ra = new RotateAnimation(
                mCurrentDegree, 
                -azimuthInDegress,
                Animation.RELATIVE_TO_SELF, 0.5f, 
                Animation.RELATIVE_TO_SELF,
                0.5f);

        ra.setDuration(250);

        ra.setFillAfter(true);

        mPointer.startAnimation(ra);
        mCurrentDegree = -azimuthInDegress;
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

}

Maybe it's a little unclear. Let's say that I'm outside and I want to know the direction of a specific place. This app should help me finding the direction

Nasc
  • 65
  • 9

1 Answers1

0

You do not need compass for this. You need to get the current location and then use the method bearingTo in the Location class.

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54