如何获得Blackberry当前位置的纬度和经度?

时间:2012-01-03 10:58:13

标签: blackberry gps

我正在开发一款具有GPS功能的应用。如何获得当前位置的纬度和经度。

3 个答案:

答案 0 :(得分:1)

请查看以下链接:

另请参阅Eclipse插件提供的示例应用程序,该插件名为 GPSDemo

答案 1 :(得分:1)

我自己找到了解决方案。以下代码适用于我:

package mypackage;

import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

public final class MyScreen extends MainScreen
{
    private LocationProvider locationProvider;
    private static int interval = 1;
    double lat;
    double longt;

    public MyScreen()
    {
        setTitle("MyTitle");
        startLocationUpdate();
    }

    private boolean startLocationUpdate()
    {
        boolean retval = false;

        try
        {
            locationProvider = LocationProvider.getInstance(null);

            if ( locationProvider == null )
            { 
                Runnable showGpsUnsupportedDialog = new Runnable()
                {
                    public void run()
                    {
                        Dialog.alert("GPS is not supported on this platform, exiting...");
                        //System.exit( 1 );
                    }
                };

                UiApplication.getUiApplication().invokeAndWait( showGpsUnsupportedDialog ); // Ask event-dispatcher thread to display dialog ASAP.
            }
            else
            { 
                locationProvider.setLocationListener(new LocationListenerImpl(), interval, 1, 1);

                retval = true;
            }
        }
        catch (LocationException le)
        {
            System.err.println("Failed to instantiate the LocationProvider object, exiting...");
            System.err.println(le);
            System.exit(0);
        }
        return retval;
    }

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

                updateLocationScreen(latitude, longitude);              
            }
        }

        public void providerStateChanged(LocationProvider provider, int newState)
        {
        }
    }

    private void updateLocationScreen(final double latitude, final double longitude)
    {
        UiApplication.getUiApplication().invokeAndWait(new Runnable()
        {
            public void run()
            {
                lat = latitude;
                longt = longitude;

                RichTextField txt=new RichTextField();
                txt.setText("Long=="+longt);

                RichTextField txt1=new RichTextField();
                txt1.setText("lat=="+lat);

                add(txt);
                add(txt1);
                //persistentLatitude.setContents(Double.toString(latitude));
                //persistentLongitude.setContents(Double.toString(longitude));
            }
        });
    }
}

答案 2 :(得分:1)

如果您需要快速获取当前位置,请使用以下代码。它适用于OS 6及更高版本:

private void findGeolocation() {
    Thread geolocThread = new Thread() {
        public void run() {
            try {
                if (!LocationInfo.isLocationOn()) {
                    LocationInfo.setLocationOn();
                }

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

                        showResults(_latitude, _longitude);

                    } catch (InterruptedException e) {
                        showException(e);
                    } catch (LocationException e) {
                        showException(e);
                    }
                } catch (LocationException e) {
                    showException(e);
                } catch (Exception e) {
                    showException(e);
                }
            } catch (UnsupportedOperationException e) {
                showException(e);
            } catch (Exception e) {
                showException(e);
            }
        }
    };

    geolocThread.start();
}

private void showResults(double _latitude, double _longitude) {
    System.out.println("latitude = " + _latitude + "longitude" + _longitude );

    Application.getApplication().invokeLater(new Runnable() {
        public void run() { 
            Dialog.alert("latitude = " + _latitude + "longitude" + _longitude );
        }
    });
}

private void showException(final Exception e) {
    Application.getApplication().invokeLater(new Runnable() {
        public void run() { 
            Dialog.alert(e.getMessage());
        }
    });
}