How can I get latitude and longitude of the current location in Blackberry?

こ雲淡風輕ζ 提交于 2019-12-19 11:20:14

问题


I am developing an app which has GPS functionality. How can I get latitude and longitude of the current location.


回答1:


I found a solution by myself. The following code works fine for me:

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:


Please check these links:

  • BlackBerry GPS.
  • Location APIs – Start to finish.
  • BlackBerry Java Development Environment - GPS and Map.

Also have a look at the Sample Application provided with the Eclipse plug-in named GPSDemo.




回答3:


If you need to get the current location fast, then use the following code. It works for OS 6 and higher:

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


来源:https://stackoverflow.com/questions/8711230/how-can-i-get-latitude-and-longitude-of-the-current-location-in-blackberry

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!