Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Compass.GetCurrentReading | getCurrentReading method

Applies to Windows and Windows Phone

Gets the current compass reading.

Syntax


var compassReading = compass.getCurrentReading();

Parameters

This method has no parameters.

Return value

Type: CompassReading

The current compass reading.

Remarks

An application may use this method to poll the sensor for the current reading as an alternative to registering a ReadingChanged event handler. This would be the preferred alternative for an application that updates its user interface at a specific frame rate. Whether polling once or many times, the application must establish a desired ReportInterval. This informs the sensor driver that resources should be allocated to satisfy subsequent polling requests

The returned value is a magnetic heading specified in degrees.

The accuracy of this value is dependent on the capabilities of the compass.

Before using the return value from this method, the application must first check that the value is not null. (If the value is null and you attempt to retrieve it, Windows will generate an exception.)

The following example demonstrates how a Windows Store app built with XAML and C# retrieves the current heading for the default compass.


private void DisplayCurrentReading(object sender, object args)
{
    CompassReading reading = _compass.GetCurrentReading();
    if (reading != null)
    {
        ScenarioOutput_MagneticNorth.Text = String.Format("{0,5:0.00}", reading.HeadingMagneticNorth);
        if (reading.HeadingTrueNorth != null)
        {
            ScenarioOutput_TrueNorth.Text = String.Format("{0,5:0.00}", reading.HeadingTrueNorth);
        }
        else
        {
            ScenarioOutput_TrueNorth.Text = "No data";
        }
    }
}


The following example demonstrates how a Windows Store app built for Windows using JavaScript retrieves the current heading for the default compass.


function getCurrentReading() {
    var reading = compass.getCurrentReading();
    if (reading) {
        document.getElementById("readingOutputMagneticNorth").innerHTML = reading.headingMagneticNorth.toFixed(2);
        if (reading.headingTrueNorth) {
            document.getElementById("readingOutputTrueNorth").innerHTML = reading.headingTrueNorth.toFixed(2);
        } else {
            document.getElementById("readingOutputTrueNorth").innerHTML = "no data";
        }
    }
}


Requirements

Minimum supported client

Windows 8

Minimum supported server

None supported

Minimum supported phone

Windows Phone 8

Namespace

Windows.Devices.Sensors
Windows::Devices::Sensors [C++]

Metadata

Windows.winmd

See also

Compass

 

 

Show:
© 2014 Microsoft