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.

JAVA:
  1. import javax.microedition.midlet.*;
  2. import javax.microedition.lcdui.*;
  3. import javax.microedition.location.*;
  4.  
  5. public class loctest extends MIDlet implements CommandListener
  6. {
  7.     private Display display;
  8.     private Form form;
  9.     private Command cmdExit,cmdOK;
  10.     private StringItem si;
  11.  
  12.     public loctest()
  13.     {
  14.  
  15.         display =Display.getDisplay(this);
  16.         form = new Form("Location Api test");
  17.         cmdExit = new Command("Exit",Command.EXIT,5);
  18.         cmdOK = new Command("OK",Command.OK,1);
  19.         si = new StringItem("Geo Location", "Click OK");
  20.         form.append(si);
  21.         form.addCommand(cmdOK);
  22.         form.addCommand(cmdExit);
  23.         form.setCommandListener(this);
  24.  
  25.     }
  26.  
  27.     public void startApp()
  28.     {
  29.         display.setCurrent(form);
  30.     }
  31.  
  32.  
  33.     public void pauseApp()
  34.     {
  35.  
  36.     }
  37.  
  38.     public void destroyApp(boolean flag) {
  39.         notifyDestroyed();
  40.     }
  41.  
  42.     public void commandAction(Command c, Displayable d)
  43.     {
  44.         if (c == cmdOK){
  45.             Retriever ret = new Retriever(this);
  46.             ret.start();
  47.  
  48.         } else if (c == cmdExit) {
  49.             destroyApp(false);
  50.         }
  51.     }
  52.  
  53.     public void displayString(String string)
  54.     {
  55.         si.setText(string);
  56.     }
  57. }
  58.  
  59. class Retriever extends Thread {
  60.  
  61.     private loctest midlet;
  62.  
  63.         public Retriever(loctest midlet)
  64.         {
  65.             /**
  66.               * Constructor
  67.               *
  68.               * EFFECTS: Initialise the server and store midlet information
  69.               *
  70.               * @param midlet The main application midlet
  71.               * @param server Forecast Server URL
  72.               *
  73.               */
  74.             this.midlet = midlet;
  75.  
  76.     }
  77.  
  78.     public void run()
  79.         {
  80.             /**
  81.             * Entry point of the thread
  82.             *
  83.             * EFFECTS: call to connect() method
  84.             */
  85.             try {
  86.                             checkLocation();
  87.                         } catch (Exception ex)
  88.                         {
  89.                             ex.printStackTrace();
  90.                             midlet.displayString(ex.toString());
  91.             }
  92.         }
  93.  
  94.     public void checkLocation() throws Exception
  95.     {
  96.         String string;
  97.         Location l;
  98.         LocationProvider lp;
  99.         Coordinates c;
  100.         // Set criteria for selecting a location provider:
  101.         // accurate to 500 meters horizontally
  102.         Criteria cr= new Criteria();
  103.         cr.setHorizontalAccuracy(500);
  104.  
  105.         // Get an instance of the provider
  106.         lp= LocationProvider.getInstance(cr);
  107.  
  108.         // Request the location, setting a one-minute timeout
  109.         l = lp.getLocation(60);
  110.         c = l.getQualifiedCoordinates();
  111.  
  112.         if(c != null ) {
  113.           // Use coordinate information
  114.           double lat = c.getLatitude();
  115.           double lon = c.getLongitude();
  116.           string = "\nLatitude : " + lat + "\nLongitude : " + lon;
  117.  
  118.         } else {
  119.             string ="Location API failed";
  120.         }
  121.         midlet.displayString(string);
  122.     }
  123. }

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: , , , , , , ,

Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • co.mments
  • del.icio.us
  • digg
  • YahooMyWeb
  • DZone
  • StumbleUpon
  • Technorati

 

Recommended Reading

3 smashing comments for this post.

  1. Blog tak berdaftar Said:

    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 Said:

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

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

Leave a Comment

Subscribe by Email


Powered by FeedBlitz

 


Your Ad Here