코드 샘플: 여러 개의 최적 수정값 검색
다음 코드 샘플은 GPS 수정값을 사용할 수 있거나 지정된 시간 범위 내에 있는 경우에 GPS 수정값을 검색하여 위치를 지속적으로 업데이트하는 UI 프로그램을 만드는 방법을 보여 줍니다. GPS 수정값을 사용할 수 없으면 Geolocation 수정값이 대신 반환됩니다.
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);
}
}
}
다음 주제: GPS와 Geolocation 업데이트 동시 요청
이전 주제: 여러 개의 최적 수정값 검색