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);
}
}
public void displayString(String string)
{
si.setText(string);
}
}
class Retriever extends Thread {
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();
} catch (Exception ex)
{
ex.printStackTrace();
midlet.displayString(ex.toString());
}
}
public void checkLocation() throws Exception
{
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 [email protected]
August 5th, 2010 at 7:55 pm
Plz help…
How to find out the exact location of mobile in case of gps enabled phone. And if phone is not gps enabled then what we have to do.
August 25th, 2010 at 3:26 pm
Thank you for this application..
when I build the application like you mentioned..
I am getting the error “Location Request Timed Out Error”.
like mr. VishnuPrasad.
Please help me what can I do….??
August 25th, 2010 at 4:02 pm
thx brothers…Your tutorial can Solve my problem..
November 1st, 2010 at 4:32 pm
Adhere to the customer-oriented, post-sale Chiayi Detached villa, apartment houses, commercial stores, caring service of a Yuan Cheng’s dream.
March 1st, 2011 at 12:49 am
Thank you for this application, and thank for share, I tested it on nokia 2710c and works well. :)
March 18th, 2011 at 9:09 pm
Thanks For this application.
April 20th, 2011 at 4:40 pm
I try compiling your example in netbeans, but it did not.
I did create a project with the configurations you specify. Any idea of what could be generating the error. I think is something with this line:
public class loctest extends MIDlet implements CommandListener
please help me if you can.
April 21st, 2011 at 8:55 pm
Hello, I fix that error, but would you give me any information regarding the state of the app. It keeps saying select 1 to launch and that is it. Thank you.
June 2nd, 2011 at 6:25 am
thanks for this tutorial ;)
June 3rd, 2011 at 4:11 pm
I am new in j2me application and i don’t know how to start my project as i am really confused i am developing an application in which i want to design application which will work same as gps but without net connection can u please guide me how i can start what will be better way to display image.