개발 가이드

Local Navigation

코드 샘플: 최적 단일 수정값 검색

다음 코드 샘플은 Geolocation 또는 GPS 위치 소스에서 검색될 수 있는 사용 가능한 위치 수정값 중 가장 빠른 수정값을 제공하는 UI 프로그램을 만드는 방법을 보여 줍니다.

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());
            }
        });
    }
}

이 정보가 도움이 되었습니까? 의견을 보내 주십시오.