Java Location API

Here is Example Java Location API example. But work for this want to support Java Location API in UR mobile and inbuilt GPS receiver(I only test with mobile emulator).

what is Location API:

The Java Location API for J2ME is intended to run on small client devices such as mobile phones.The Location API object model consists of 11 classes and two listener interfaces (LocationListener and ProximityListener), all in the javax.microedition.location package. Their design approach uses several standard patterns—Facade, Factory Method, Singleton, and Value Object, and standard JavaBeans-style accessors. Of the 11 classes, two are Exception classes (LocationException and LandmarkException) and another four (AddressInfo, Criteria, Orientation, and QualifiedCoordinates) are primarily value objects. Many of the properties of these objects may in practice be unavailable, depending on the location-finding technology implementing the API. Still, these properties anticipate likely future developments in mobile networks and devices and the level of location and context detail that they provide.


Example Code:
package com.test.location;

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);
}
}

public class Retriever extends Thread {

private LocTest midlet;

public Retriever(LocTest midlet) {
this.midlet = midlet;
}

public void run() {
try {
checkLocation();
} catch (Exception ex) {
ex.printStackTrace();
midlet.displayString(ex.toString());
}
}

public void checkLocation() throws Exception {
String string;
Location l;
LocationProvider lp;
Coordinates c;

Criteria cr = new Criteria();
cr.setHorizontalAccuracy(500);

lp = LocationProvider.getInstance(cr);
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+ "\nSpeed(km/h): " + (l.getSpeed() *3.59999);
} else {
string = "Location API failed";
}
midlet.displayString(string);
}
}

Comments

Popular posts from this blog

XSLT - Modify Date and DateTime value

Integrate With Mutual Certificate Authentication based Service

Yield Price Sri Lanka - Android Application