Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:44:07+00:00 2026-06-14T00:44:07+00:00

I am trying to program a compass with Android using the accelorometer and the

  • 0

I am trying to program a compass with Android using the accelorometer and the magnetic field sensor, now I am wondering how to get the correct angle for my compass.

I read in the values of the accelerometer and of the magnetic field sensor in “accele”, and “magne” respectively. To get the angle, I perform the following:

float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, accele, magne);
        if(success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            azimuth = orientation[0]; // contains azimuth, pitch, roll
                            ....

Later on, I use a rotation matrix to put my needle:

rotation.setRotate(azimuth, compass.getWidth() / 2, compass.getHeight() / 2);
canvas.drawBitmap(needle, rotation, null);

Now, the documentation of getOrientation says, that orientation[0] should be the rotation around the z-axis. The documentation for TYPE_ORIENTATION states that
“Azimuth, angle between the magnetic north direction and the y-axis, around the z-axis (0 to 359). 0=North, 90=East, 180=South, 270=West”.

My azimuth however is not between 0 and 359, but rather around -2 to 2. What exactly is the azimuth from getOrientation and how can I convert it to an angle?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:44:08+00:00Added an answer on June 14, 2026 at 12:44 am

    Use the following to convert from the given azimuth in radians (-PI, +PI) to degrees (0, 360)

    float azimuthInRadians = orientation[0];
    float azimuthInDegress = (float)Math.toDegrees(azimuthInRadians);
    if (azimuthInDegress < 0.0f) {
        azimuthInDegress += 360.0f;
    }
    

    variable names used for convenience 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to program Optical flow on android device. My problem is to get
I am trying to program a web-crawler, but now I am wondering: What’s the
I'm trying to program my first GUI-class in Java, using the Window Builder. Ok,
I'm trying to program a graph class using an adjacent list from an example
I'm trying to program ARM using Eclipse + CDT + yagarto (gnu toolchain) +
I am trying a program with Swing. I am using a socket to connect
I'm trying to program an Android interface which uses an expandable list on the
I've been writing a program trying to find itself using the procps library. But
I'm trying to program a C++ module for node.js. Node is using waf as
I'm trying to program a game for android phones. Anyway I have to check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.