Exemplo de código: Recuperar um ponto único otimizado
O seguinte exemplo de código demonstra como criar um aplicativo de interface de usuário que fornece o ponto de localização mais rápido disponível, que pode vir de uma localização geográfica ou de uma fonte de localização de GPS.
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());
}
});
}
}
Próximo tópico: Recuperar múltiplos pontos otimizados
Tópico anterior: Recuperar o ponto único otimizado
Estas informações foram úteis? Gostaríamos de receber seus comentários.