make sure to keep Gps1.png and Gps2.png (small images to show gps working or not)
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
/**
*
* @author Susith
*/
public class LocationListenerImpl implements LocationListener{
public static String lat_check = "", lon_check = "";
public void locationUpdated(LocationProvider provider, Location location) {
lat_check = "";
lon_check = "";
if (location.isValid()) {
double longitude = location.getQualifiedCoordinates().getLongitude();
double latitude = location.getQualifiedCoordinates().getLatitude();
MyMidlet.setLon(Double.toString(longitude));
MyMidlet.setLat(Double.toString(latitude));
}
}
public void providerStateChanged(LocationProvider arg0, int arg1) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.StringItem;
/*
* To change this template, choose Tools Templates
* and open the template in the editor.
*/
import javax.microedition.location.LocationProvider;
import javax.microedition.midlet.MIDlet;
/**
*
* @author Susith
*/
public class MyMidlet extends MIDlet implements Runnable{
private static LocationProvider locationProvider;
private static String lat = "";
private static String lon = "";
private int count;
Form frm = null;
Thread th = null;
public void startApp() {
try {
frm = new Form("GPS Co-ordinates");
startLocationUpdate();
th = new Thread(this);
th.start();
} catch (Exception ex) {
Alert alert = new Alert("Error Message");
alert.setString("Exception "+ex.getMessage());
alert.setTimeout(3000);
alert.setType(AlertType.ALARM);
Display.getDisplay(this).setCurrent(alert);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void run() {
while (true) {
count+=5;
frm = new Form("test");
StringItem strlat = new StringItem("LAT : ", lat);
StringItem strlon = new StringItem("LON : ", lon);
if (!LocationListenerImpl.lat_check.equals("") !LocationListenerImpl.lon_check.equals("")) {
try {
ImageItem img = new ImageItem("", Image.createImage("/gps1.png"), 0 0, "");
frm.append(img);
} catch (IOException ex) {
//todo handle error
}
} else {
try {
ImageItem img = new ImageItem("", Image.createImage("/gps2.png"), 0 0, "");
frm.append(img);
} catch (IOException ex) {
//todo handle error
}
}
frm.append(strlat);
frm.append(strlon);
frm.append(new StringItem("Count: ",String.valueOf(count) ));
Display.getDisplay(this).setCurrent(frm);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
private static boolean startLocationUpdate() {
boolean retval = false;
try {
locationProvider = javax.microedition.location.LocationProvider.getInstance(null);
if (locationProvider != null) {
// Only a single listener can be associated with a provider, and unsetting it
// involves the same call but with null, therefore, no need to cache the listener
// instance request an update every second.
locationProvider.setLocationListener(new LocationListenerImpl() , 1, 1, 1);
retval = true;
}
} catch (javax.microedition.location.LocationException le) {
System.err.println("Failed to instantiate the LocationProvider object, exiting...");
System.err.println(le);
System.exit(0);
}
return retval;
}
public static void setLat(String lat) {
MyMidlet.lat = removeTail(lat);
}
public static void setLon(String lon) {
MyMidlet.lon = removeTail(lon);
}
private static String removeTail(String string){
return string.length()>5?string.substring(0, 5):string;
}
}