Finding Mobile Location (using latitude and longitude) by Google Maps in Java ME

logoA

In a cloudy sunday I take a new decision and work on some mobile coding. The first small application I coded is as in the title ” Finding Mobile Location (using latitude and longitude) by Google Maps in Java ME “. The aim is finding my place on earth from latitude and longitude positions gathered from my mobile device.

Let me introduce you my mobile device: Sony Ericsson W810i  (not cool one! I know)

The application must be set to CLDC 2.0 (CLDC 1.0 not support floating-point number that API uses in coordinates).

Supported JSRs:

JSR
cbno.gif 30: CLDC1.0 cbyes.gif 139: CLDC1.1
cbyes.gif 37: MIDP1.0 cbyes.gif 118: MIDP2.0
cbno.gif 271: MIDP3.0 cbyes.gif 185: JTWI
cbyes.gif 172: WSAPI cbno.gif 211: CHAPI
cbyes.gif 120: WMA1.0 cbyes.gif 205: WMA2.0
cbyes.gif 135: MMAPI cbyes.gif 75: PDAP
cbyes.gif 82: BTAPI cbno.gif 177: SATSA
cbyes.gif 184: M3G cbno.gif 129: PB
cbno.gif 209: AGUI cbno.gif 179: LAPI
cbqm.gif 256: MSAPI cbno.gif 257: CCAPI
cbqm.gif 238: MIAPI cbno.gif 234: AMMSAPI
cbno.gif 226: SVGAPI cbno.gif 180: SIPAPI
cbqm.gif 248: MSA

What is Java ME:

Java Platform, Micro Edition (Java ME) provides a robust, flexible environment for applications running on mobile and other embedded devices—mobile phones, personal digital assistants (PDAs), TV set-top boxes, and printers. Java ME includes flexible user interfaces, robust security, built-in network protocols, and support for networked and offline applications that can be downloaded dynamically. Applications based on Java ME are portable across many devices, yet leverage each device’s native capabilities. [from java.sun.com]

Lets code: (this is my first application sorry for mistakes please inform about them to me if you can!)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;

/**
 * @author huseyin
 */
public class VisualMIDlet extends MIDlet {

 private boolean midletPaused = false;

 private Display display;
 private StringItem label;

 //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
 private Form form;
 private Command exitCommand;
 private Command exitCommand1;
 //</editor-fold>

 /**
 * The VisualMIDlet constructor.
 */
 public VisualMIDlet() throws ConnectionNotFoundException {

 try {
 LocationProvider lp = LocationProvider.getInstance(null);
 if (lp != null) {
 Location l = lp.getLocation(300);
 if (l.isValid()) {
 Coordinates c = l.getQualifiedCoordinates();
 if (c!=null) {
 //for izmir
 //double latitude = 38.418698;
 //double longitude = 27.129687;
 double latitude = c.getLatitude();
 double longitude = c.getLongitude();
//ek                // do something with the coordinates

platformRequest("http://maps.google.com/maps?q="+latitude+","+longitude+"");
 display = Display.getDisplay(this);
 form = new Form("myLocation:");
 label = new StringItem("", "Today is: " + new java.util.Date().toString()+"\n please wait...\n myLocation is founded you!\n"+"@ Latitude:"+latitude+"\nLongitude:"+longitude+"");
 form.append(label);

//ek
 }
 }
 else {
 //System.out.println("Location is not valid!");
 }
 }
 else {
 //System.out.println("LocationProvider = null!");
 }
} catch (LocationException le) { // not able to retrieve location information
 //System.out.println("LocationException: " + le.getMessage());
} catch (InterruptedException ie) {
 //System.out.println("InterruptedException: " + ie.getMessage());
}
 }

 //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
 //</editor-fold>

 //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
 /**
 * Initilizes the application.
 * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
 */
 private void initialize() {
 // write re-initialize user code here

 // write post-initialize user code here
 }
 //</editor-fold>

 //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
 /**
 * Performs an action assigned to the Mobile Device - MIDlet Started point.
 */
 public void startMIDlet() {
 display.setCurrent(form);

 switchDisplayable(null, getForm());
 // write post-action user code here
 }
 //</editor-fold>

 //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
 /**
 * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
 */
 public void resumeMIDlet() {
 // write pre-action user code here

 // write post-action user code here
 }
 //</editor-fold>

 //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
 /**
 * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
 * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
 * @param nextDisplayable the Displayable to be set
 */
 public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
 // write pre-switch user code here
 Display display = getDisplay();
 if (alert == null) {
 display.setCurrent(nextDisplayable);
 } else {
 display.setCurrent(alert, nextDisplayable);
 }
 // write post-switch user code here
 }
 //</editor-fold>

 //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">
 /**
 * Returns an initiliazed instance of exitCommand component.
 * @return the initialized component instance
 */
 public Command getExitCommand() {
 if (exitCommand == null) {
 // write pre-init user code here
 exitCommand = new Command("Exit", Command.EXIT, 0);
 // write post-init user code here
 }
 return exitCommand;
 }
 //</editor-fold>

 //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">
 /**
 * Returns an initiliazed instance of form component.
 * @return the initialized component instance
 */
 public Form getForm() {
 if (form == null) {
 // write pre-init user code here
 form = new Form("form");
 // write post-init user code here
 }
 return form;
 }
 //</editor-fold>

 //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">
 /**
 * Returns an initiliazed instance of exitCommand1 component.
 * @return the initialized component instance
 */
 public Command getExitCommand1() {
 if (exitCommand1 == null) {
 // write pre-init user code here
 exitCommand1 = new Command("Exit", Command.EXIT, 0);
 // write post-init user code here
 }
 return exitCommand1;
 }
 //</editor-fold>

 /**
 * Returns a display instance.
 * @return the display instance.
 */
 public Display getDisplay () {
 return Display.getDisplay(this);
 }

 /**
 * Exits MIDlet.
 */
 public void exitMIDlet() {
 switchDisplayable (null, null);
 destroyApp(true);
 notifyDestroyed();
 }

 /**
 * Called when MIDlet is started.
 * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
 */
 public void startApp() {
 if (midletPaused) {
 resumeMIDlet ();
 } else {
 initialize ();
 startMIDlet ();
 }
 midletPaused = false;
 }

 /**
 * Called when MIDlet is paused.
 */
 public void pauseApp() {
 midletPaused = true;
 }

 /**
 * Called to signal the MIDlet to terminate.
 * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
 */
 public void destroyApp(boolean unconditional) {
 }

}

<pre>

By the way:

I used NetBeans IDE 6.7.1 and Sun Java(TM) Wireless Toolkit 2.5 for CLDC in windows XP for developing this application.

Output is something like this: (I will work on it a little bit so sorry for user experience for now!)

A tip for googleMap latitude and longitude: To learn the lat./long. points for the place you found on googleMap just enter:  “javascript:void(prompt(”,gApplication.getMap().getCenter()));” to the adress bar of your browser.

Ex: for izmir it gives me [(38.418698, 27.129687)]

3

1

4

THAT’S ALL FOLKS!

thats-all-folks

Download (.jar file)

About these ads