Retrieve the location of a BlackBerry device

  1. Import the required classes.
    import javax.microedition.location.*;
  2. Create a class and a constructor.
    public class handleGPS
    {
       public handleGPS()
       {
       }
    }
  3. Declare static fields in the class.
    static GPSThread gpsThread;
    static double latitude;
    static double longitude;
  4. In the constructor, create and start a local thread.
    gpsThread = new GPSThread();
    gpsThread.start();
  5. In the class, create a private class that extends Thread, and create a run() method.
    private class GPSThread extends Thread
    {
       public void run()
       {
       }
    }
  6. In the run() method, create an instance of the Criteria class. Invoke setCostAllowed(false) to specify that the autonomous mode.
    Criteria myCriteria = new Criteria();
    myCriteria.setCostAllowed(false);
  7. In the run() method, create a try/catch block. In the block create a LocationProvider object by getting an instance of the Criteria object. Create another try/catch block to create a Location object to request the current location of the BlackBerry® device and specify the timeout period in seconds. When the getLocation() method returns, request the latitude and longitude coordinates.
    try
    {
        LocationProvider myLocationProvider =
            LocationProvider.getInstance(myCriteria);
    
        try
        {
            Location myLocation = myLocationProvider.getLocation(300);
            latitude  = myLocation.getQualifiedCoordinates().getLatitude();
            longitude = myLocation.getQualifiedCoordinates().getLongitude();
        }
        catch ( InterruptedException iex )
        {
            return;
        }
        catch ( LocationException lex )
        {
            return;
        }
    }
    catch ( LocationException lex )
    {
        return;
    }
    return;

Was this information helpful? Send us your comments.