MagnetometerEvent to compass values
Hi, I'm desperately searching for an example on how to get the 0-360 compass values out of the MagnetometerEvent. Any hint is very appreciated. Thanks Joerg
We can do this-
magnetometerEvents.listen((MagnetometerEvent event)
{
double D = 360 - atan2(event.x, event.y) * (180 / pi);
if (D < 0) {
D += 360;
}
print(D);
});
}
For reference - https://arduino.stackexchange.com/questions/18625/converting-three-axis-magnetometer-to-degrees
You can also use this package - https://pub.dev/packages/flutter_compass
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days
The code snippet in https://github.com/fluttercommunity/plus_plugins/issues/903#issuecomment-1158424761 is wrong. I've tried it and it returned 400+ degrees.
- Why?:
atan2(event.x, event.y) * (180 / pi)could return negative numbers, and 360 - negative_number is more than 360. For example, if -45 degrees is returned,360--45is405.
I would highly recommend using https://pub.dev/packages/flutter_compass or writing your own plugin to use the OS APIs to get the data you need directly, instead of using the magnetometer readings (unit: micro Teslas), to allow you to use the operating-system calculated heading, which could use multiple sensors (sensor fusion) to give improved accuracy based on the hardware, as well as compensate for the location (the magnetometer only allows you to calculate magnetic north, not true north).
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days