Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Compass Bearing

I am trying to get the compass bearing in degrees (i.e. 0-360) using the following method:

float[] mGravity;
float[] mGeomagnetic;
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        mGeomagnetic = event.values;
    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I, mGravity,
                mGeomagnetic);
        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            float azimut = orientation[0];
            bearing.setText("Bearing: "+ azimut);
        }
    }
}

The azimuth value (i.e. orientation[0]) should be 0<=azimuth<360 but I am getting only values from -3 to 3 as I rotate my device. Can someone please tell me what the problem might be please?

like image 762
duncanportelli Avatar asked Mar 01 '13 10:03

duncanportelli


People also ask

What is bearing in location Android?

In this an arrow on compass shows the direction from your location to Kaaba(destination Location) you can simple use bearingTo in this way.bearing to will give you the direct angle from your location to destination location.

How do I find my bearings with a compass?

Be sure the north end of the needle (usually red) points to N, not S. Find where the degree markings around the azimuth ring line up with the direction-of-travel arrow. That degree mark is your bearing.

What does bearing on a compass mean?

In navigation, bearing is the horizontal angle between the direction of an object and another object, or between it and that of true north. Absolute bearing refers to the angle between the magnetic north (magnetic bearing) or true north (true bearing) and an object.

What compass bearing is 135 degrees?

An azimuth of 135 degrees is the same as the bearing 45 degrees East of South (S 45 E). There are special compasses if you want to express direction this way.


3 Answers

It is true that it is in Radians. Thanks Hoan. I added some logic to get that bearing in degrees from 0 to 360 becuase if I only converted it to degrees, I was getting values from -180 to 180.

float azimuthInRadians = orientation[0];
float azimuthInDegress = (float)Math.toDegrees(azimuthInRadians)+360)%360;
like image 114
duncanportelli Avatar answered Nov 06 '22 08:11

duncanportelli


// This answer applies to Google Maps api v2.
// It is possible by registering your application with Sensor Listener for Orientation and get the
// angle relative to true north inside onSensorChanged and update camera accordingly.
// Angle can be used for bearing. Following code can be used:

// Instead of using Sensor.TYPE_ORIENTATION try using getOrinetation api. Sensor.TYPE_ORIENTATION
// has been deprecated.

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    if (sensorManager != null)
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
}

public void onSensorChanged(SensorEvent event) {

    float compassBearingRelativeToTrueNorth = Math.round(event.values[0]);

    Log.d(TAG, "Degree ---------- " + degree);

    updateCamera(compassBearingRelativeToTrueNorth);

}

private void updateCamera(float bearing) {
    CameraPosition oldPos = googleMap.getCameraPosition();

    CameraPosition pos = CameraPosition.builder(oldPos).bearing(bearing)
            .build();

    googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(pos));

}
like image 31
Prashant Avatar answered Nov 06 '22 08:11

Prashant


The values are in radian, you have to convert to degree of arc

int azimut = (int) Math.round(Math.toDegrees(orientation[0]));
like image 42
Hoan Nguyen Avatar answered Nov 06 '22 10:11

Hoan Nguyen