Basic Digital Compass with True North adjustment

313 views
Skip to first unread message

Scott Ferguson

unread,
Jul 1, 2014, 9:19:35 PM7/1/14
to
This project uses the device OrientationSensor.Azimuth property to display the magnetic heading of the device and optionally to correct for magnetic declination.
By default, the OrientationSensor Azimuth property returns a raw magnetic heading which varies from location to location.
For example, at Los Angeles, California, it would return a heading that is 12 degrees off from True North.
In order to correct for this, a javascript algorithm which takes the arguments current altitude, latitude, longitude and time of year is used.
The values for the arguments are provided by the LocationSensor  and the Clock component.
You can read a detailed explanation of how the LocationSensor works > here <.



You can download the html doc from the Media list in the aia project.

But the really interesting part in that document is the line highlighted in green below that takes a string from AppInventor in this format

"0.0,30.24163,-92.71774,2014.52"

and converts it to => [0.0,30.24163,-92.71774,2014.52]

which is a JSON array.

Then you can use javascript to access each element in the list:

list[2] contains: -92.71774

which is the third element of the array (arrays in javascript start with an index of 0 rather than 1 as in an App Inventor list)

:

<script>

  var list = eval('[' + window.AppInventor.getWebViewString() + ']');

  var alt = list[0];

  var lat = list[1];

  var lon = list[2];

  var yrf = list[3];

  var wmm = new WorldMagneticModel();

  var dec = wmm.declination(alt,lat,lon,yrf); 

  window.document.title = dec;

</script>

:


CompassDeclination.aia
Reply all
Reply to author
Forward
0 new messages