Android Question phone as compass

canalrun

Well-Known Member
Licensed User
Longtime User
I've been looking into this problem for several years.

I want to orient a Google map based on the compass heading that the phone is pointing.

I've tried using GPS bearing, but that seems to rely on the phone being moving.

I've tried many of the examples posted in the forum on a dozen or so phones. I have also downloaded free apps from Google Play. I've never found anything that seems reliable – except for one app I downloaded, but I don't know how they did it.

I've tried using just the orientation or accelerometer sensor and the newer method of using both the accelerometer sensor and magnetic sensor. The accuracy/reliability I've been able to achieve are less than I would like, even after calibrating the phones.

Is there a newer method for achieving an accurate compass?

Most forum posts I have looked at are several years old – maybe there is a newer Post that I missed?

Thanks,
Barry.
 

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks.

I created a small test app using the code posted, replacing the Log statement with a write to a label.

If I hold my phone vertically and swivel around in my chair, I sort of get accurate numbers a few degrees around North, but nothing much as I swivel through the other 355 degrees. (I'm testing this indoors).

When I hold my phone horizontally, I basically get nothing. (I've tested on several phones, the main one being an LG G3).

I see similar results posted a few posts after the post cited.

On StackOverflow there are many posts essentially using the same two sensors you use. But, I also found one or two posts that use the Geomagnetic_Rotation_Vector sensor. I will try that and see what I get. If I have any success, I will post source code and a small demo app.

It's funny. I downloaded this one free app, Compass (No Ads) by Antoine Vianey, from Google Play. Out of quite a few free compass apps I have downloaded this is the only one that gives accurate numbers when I hold my phone vertically, horizontally, or at any orientation.

I wonder how he did it?

Barry.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
I've uploaded a library with Java source and B4A example project with source. in the zip file, I've also included the compiled APK in case you just want to quickly try it out.

The library sends the compass heading for which the phone is pointing.

It also allows you to enable and configure a smoothing IIR filter to get rid of some of the jitteriness.

I used the sensor type TYPE_ROTATION_VECTOR rather than the usual TYPE_ACCELEROMETER and TYPE_MAGNETIC_FIELD.

Using TYPE_ROTATION_VECTOR seems to perform better on my phones.

I also use the geomagnetic function to correct True North versus Magnetic North.

upload_2017-9-15_17-4-24.png



The attached file contains the source code for the small B4A test app, the compiled APK, the two library files for B4A, and the Java source code for the library.

Barry.
 

Attachments

  • GeoRot2.zip
    152.5 KB · Views: 330
Upvote 0
Top