Time for action – displaying the compass heading
This example is available for download or browse at https://github.com/myleftboot/chapter-7-compass. To display the compass heading, perform the following steps:
Create a new blank mobile app by clicking on File | New | Titanium Project. Don't use a template as it will just generate code that gets in the way.
Create the window for the app:
var win1 = Titanium.UI.createWindow({ backgroundColor:'#fff' });
The layout will simply consist of two labels arranged vertically. One will display the compass bearing and the other label displays a summary of your bearing. Add the labels to the window:
var vertVw = Ti.UI.createView({layout: 'vertical'}); var compassHeading = Ti.UI.createLabel({}); var direction = Ti.UI.createLabel({}); vertVw.add(compassHeading); vertVw.add(direction); win1.add(vertVw);
Add a function that will update one of the labels with the compass bearing and compute the rough direction, which will be applied to the other label. The...