JavaME Location API Example application with Source Code.
Here's a sample application that I created to use J2ME/JavaME Location API. The example should work with most modern phone which implements JSR-179. The application will display your current coordinates in Latitude, Longitude using GPS-enabled phone.
With simple modification, you can use LocationListener to estimate the speed of vehicle you're travelling with this simple application.
-
import javax.microedition.midlet.*;
-
import javax.microedition.lcdui.*;
-
import javax.microedition.location.*;
-
-
public class loctest extends MIDlet implements CommandListener
-
{
-
private Display display;
-
private Form form;
-
private Command cmdExit,cmdOK;
-
private StringItem si;
-
-
public loctest()
-
{
-
-
display =Display.getDisplay(this);
-
form = new Form("Location Api test");
-
cmdExit = new Command("Exit",Command.EXIT,5);
-
cmdOK = new Command("OK",Command.OK,1);
-
si = new StringItem("Geo Location", "Click OK");
-
form.append(si);
-
form.addCommand(cmdOK);
-
form.addCommand(cmdExit);
-
form.setCommandListener(this);
-
-
}
-
-
public void startApp()
-
{
-
display.setCurrent(form);
-
}
-
-
-
public void pauseApp()
-
{
-
-
}
-
-
public void destroyApp(boolean flag) {
-
notifyDestroyed();
-
}
-
-
public void commandAction(Command c, Displayable d)
-
{
-
if (c == cmdOK){
-
Retriever ret = new Retriever(this);
-
ret.start();
-
-
} else if (c == cmdExit) {
-
destroyApp(false);
-
}
-
}
-
-
{
-
si.setText(string);
-
}
-
}
-
-
-
private loctest midlet;
-
-
public Retriever(loctest midlet)
-
{
-
/**
-
* Constructor
-
*
-
* EFFECTS: Initialise the server and store midlet information
-
*
-
* @param midlet The main application midlet
-
* @param server Forecast Server URL
-
*
-
*/
-
this.midlet = midlet;
-
-
}
-
-
public void run()
-
{
-
/**
-
* Entry point of the thread
-
*
-
* EFFECTS: call to connect() method
-
*/
-
try {
-
checkLocation();
-
{
-
ex.printStackTrace();
-
midlet.displayString(ex.toString());
-
}
-
}
-
-
{
-
String string;
-
Location l;
-
LocationProvider lp;
-
Coordinates c;
-
// Set criteria for selecting a location provider:
-
// accurate to 500 meters horizontally
-
Criteria cr= new Criteria();
-
cr.setHorizontalAccuracy(500);
-
-
// Get an instance of the provider
-
lp= LocationProvider.getInstance(cr);
-
-
// Request the location, setting a one-minute timeout
-
l = lp.getLocation(60);
-
c = l.getQualifiedCoordinates();
-
-
if(c != null ) {
-
// Use coordinate information
-
double lat = c.getLatitude();
-
double lon = c.getLongitude();
-
string = "\nLatitude : " + lat + "\nLongitude : " + lon;
-
-
} else {
-
string ="Location API failed";
-
}
-
midlet.displayString(string);
-
}
-
}
Please note that not all JSR-179 enabled phone supports this feature as some of those features depends on external factor such as the availability of GPS signal, type of your network and telco features.
Requirements
This application requires MIDP2.0/CLDC 1.1 with JSR 179 Location API enabled phone like Nokia N95
For more information, please refer to List of Mobile Phones that supports JSR-179 Location API
The sample application that I build here was compiled using Sun Java Wireless Toolkit 2.5.1
Download Sample Application
LocationTest.zip (8KB)
[tags]gps,java,j2me,javame,mobile,pda,jsr-179,midlet[/tags]
May 30th, 2007 at 3:00 pm
JavaME Location API Example application with Source Code. List of Pocket PC Freeware Websites Great Resource for Nokia E-series Owners
June 28th, 2007 at 4:50 am
[...] • Email to a friend • Search • View comments • Track comments • More Recent ArticlesJavaME Location API Example application with Source Code.List of Pocket PC Freeware WebsitesGreat Resource for Nokia E-series Owners [...]
July 6th, 2007 at 1:34 am
[...] Par kart?m Google maps neapšaub?mi iesp?rda, bet ar? Microsoft ir sava atbilde vi?iem Virtual Earth. Karšu detaliz?cija Latvij? gan nav tik laba pat R?g?. L?k uz Virtual Earth uzb?v?ts risin?jums, kur var sekot l?dzi Barney tripam – http://due.lv/trip/. Man jau pat?k :) Var?tu uzb?v?t t?du pas?kumu budžeta versij?, izmantojot JSR-179. L?k ar? sample Varb?t man ar? vajag pagarin?t?ju N95, uz kura šo izm??in?t? Tikai nesan?ks nek?da budžeta versija vairs ;) Interesentiem liste ar mobilajiem, kas šo standartu support? [...]
October 13th, 2009 at 5:42 pm
Thank you for this application..
I build the application like you mentioned..
But i am getting the error “Location Request Timed Out Error ” each time while running the problem on N95 8gb device.
Please help….??
January 9th, 2010 at 2:41 am
Can any1 plz suggest any gud projects on J2mE and Symbian on which i can work on. the project should be such tht i can complete it in 1 week. Plz help. Plz mail me on my email id panickerrahul@ymail.com