Guest Book
What people have said about the book:
New BlackBerry developers are going to love this book! It has everything they need to get started with BlackBerry development. If you’re beginning your first BlackBerry development project – you need to read this book first. If you’ve been doing BlackBerry development for a while, there’s still things you’ll find here that you didn’t know.
Brent Thornton |
|
|
|
|
Chapter 11 contains sample BlackBerry Java code snippets that illustrate how to obtain a BlackBerry device's location (using the GPS capabilities of a device) and use it within an application. When I first learned how to do this, I created the simple BlackBerry Java application you see below. It's a Local Weather application that uses the GPS capabilities of a device (through JSR 179) to determine it's location and pass the information to the browser to open the US National Weather Service web site to display the weather conditions for the current location.
The US National Weather Service has a bunch of web services available to developers, and I'd originally wanted to use them to build a BlackBerry Java appliction that consumed the service. As I dug into the options available to me, I quickly discovered that there was a better way, and the application you see below is the results of that work.
The application gets the Longitude and Latitude from the device using the location object described in chapter 11 and passes them into the URL used to open the local weather.
/*********************************************************
* Local Weather Application
* By John M. Wargo
*
* This application uses the GPS capabilities of a
* BlackBerry handheld as input to the Weather services
* provided by the US National Weather Service (NOAA)
*
* My first thought was to just try to invoke the
* service using the capabilities of the 4.3 hh code,
* but during researched discovered that I can just pass
* long and lat values into this URL:
* http://forecast.weather.gov/MapClick.php?textField1=41.06&textField2=-81.50
* to get the same result.
*********************************************************/
package com.bbdevfundamentals.LocalWeather;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.container.MainScreen;
public class LocalWeather extends UiApplication {
public static void main(String[] args) {
LocalWeather theApp = new LocalWeather();
theApp.enterEventDispatcher();
}
public LocalWeather() {
pushScreen(new localWeatherScreen());
}
}
final class localWeatherScreen extends MainScreen {
private static String appTitle = "Local Weather";
private RichTextField rtField;
public LocationProvider lp;
final String gpsError = "Unable to access GPS information.";
public localWeatherScreen() {
// Make sure the default menu is displayed on the scroll wheel
super(DEFAULT_MENU | DEFAULT_CLOSE);
// Set the title for the application
setTitle(new LabelField(appTitle, LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));
// Create the rich text field, we'll populate it later
rtField = new RichTextField("Retrieving GPS information.");
// Add the rich text field to the screen
add(rtField);
// Now lets see if we have GPS capabilities
Criteria cr = new Criteria();
cr.setAddressInfoRequired(false);
cr.setAltitudeRequired(false);
cr.setPreferredResponseTime(Criteria.NO_REQUIREMENT);
cr.setSpeedAndCourseRequired(false);
cr.setCostAllowed(true);
cr.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
cr.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
cr.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
try {
// set up a location provider instance
lp = LocationProvider.getInstance(cr);
if (lp == null) {
// No location provider, so we can't do much.
rtField.setText(gpsError);
} else {
rtField.setText("Please wait while the local weather is launched.");
// fire off a thread to get the GPS information and launch
// the weather URL
Runnable initialGPSLoad = new Runnable() {
public void run() {
updateGPSInfo();
}
};
//UiApplication.getUiApplication().invokeLater(initialGPSLoad);
UiApplication.getUiApplication().invokeLater(initialGPSLoad);
}
} catch (LocationException le) {
rtField.setText(gpsError);
}
}
public void makeMenu(Menu menu, int instance) {
// Add a simple About item to the menu
menu.add(mnuAbout);
};
public MenuItem mnuAbout = new MenuItem("About", 150, 10) {
// toss up a little screen with my name and the copyright
public void run() {
Status.show("By John M. Wargo\nwww.bbdevfundamentals.com.");
}
};
public void updateGPSInfo() {
Runnable gpsJob = new GPSInfo();
Thread gpsThread = new Thread(gpsJob);
gpsThread.start();
}
public class GPSInfo implements Runnable {
public void run() {
try {
// Get location, one minute timeout
// leave up to 180 seconds for autonomous mode
Location loc = lp.getLocation(60);
if (loc.isValid()) {
Coordinates c = loc.getQualifiedCoordinates();
if (c != null) {
// use coordinate information to update the screen
double latitude = c.getLatitude();
double longitude = c.getLongitude();
launchWeatherURL(latitude, longitude);
}
} else {
locationError("The program returned an Invalid Location");
}
} catch (InterruptedException ie) {
System.err.println(ie);
locationError("The program raised an InterruptedException.\nException: " + ie);
} catch (LocationException le) {
System.err.println(le);
locationError("The program raised a LocationException.\nException: " + le);
} catch (Exception e) {
System.err.println(e);
locationError(e.getMessage());
}
}
private void launchWeatherURL(final double latVal, final double longVal) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// http://forecast.weather.gov/MapClick.php?textField1=41.06&textField2=-81.50
String appURL = ("http://forecast.weather.gov/MapClick.php?textField1="
+ Double.toString(latVal) + "&textField2=" + Double.toString(longVal));
// Get the default browser session
BrowserSession browserSession = Browser.getDefaultSession();
// Then display the page using the browser session
browserSession.displayPage(appURL);
// The following line is a work around to the issue found in
// version 4.2.0
browserSession.showBrowser();
// Once the URL is launched, close this application
System.exit(0);
}
});
}
private void locationError(final String errMsg) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
rtField.setText(gpsError + "\n\n" + errMsg);
}
});
}
}
}
|
|
|