Wednesday, April 21, 2010

Update GPS positions in blackberry at regular interval of time

first made a class GPSScreen .Java in which write this code .This will update latitude and longitude after 5 seconds,and shows longitude and latitude update and as well as mapfield changes as lat and long changes.

package com.rim.gpstryupadate;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.location.Coordinates;

import net.rim.device.api.lbs.MapField;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

public class GPSScreen extends UiApplication {
public static void main(String[]args)
{
GPSScreen theapp = new GPSScreen();
theapp.enterEventDispatcher();
}
public GPSScreen()
{
pushScreen(new first());
}

class first extends MainScreen
{GPS gps;
Timer timer;
RichTextField txtGPS;
MapField mpfld;
Coordinates cor;
public first(){
gps = new GPS();
timer = new Timer();
timer.schedule(new CheckGPS(), 100, 5000); //check for GPS every 5 second;

String textGPS = "";
txtGPS = new RichTextField(textGPS, RichTextField.NON_FOCUSABLE);
mpfld = new MapField();

add(txtGPS);
add(mpfld);

}
public boolean onClose()
{
timer.cancel(); //cleanup
this.close();
return true;
}

public class CheckGPS extends TimerTask{
public CheckGPS() {
}

public void run() {
double lat;
double lng;
lat = 0;
lng = 0;

lat = gps.getLatitude();
lng = gps.getLongitude();

if (lat != 0.0 & lng != 0.0) {
//synchronized (MyApplicationName.getEventLock()) {
double acc = gps.getAccuracy();
txtGPS.setText(lat + ", " + lng + " (" + gps.getSatCount() + ") (" + (int) acc + ")");
cor = new Coordinates(lat,lng,0);
mpfld.moveTo(cor);
//}
}
else
{
String thetxt = txtGPS.getText();
//synchronized (MyApplicationName.getEventLock()) {
if(thetxt.length() > 10)
if(thetxt.length() > 25)
txtGPS.setText("Waiting for GPS.");
else
txtGPS.setText(thetxt + ".");
else
txtGPS.setText("Waiting for GPS.");
//}
}
}
}
}
}


Make a class GPS.Java this will provide you longitude and latitude values



package com.rim.gpstryupadate;

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

public class GPS extends Thread{
private double latitude;
private double longitude;
private String satCountStr;
private float accuracy;
/* private double heading;
private double altitude;
private double speed;*/

private int interval = 1; // time in seconds to get new gps data

/**
* This will start the GPS
*/
public GPS() {
// Start getting GPS data
if (currentLocation()) {
// This is going to start to try and get me some data!
}
}

private boolean currentLocation() {
boolean retval = true;
try {
LocationProvider lp = LocationProvider.getInstance(null);
if (lp != null) {
lp.setLocationListener(new LocationListenerImpl(), interval, 1, 1);
} else {
// GPS is not supported, that sucks!
// Here you may want to use UiApplication.getUiApplication() and post a Dialog box saying that it does not work
retval = false;
}
} catch (LocationException e) {
System.out.println("Error: " + e.toString());
}

return retval;
}

private class LocationListenerImpl implements LocationListener {
public void locationUpdated(LocationProvider provider, Location location) {
if (location.isValid()) {
//heading = location.getCourse();
longitude = location.getQualifiedCoordinates().getLongitude();
latitude = location.getQualifiedCoordinates().getLatitude();
//altitude = location.getQualifiedCoordinates().getAltitude();
//speed = location.getSpeed();

// This is to get the Number of Satellites
String NMEA_MIME = "application/X-jsr179-location-nmea";
satCountStr = location.getExtraInfo("satellites");
if (satCountStr == null) {
satCountStr = location.getExtraInfo(NMEA_MIME);
}

// this is to get the accuracy of the GPS Cords
QualifiedCoordinates qc = location.getQualifiedCoordinates();
accuracy = qc.getHorizontalAccuracy();
}
}

public void providerStateChanged(LocationProvider provider, int newState) {
// no-op
}
}

/**
* Returns the terminal's course made good in degrees relative to true north.
* The value is always in the range (0.0,360.0) degrees.
*
* @return double
*/
//public double getHeading() {
//return heading;
// }

/**
* Returns the altitude component of this coordinate.
* Altitude is defined to mean height above the WGS84 reference ellipsoid.
* 0.0 means a location at the ellipsoid surface, negative values mean the
* location is below the ellipsoid surface, Float.NaN that no altitude is
* available.
*
* @return double
*/
//public double getAltitude() {
//return altitude;
//}

/**
* Get the number of satellites that you are currently connected to
*
* @return String
*/
public String getSatCount() {
return satCountStr;
}

/**
* Get the Accuracy of your current GPS location
*
* @return float
*/
public float getAccuracy() {
return accuracy;
}

/**
* Returns the latitude component of this coordinate.
*
* Positive values indicate northern latitude and negative values southern latitude.
*
* @return double
*/
public double getLatitude() {
return latitude;
}

/**
* Returns the longitude component of this coordinate.
*
* Positive values indicate eastern longitude and negative values western longitude.
*
* @return double
*/
public double getLongitude() {
return longitude;
}

/**
* Get your current ground speed in meters per second (m/s) at the time of measurement
*
* @return double
*/
//public double getSpeed() {
// return speed;
//}

}

No comments:

Post a Comment