The Compass of the Windows Phone

I've been involved in the electronic compass of the Windows Phone, and it seems not that difficult.

The Compass class is in the Microsoft.Devices.Sensors namespace, and must add the referrence before using it.

private Compass _compass;

//...

if (Compass.IsSupported)
{
    _compass = new Compass();
    _compass.CurrentValueChanged += CompassCurrentValueChanged;
    _compass.Start();
}

That's the initialization of the compass.

private void CompassCurrentValueChanged(object sender, SensorReadingEventArgs<CompassReading> e)
{
    Dispatcher.BeginInvoke(() =>
        {
            Direction.Text = e.SensorReading.TrueHeading.ToString(CultureInfo.InvariantCulture);
        });
}

The SensorReading has about 4 useful values, if you only want to know where the north is, you can take the TrueHeading value and it's the angle between the north direction and the direction which is the positive y axis direction of your phone.

原文地址:https://www.cnblogs.com/boyweb/p/3267011.html