JavaME Location API Example application with Source Code.

May 30th, 2007 Posted in J2ME / JavaME

 

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]

 

Random Posts


15 Responses to “JavaME Location API Example application with Source Code.”

  1. Blog tak berdaftar Says:

    JavaME Location API Example application with Source Code. List of Pocket PC Freeware Websites Great Resource for Nokia E-series Owners



  2. Your FeedBlitz Email Newsletter Updates Says:

    [...] • 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   [...]



  3. Par kart?m (?riks Nu?is) Says:

    [...] 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? [...]



  4. VishnuPrasad Says:

    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….??



  5. Rahul Panicker Says:


  6. areef Says:

    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.



  7. Arifin Says:

    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….??



  8. Arifin Says:

    thx brothers…Your tutorial can Solve my problem..



  9. ???? Says:

    Adhere to the customer-oriented, post-sale Chiayi Detached villa, apartment houses, commercial stores, caring service of a Yuan Cheng’s dream.



  10. fanmaer Says:

    Thank you for this application, and thank for share, I tested it on nokia 2710c and works well. :)



  11. Rahul Says:

    Thanks For this application.



  12. Teffy Says:

    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.



  13. Teffy Says:

    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.



  14. merq.de Says:

    thanks for this tutorial ;)



  15. arpit Says:

    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.



Leave a Reply

This blog is protected by dr Dave\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'s Spam Karma 2: 174427 Spams eaten and counting...