topics
mobile
data mining
web
data visualization
distributed computing
blackberry, iphone, android
sentiment analysis, string matching
social networking, google app engine
processing
hadoop, aster data
code blackberry gps
This example shows how to build a simple BlackBerry class that tracks a user's GPS location.

LOCATION TRACKER
LocationTracker is a class that will track a user's location and send GPS updates to a website address every 10 seconds. This is useful if you wish to track a user's location over an extended period of time.

ALTERNATIVE TECHNIQUE
I am aware that there are many different ways to do this. Another common way is to send location updates only when we get them (rather than every 10 seconds). This is good, except when a user turns his phone off (or loses internet connectivity), in which case, the user will be indefinitely placed at his (or her) last known location. The solution to this is to send a keep alive signal every so often, which lets the web service know that the user just hasn't moved. I opted not to do this for simplicity's sake, and also because my 10 second long/lat update is used as a keep alive (even if the user hasn't moved, the update is still sent).

CONFIGURE BLACKBERRY GPS FOR ECPLIPSE
By default, if you are using Eclipse with a BlackBerry simulator, it will not have GPS enabled. This is because the default device for the simulator doesn't support GPS. To enable it, you must change the device in the project properties to a BlackBerry device that comes with GPS.
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

public class LocationTracker extends TimerTask {
	public final static String LOCATION_URL = "http://<url that you wish to post long/lat to>";

	private Timer timer;
	private double longitude;
	private double latitude;

	public LocationTracker() {
		timer = new Timer();

		try {
			// TODO: update criteria with desired location distances, etc
			LocationProvider.getInstance(new Criteria()).setLocationListener(new MyLocationListener(), 1, 1, 1);
		} catch(Exception e) { System.err.println(e.toString()); }
		
		timer.schedule(this, 0, 10000);
	}

	public void run() {
		HTTPClient.getPage(LOCATION_URL + "?longitude=" + longitude + "&latitude=" + latitude);
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	private class MyLocationListener implements LocationListener {
		public void locationUpdated(LocationProvider provider, Location location)
		{
			if(location != null && location.isValid())
			{
			    QualifiedCoordinates qc = location.getQualifiedCoordinates();

				try {
					// TODO: not thread safe (assignment should be done at one time)
					LocationTracker.this.longitude = qc.getLongitude();
					LocationTracker.this.latitude = qc.getLatitude();
				} catch(Exception e) { System.err.println("criccomini " + e.toString()); }
			}
			else
			{
			    System.err.println("criccomini location not valid");
			}
		}

		public void providerStateChanged(LocationProvider provider, int newState) {
			// TODO: if provider was disabled, then disable reporting
		}
	}
}
BLACKBERRY GPS FOR THE STORM (9530)
If you run the above code on your BlackBerry device (for instance a Storm), you will get a "GPS not allowed" LocationProvider exception. You need to get your code signed if you want to use the BlackBerry Storm with GPS in your app. To do this, you need to buy a $20 certificate from RIM.

LINKS
BlackBerry Storm GPS
Blackberry GPS/Location API
BlackBerry GPSInfo
blog comments powered by Disqus