Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

Determining the direction or bearing

Retrieving the bearing between two locations

The net.rim.device.api.gps.LocationInfo class provides a method that allows you to retrieve the bearing between two locations. The LocationInfo.getBearing method receives the coordinates for a start location and an end location of a course as parameters, and returns the bearing. The bearing is an angle (in degrees) between a line connecting two points (the origin and the destination) and a north-south line. For example, if the end location is directly northeast of the start location, the bearing is 45 degrees.

double bearing = LocationInfo.getBearing(lat1, lon1, lat2, lon2);

Adding a compass field to an app

The net.rim.device.api.lbs.compass.CompassField class allows you to add a visual representation of a compass rose to your app, without requiring that you implement the Magnetometer APIs. CompassField objects can be created and added to a field manager, just like any other UI component. After you add a CompassField object to a screen, the field listens to the magnetometer sensor for changes to the bearing of the BlackBerry device, and re-renders the field accordingly.

Since the compass field relies on the magnetometer sensor to function correctly, the device might need to be calibrated occasionally so that the compass field displays an accurate heading (such as when the device comes in contact with a magnetic field). When device calibration is required, a prompt appears that instructs the user to calibrate the device. The calibration prompt generated by the CompassField class is the same as the prompt generated by the Compass application.

Once the compass field is no longer required, you must close it to ensure that your app releases the resources that are associated with it.

Adding a compass field to a screen

CompassField compField = new CompassField();

Releasing a compass field's resources


Code sample: Adding a compass field to an app

import net.rim.device.api.lbs.compass.*;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

public class CompassFieldDemo extends UiApplication
    public static final void main(String[] args)
		      new CompassFieldDemo().enterEventDispatcher();
	   public CompassFieldDemo()
		      pushScreen(new CompassFieldDemoScreen());
	   private class CompassFieldDemoScreen extends MainScreen
		      CompassGLField _compField;
		      public CompassFieldDemoScreen()
            // Create and add the compass field.
			         _compField = new CompassGLField();
		      public boolean onClose()
            // Close the compass field to release its resources.
			         return true;