UWP development direction sensor and UWP development direction Sensor
In UWP development, we can use three methods in the following directions: four elements in OrientationSensor, HeadingMagneticNorth of Compass, and SimpleOrientationSensor.
SimpleOrientationSensor is used in simple cases. It can only read six directions: 0, 90, 180,270, FaceUP, and FaceDown.
var simpleOrien = SimpleOrientationSensor.GetDefault().GetCurrentOrientation(); switch (simpleOrien) { case SimpleOrientation.Facedown: // To do ... break; case SimpleOrientation.Faceup: // To do ... break; case SimpleOrientation.NotRotated: // To do ... break; case SimpleOrientation.Rotated180DegreesCounterclockwise: // To do ... break; case SimpleOrientation.Rotated270DegreesCounterclockwise: // To do ... break; case SimpleOrientation.Rotated90DegreesCounterclockwise: // To do ... break; }
Compass:
var compass = Compass.GetDefault().GetCurrentReading(); var angle= compass.HeadingMagneticNorth;
It is easy to use, but it seems that you are moving in the direction, that is, if you do not move, the reading is meaningless.
Direction sensor:
var _sensor = OrientationSensor.GetDefault(); var quaternion = _sensor.GetCurrentReading().Quaternion; var heading = 180 * Math.Atan2(2 * (quaternion.W * quaternion.Z + quaternion.X * quaternion.Y), 1 - 2 * (quaternion.Y * quaternion.Y + quaternion.Z * quaternion.Z)) / Math.PI;
This may be a little troublesome, because it involves the four-dimensional element. For information about the four-dimensional element, google or flip the book.