Finding GPS using J2ME

The Global Positioning System (GPS) is a satellite based navigation system made up of a network of 24 satellites placed into orbit by the U.S. Department of Defense. GPS was originally intended for military applications, but in the 1980s, the government made the system available for civilian use. GPS works in any weather conditions, anywhere in the world, 24 hours a day. There are no subscription fees or setup charges to use GPS.

The GPS is made up of three parts: satellites orbiting the Earth; control and monitoring stations on Earth; and the GPS receivers owned by users. GPS satellites broadcast signals from space that are picked up and identified by GPS receivers. Each GPS receiver then provides three-dimensional location (latitude, longitude, and altitude) plus the time.

I have created a J2ME midlet which is used to get the Location,Country,Address and few other details about the location, where the user connected to the GPS provider. This code can only used to get the GPS information from a GPS enabled phone. And also the phone should support java CLDC 1.1 and MIDP 2.0. It will not work for CLDC 1.0. Because the Location API(JSR 179) contains the foating-point math. Therefore only the CLDC 1.1 is capable of running this application.

Here is the GPSConnection.java(Midlet)


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.AddressInfo;
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;

/**
* @author DulanDissanayake
*/
public class GPSConnection extends MIDlet implements CommandListener {

Display display;
Form mainForm;
Form informationForm;
private Command gpsConnectionCmd;
private ConnectionHandler connectionHandler = null;
private Thread connectionThread = null;

public void startApp() {

if(midp2Capable() & cldc11Capable()){//Location API is works only in CLDC 1.1
display = Display.getDisplay(this);
mainForm = new Form("GPS LOCATOR");
gpsConnectionCmd = new Command("Connect", Command.OK, 5);
mainForm.addCommand(gpsConnectionCmd);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
notifyDestroyed();
}

public void commandAction(Command cmd, Displayable arg1) {
if (cmd == gpsConnectionCmd) {

connectionHandler = new ConnectionHandler(this);
connectionHandler.start();

}
}

public static boolean midp2Capable() {
return System.getProperty("microedition.profiles").equals("MIDP-2.0");
}

public static boolean cldc11Capable() {
return System.getProperty("microedition.configuration").equals("CLDC-1.1");
}

}

class ConnectionHandler extends Thread {


private LocationProvider locationProvider = null;
private Criteria criteria;
private boolean isLocationCreated = false;
private Location location;
private AddressInfo addressInformation;
private Coordinates coordinates;
private double latitude;
private double longitude;
private float altitude;
private GPSConnection gpsConnection;

public ConnectionHandler(GPSConnection gpsConnection){

this.gpsConnection = gpsConnection;
}




public void createLocationProvider() {
if (locationProvider == null) {
criteria = new Criteria();

criteria.setAddressInfoRequired(true);
criteria.setAltitudeRequired(true);
try {
locationProvider = LocationProvider.getInstance(criteria);
isLocationCreated = true;

} catch (Exception le) {

Alert a = new Alert("Cannot create LocationProvider for this criteria");
gpsConnection.display.setCurrent(a);
isLocationCreated = false;
//le.printStackTrace();
}
}
}

public void run() {
try {
createLocationProvider();

if ((locationProvider == null)) {


Alert a = new Alert("Cannot run without location provider!");
gpsConnection.display.setCurrent(a);
return;
}
else {

location = locationProvider.getLocation(Location.MTE_SATELLITE);
addressInformation = location.getAddressInfo();//this is for getting the address of the location
coordinates = location.getQualifiedCoordinates();//this gives the locations Coordinates

if (gpsConnection.informationForm == null) {
gpsConnection.informationForm = new Form("Information");

String[] array = new String[5];
array[0] = addressInformation.getField(AddressInfo.EXTENSION);
array[1] = addressInformation.getField(AddressInfo.STREET);
array[2] = addressInformation.getField(AddressInfo.COUNTY);
array[3] = addressInformation.getField(AddressInfo.CITY);
array[4] = addressInformation.getField(AddressInfo.STATE);

//address of the location
StringItem address = new StringItem("Address :", new StringBuffer(array[0] + "\n" + array[1] + "\n" + array[2] + "\n" + array[3] + "\n" + array[4]).toString());

//postal code of the location
StringItem postalCode = new StringItem("Postal Code :", addressInformation.getField(AddressInfo.POSTAL_CODE));

//country of the location
StringItem country = new StringItem("Country :", addressInformation.getField(AddressInfo.COUNTRY));

gpsConnection.informationForm.append(address);
gpsConnection.informationForm.append(postalCode);
gpsConnection.informationForm.append(country);


if(coordinates != null){

latitude = coordinates.getLatitude();//this gives the latitude of the location
longitude = coordinates.getLongitude();//this gives the longitude of the location

StringItem latitudeString = new StringItem("Latitude :", " "+latitude);
StringItem longitudeString = new StringItem("Longitude :", " "+longitude);

gpsConnection.informationForm.append(latitudeString);
gpsConnection.informationForm.append(longitudeString);

}

}

}
//}
} catch (LocationException ex) {
Alert a = new Alert("Location Cannot Find");
gpsConnection.display.setCurrent(a);
//ex.printStackTrace();
} catch (InterruptedException ex) {
//System.out.println("InterruptedException " + ex);
//ex.printStackTrace();
} catch (Exception ex) {

//System.out.println("Whole Exception " + ex);
}

}
}

Comments

Popular posts from this blog

SSL Pinning in Android

Java Unit testing using JUnit @RunWith(Suite.class)

Android Event Bus Implementations using Otto