Skip to content

Instantly share code, notes, and snippets.

@criccomini
Created September 23, 2012 22:49
Show Gist options
  • Save criccomini/3773331 to your computer and use it in GitHub Desktop.
Save criccomini/3773331 to your computer and use it in GitHub Desktop.
BlackBerry 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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment