Retrieving the optimal fix with GPS and geolocation
You can retrieve the optimal location fix by requesting geolocation updates along with GPS updates. An optimal fix provides a geolocation fix when a GPS fix cannot be retrieved during the specified timeout period. You can use this request in an application that requires the location of a BlackBerry device at all times, and you are not concernced about how the location is retrieved.
The BlackBerryCriteria class, in the net.rim.device.api.gps package, provides the following methods for retrieving the optimal fix:
|
Method |
Description |
|---|---|
|
enableGeolocationWithGPS() |
This method returns a GPS fix as soon as it is available or within the timeout period that is specified in the location request. If the GPS fix is unavailable, a geolocation fix is returned. You can use this method for single or multiple fix requests. |
|
enableGeolocationWithGPS(BlackBerryCriteria.FASTEST_FIX_PREFERRED) |
This method returns the first available fix, regardless of the location source (GPS or geolocation). During the timeout period that is specified in the location request, the first fix that is available from a location source is provided to the application. You can use this mode only for single fix requests. |
Retrieve the optimal single fix
The following steps demonstrate how to create a UI application that provides the fastest available location fix, which may come from a geolocation or GPS location source. The location coordinates and the mode that the application uses to retrieve the location are displayed on the screen.
Code sample: Retrieving the optimal single fix
The following code sample demonstrates how to create a UI application that provides the fastest available location fix, which may come from a geolocation or GPS location source.
import net.rim.device.api.gps.*;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import javax.microedition.location.*;
public final class SingleFixDemo extends UiApplication
{
public static void main(String[] args)
{
SingleFixDemo theApp = new SingleFixDemo();
theApp.enterEventDispatcher();
}
public SingleFixDemo()
{
pushScreen(new SingleFixScreen());
}
}
class SingleFixScreen extends MainScreen
{
private LabelField _coordLabel;
private double _latitude;
private double _longitude;
private int _modeUsed;
private String _mode;
public SingleFixScreen()
{
super(DEFAULT_CLOSE | DEFAULT_MENU);
setTitle(new LabelField("Single Fix Demo" , Field.USE_ALL_WIDTH |
DrawStyle.HCENTER));
ButtonField locButton = new ButtonField("Get location fix",
ButtonField.CONSUME_CLICK);
locButton.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
findLocation();
}
});
add(locButton);
this._coordLabel = new LabelField();
add(this._coordLabel);
}
private void findLocation()
{
this._coordLabel.setText("");
Thread locThread = new Thread()
{
public void run()
{
try
{
BlackBerryCriteria myCriteria = new BlackBerryCriteria();
myCriteria.enableGeolocationWithGPS(BlackBerryCriteria
.FASTEST_FIX_PREFERRED);
try
{
BlackBerryLocationProvider myProvider =
(BlackBerryLocationProvider)LocationProvider
.getInstance(myCriteria);
try
{
BlackBerryLocation myLocation = (BlackBerryLocation)
myProvider.getLocation(-1);
_longitude = myLocation.getQualifiedCoordinates()
.getLongitude();
_latitude = myLocation.getQualifiedCoordinates()
.getLatitude();
_modeUsed = myLocation.getGPSMode();
switch (_modeUsed)
{
case LocationInfo.GEOLOCATION_MODE:
case LocationInfo.GEOLOCATION_MODE_CELL:
case LocationInfo.GEOLOCATION_MODE_WLAN:
_mode = "Geolocation";
break;
default:
_mode = "GPS";
}
StringBuffer sb = new StringBuffer();
sb.append("Longitude: ");
sb.append(_longitude);
sb.append("\n");
sb.append("Latitude: ");
sb.append(_latitude);
sb.append("\n");
sb.append("Mode: ");
sb.append(_mode);
String msg = sb.toString();
showResults(msg);
}
catch (InterruptedException e)
{
showException(e);
}
catch (LocationException e)
{
showException(e);
}
}
catch (LocationException e)
{
showException(e);
}
}
catch (UnsupportedOperationException e)
{
showException(e);
}
}
};
locThread.start();
}
private void showResults(final String msg)
{
Application.getApplication().invokeLater(new Runnable()
{
public void run()
{
SingleFixScreen.this._coordLabel.setText(msg);
}
});
}
private void showException(final Exception e)
{
Application.getApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert(e.getMessage());
}
});
}
}
Retrieve optimal multiple fixes
The following steps demonstrate how to create a UI application that provides continuous location updates by retrieving GPS fixes when the fixes are available or within the specified timeout period. If the GPS fix is unavailable, a geolocation fix is returned instead.
Code sample: Retrieving optimal multiple fixes
The following code sample demonstrates how to create a UI application that provides continuous location updates by retrieving GPS fixes when the fixes are available or within the specified timeout period. If the GPS fix is unavailable, a geolocation fix is returned instead.
import net.rim.device.api.gps.*;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import javax.microedition.location.*;
public class MultipleFixDemo extends UiApplication
{
private static int _interval = 1;
private EditField _status;
public static void main(String[] args)
{
new MultipleFixDemo().enterEventDispatcher();
}
public MultipleFixDemo()
{
MultipleFixScreen screen = new MultipleFixScreen();
screen.setTitle("Multiple Fix Demo");
_status = new EditField(Field.NON_FOCUSABLE);
screen.add(_status);
startLocationUpdate();
pushScreen(screen);
}
private void updateLocationScreen(final String msg)
{
invokeLater(new Runnable()
{
public void run()
{
_status.setText(msg);
}
});
}
private void startLocationUpdate()
{
try
{
BlackBerryCriteria myCriteria = new BlackBerryCriteria();
myCriteria.enableGeolocationWithGPS();
try
{
BlackBerryLocationProvider myProvider =
(BlackBerryLocationProvider)LocationProvider
.getInstance(myCriteria);
if ( myProvider == null )
{
Runnable showUnsupportedDialog = new Runnable()
{
public void run() {
Dialog.alert("Location service unsupported,
exiting...");
System.exit( 1 );
}
};
invokeLater( showUnsupportedDialog );
}
else
{
myProvider.setLocationListener(new LocationListenerImpl(),
_interval, 1, 1);
}
}
catch (LocationException le)
{
System.err.println("Failed to retrieve a location provider");
System.err.println(le);
System.exit(0);
}
}
catch (UnsupportedOperationException ue)
{
System.err.println("Require mode is unavailable");
System.err.println(ue);
System.exit(0);
}
return;
}
private class LocationListenerImpl implements LocationListener
{
public void locationUpdated(LocationProvider provider,
Location location)
{
if(location.isValid())
{
double longitude = location.getQualifiedCoordinates()
.getLongitude();
double latitude = location.getQualifiedCoordinates()
.getLatitude();
float altitude = location.getQualifiedCoordinates()
.getAltitude();
StringBuffer sb = new StringBuffer();
sb.append("Longitude: ");
sb.append(longitude);
sb.append("\n");
sb.append("Latitude: ");
sb.append(latitude);
sb.append("\n");
sb.append("Altitude: ");
sb.append(altitude);
sb.append(" m");
MultipleFixDemo.this.updateLocationScreen(sb.toString());
}
}
public void providerStateChanged(LocationProvider provider,
int newState)
{
// Not implemented
}
}
private final static class MultipleFixScreen extends MainScreen
{
MultipleFixScreen()
{
super(DEFAULT_CLOSE | DEFAULT_MENU);
RichTextField instructions = new RichTextField(
"Waiting for location update...",Field.NON_FOCUSABLE);
this.add(instructions);
}
}
}