[WP 8.1 Development] electronic compass

Source: Internet
Author: User

The compass doesn't need to be introduced too much. Anyone who has studied junior high school physics knows that no matter whether it is a compass or a northern finger, its essence is to identify the direction.

It is not complicated to operate the electronic compass. There are two main points:

1. The angle between the current direction and the magnetic north;

2. The angle between the current direction and the ground north.

At the same time, we also learned that the geographic North Pole and the geomagnetic North Pole are not overlapped, and there is a geographic angle. When reading the Electronic Compass data, you can give priority to the angle between reading and the Arctic, if you cannot read the angle of the Arctic, then read the angle data of the Arctic.

The compassreading class (located in the windows. devices. Sensors namespace) encapsulates data read from the Electronic Compass.

1. headingmagneticnorth attribute: obtains the angle between the current direction and the magnetic north.

2. headingtruenorth attribute: This attribute obtains the real north angle, that is, the geographical angle. We noticed that its type is double ?, That is, nullable <double> indicates that the value may be null, that is, the value of the angle between the north and the earth may not be obtained. In use, we can first determine whether the north corner of the ground is null. If it is not null, this value is used. If it is null, the north corner of the magnetic field is used.

3. headingaccuracy attribute: it indicates the accuracy of the compass reading. If the reading accuracy is high, it returns High. Our application can check if this attribute is high when appropriate. If not, consider prompting the user to calibrate the compass.

When it comes to calibration, it is worth noting that the system prompts are not required to calibrate the compass, and the application may not prompt. You can prompt the user for calibration only considering the user experience. The calibration method is simple, whether you use a map application that comes with WP, other third-party applications, or applications developed by yourself. As long as you are using the compass, you can move back and forth several "8" characters in the air with your mobile phone, without waiting for a prompt, you can calibrate the data whenever you use the compass.

The API of the electronic compass is encapsulated in windows. devices. sensors. in the compass class, all the sensors in the wp api call is very simple. First, an instance of a sensor class is obtained. Generally, it can be returned through the getdefault method (static method, then set the read interval, in milliseconds. If you want to read data once per second, set reportinterval to 1000. However, the interval cannot be less than the value specified by the minimumreportinterval attribute, it should be noted that setting the interval to 20 ms or more has a good effect. Of course, it depends on the actual situation you are using.

Finally, the readingchanged event is processed. When new data is read, the event is triggered and the newly read data is passed to the event, we can get the latest reading from the event parameters.

 

Well, the theory will always be abstract. Let's take a look at a simple compass that I have made. It is really simple. I hope you will not smile, because I am a good cook, therefore, you can use XAML to directly draw a compass on the home page.

First.

Look, it's simple. There's no way. It's just like a poor person. You need a simple vegetarian clothing. I painted the background of the compass in several circles, and the pointer to the direction was painted with the path element.

 

The principle is as follows:

According to the angle read by the electronic compass, the red pointer object is rotated using the rotatetransform class. But pay attention to the rotation angle. For example, if my current direction is 20 degrees, that is, the north-northeast direction, how can I set the angle to ensure that the red pointer always points to the south.

We know that the angle is based on the north. If you want to direct the pointer to the south, you can add 180 to the angle read by the compass, because the angle between the North and the South is exactly 180 degrees (angular angle), another method is to let the initial position of the pointer down, that is, to direct to the south, I use this method ..

You can rotate them at the same angle, either in the North or in the South, because the angle is exactly 180.

 

So the second problem arises-how many degrees of rotation is appropriate? The above example shows that the current direction is 20 degrees, and the reference standard is the Arctic. That is to say, our mobile phone is already in the 20 degrees direction. If we rotate the pointer 20 degrees, then the pointer is relative to the screen, the deflection angle is 40 degrees. It would have been 20 degrees. If you turn 20 degrees again, it will be doubled. Obviously, this is not the case. We must offset this angle difference.

That is to say, if my current direction is 20 degrees, then the Rotation Transformation of the pointer should be-20 degrees, so that we can flatten the angle of deviation. Otherwise, you will know it if you try, if the offset is not exceeded, the pointer is more and more biased.

Another method is to use 360 degrees as the subtrahend. For example, 360-20 = 340 degrees, while-20 degrees and 340 degrees are in different directions, their positions are the same. Therefore, both methods can be used. For example, if the reading speed is 80 degrees, you can rotate the pointer-80 degrees or 360-80 degrees.

Async void _ compass_readingchanged (compass sender, compassreadingchangedeventargs ARGs) {var res = args. reading; // if the angle value of the ground North Pole is not available, use the magnetic north pole angle double val = res. headingtruenorth ?? Res. headingmagneticnorth; await dispatcher. runasync (windows. UI. core. coredispatcherpriority. normal, () => {// If the accuracy is high, stop calibration if (ischecking & res. headingaccuracy = magnetometeraccuracy. high) {fly. hide (); ischecking = false;} // The rotation direction is the negative value rotattransform of the compass angle. angle =-val; display (VAL );});}

The display method is a self-defined method used to determine the direction of the reading and display it in text on the page. For example, the reading value is 0, that is, "North", the reading value is 180 degrees, and the reading value is "South.

/// <Summary> /// Display orientation /// </Summary> private void display (Double V) {string d = ""; int ind = convert. toint32 (V); If (IND = 0 | ind = 360) {d = "North";} else if (IND = 90) {d = "正 ";} else if (IND = 180) {d = "正 ";} else if (IND = 270) {d = "正 ";} else if (IND> 0 & ind <90) {d = "Northeast";} else if (IND> 90 & ind <180) {d = "southeast ";} else if (IND> 180 & ind <270) {d = "Southwest China";} else if (IND> 270 & ind <360) {d = "northwest China ";} TBW. TEXT = string. format ("{0} ° ({1})", IND, d );}

 

The above lists the key code snippets. For other codes, refer to the sample source code uploaded by me.

: Http://files.cnblogs.com/tcjiaan/CompassApp.zip

 

[WP 8.1 Development] electronic compass

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.