Search code examples
androidandroid-studioandroid-sensorscompassmagnetometer

Android point direction with 2 locations


I'm actually coding an application on Android and I need to point a certain direction, explaination, I have a position that is fixed and the position of my smartphone (the two in Longitude Latitude) and I want to point an arrow to the direction between the two. So I searched like hell, I have a compass that works perfectly and I tried to change pointing direction by changing the north direction get by the getRotationMatrix stuff but well after loosing my brain trying I don't know what to do xD So yeah I need some ideas and hints.

Here's my code just in case (I just past the onSensorChanged func the rest is just basic initialisations)

@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;
    }
}

Solution

  • Ok I managed something. I took my position, the fixed location and a third location with the exact same same longitude and a latitude of 90. With that I have two vectors one pointing the north and one on the straight line with our location and the fixed location. I calculate the angle between these vectors and I add this angle to the rotation animation. I don't think my solution is that good but as I didn't find anything else on forums I post an answer ^^.