Development Guide

Local Navigation

Retrieving a location using GPS

Your apps can retrieve the GPS location of a BlackBerry device. The values for the location information are returned as the coordinates for latitude, longitude, and altitude.

You can specify the following GPS modes: autonomous mode, assisted mode, and cell site mode. The GPS mode can affect the initial speed of a GPS fix and the level of location accuracy. For example, a weather application might specify cell site mode, which can quickly provide an approximate location. For more information about the BlackBerry device models and their available corresponding GPS modes, visit http://supportforums.blackberry.com/t5/Java-Development/The-BlackBerry-smartphone-models-and-their-corresponding-GPS/ta-p/445329.

To retrieve location information, you can use the Location API, which is made up of the Location API for Java ME (JSR 179), provided in the javax.microedition.location package, or the BlackBerry extension to JSR 179 in the net.rim.device.api.gps package.

The JSR 179 Location API for Java ME is supported on BlackBerry devices that run BlackBerry Device Software 4.0.2 or later.

The BlackBerry extensions to JSR 179 are supported on BlackBerry devices that run BlackBerry Device Software 5.0.0 or later.

Retrieving the GPS location of a BlackBerry device involves the following actions:

  • Specify the GPS mode
  • Retrieve a location provider
  • Make a GPS request that is based on the frequency of the GPS fix
  • Retrieve the GPS location of a BlackBerry device

Code sample: Specifying the GPS mode

/* JSR 179 */
Criteria myCriteria = new Criteria();

/* BlackBerry extension to JSR 179 */
BlackBerryCriteria myBlackBerryCriteria = new BlackBerryCriteria(…);

Code sample: Retrieving a location provider

/* JSR 179 */
LocationProvider myProvider = LocationProvider.getInstance(myCriteria);

/* BlackBerry extension to JSR 179 */
BlackBerryLocationProvider myBlackBerryProvider =
    (BlackBerryLocationProvider) LocationProvider.getInstance(myBlackBerryCriteria);

Code sample: Making a GPS request that is based on the frequency of the GPS fix

/*
 * Single GPS fix
 */
/* JSR 179 */
Location myLoc = myProvider.getLocation(…);

/* BlackBerry extension to JSR 179 */
BlackBerryLocation myBlackBerryLoc = myBlackBerryProvider.getLocation(…);

/*
 * Continuous GPS fixes
 */
/* JSR 179 */
myProvider.setLocationListener(…);

/* BlackBerry extension to JSR 179 */
myBlackBerryProvider.setLocationListener(…);

Code sample: Retrieving the GPS location of a BlackBerry device

/* JSR 179 */
double lat = myLoc.getQualifiedCoordinates().getLatitude();

/* BlackBerry extension to JSR 179 */
double lat = myBlackBerryLoc.getQualifiedCoordinates().getLatitude();

Was this information helpful? Send us your comments.