Get GPS Location Coordinates in BlackBerry

| |

Get GPS Location Coordinates in BlackBerry

BlackBerry api provides classes that allows us to retrieve location information using GPS. In this article, I have written a very basic LocationHandler class that initializes a LocationProvider with a timeout. The LocationHandler class provides public methods that return the latitude and longitude of the current location. It can be easily modified to retrieve the altitude as well. But I will keep it simple here,


First, let me introduce the LocationHandler class.

01    import javax.microedition.location.Location;
02    import javax.microedition.location.LocationException;
03    import javax.microedition.location.LocationProvider;
04    import net.rim.device.api.ui.component.Status;
05   
06    public class LocationHandler {
07      private LocationProvider locationProvider;
08      private Location currentLocation;
09      public LocationHandler()  {
10        try {
11          locationProvider = LocationProvider.getInstance(null);
12          currentLocation = locationProvider.getLocation(60);
13        }
14        catch (final Exception e) {
15          Application.getUiApplication().invokeLater(new Runnable() {
16            public void run() {
17              Status.show(e.getMessage());
18            }
19          });
20        }
21      }
22      public double getCurrentLongitude() {
23        if(currentLocation != null)
24          return currentLocation.getQualifiedCoordinates().getLongitude();
25        return -1;
26      }
27      public double getCurrentLatitude() {
28        if(currentLocation != null)
29          return currentLocation.getQualifiedCoordinates().getLatitude();
30        return -1;
31      }
32    }

That is almost enough for fetching the location parameters. But don’t just make an object of the class and use it right away. Keep in your careful mind that you can not initiate a LocationProvider service in the event thread. Event thread, as you probably know, is responsible for handling all drawing operations for the user interface. The getLocation function in the constructor of LocationHandler is a blocking operation. Using it in the event thread may stop the UI from responding for a while, and the user may end up believing it hanged for good. In fact, if you call this function from the event thread, you’ll helplessly receive a runtime error : getLocation() method cannot be called from event thread.To work-around this issue, create a new thread for updating the location information in the UI. The code belows demonstrates the usage of LocationHandler to update a LabelField with the current GPS location information.

01    public class LocationScreen extends MainScreen {
02      public LocationScreen() {
03        super("Location Information Demo");
04        final LabelField lfLocation = new LabelField();
05        new Thread() {
06          public void run() {
07            LocationHandler lh = new LocationHandler();
08            final String msg = "Long: " + lh.getCurrentLongitude() + ", Lat: " + lh.getCurrentLatitude();
09            Application.getUiApplication().invokeLater(new Runnable() {
10              public void run() {
11                lfLocation.setText(msg);
12              }
13            });
14          }
15        }.start();
16        this.add(lfLocation);
17      }
18    }

 
© Copyright 2010. yourblogname.com . All rights reserved | yourblogname.com is proudly powered by Blogger.com | Template by o-om.com - zoomtemplate.com