All ingenious — is simple. Writing location-based services for J2ME

Good day!

Introduction


Today, location-based services such as GPS and Cell ID, are an integral part of our lives. With them, we can learn where we are, for example, if we lost, or simply to share your location on social networks, e.g., Foursquare.
Many mobile phones support J2ME Location API (JSR-179). Using it, we can easily write some useful and, most importantly, an interesting application for the platform.
In this article I propose to consider the Location API for J2ME and write a small but very interesting application. But about all under the order.


why use location-based services


Location-based services pososat fast to answer three questions, namely:
the

    Where am I?

    What's around me? the

  • And how the hell did I get here?


For example, you became very ill (FIE-FIE-FIE), next to you not a single passer-by and you can't say a word. To help you will receive GPS, which will give the ambulance the coordinates of your location. Unfortunately, this principle works only in the USA when you call 911.

down to business


In order to use our app, we need a phone that will meet the following requirements:
the
    the
  • platform J2ME
  • the
  • the presence of the phone JSR-179 (Location API)
  • the
  • availability CDC (Connected Device Configuration) or CLDC 1.1 (Connected Limited Device Configuration profile)

These phones are, for example, Nokia E71, E66, N95, N96, 6210 Navigator and many others...

Defining the device


To determine the location of the device, Location API uses the available positioning methods in real time. The accuracy depends on the methods used.
Usually, use the built-in GPS devices, which give us the necessary information, namely length and breadth, and height.

Width is expressed as 0-90 degrees (North or South).
Longitude is expressed as 0-180 degrees (West or East).
Height is expressed, respectively, in meters above sea level.

Applications can use several methods of positioning.
the

    Cell ID: if this method is used as the coordinates of the location is taken the value from the nearest cell towers, so-called BTS (Base Transceiver Station). The accuracy of this method depends on the radius of the cell tower. For GSM network, this distance varies from 2 to 20 kilometers. the

  • Using the satellite: GPS or the global positioning system that uses data taken from one of 24 satellites in orbit. GPS determines your location, equal to the difference in time passing the signal from one satellite to another. GPS is the most accurate determinant of location. The error will be about 4-40 meters (with clear sky).


From theory to practice


JSR-179 is nothing but a package of the javax.microedition.location, sewn up inside your mobile phone. It provides developers with the opportunity to develop location-based services for J2ME, providing all needed information (starting coordinates and ending storage for your notes).
The hardware platform determines which positioning methods, real-time you available at the moment. You do not need to worry about which method to use, as J2ME will solve it for you. Accordingly, it will be used the most precise method available (GPS, Cell ID).

To ensure that Location API available, we can do the following:
the
...
public boolean isLocationSupported() {
boolean isItTrue = true;
try {
Class.forName("javax.microedition.location.Location");
} catch (Exception e) {
isItTrue = false;
}
return isItTrue;
}
...

so, next up


To write a location based app, we need to work with the following classes:
the

    Criteria, which contains information on the accuracy, response time from the satellites or towers, and speed.

    LocationProvider that works with the class data Criteria to determine your location.

    Location. An object containing the coordinates, speed (if available), address text (if available), and the time in which was made the calculation. the

  • the coordinates are (Yes, I understand you) we can get from one of the two classes (Coordinates or QualifiedCoordinates, your choice). The only difference is that the QualifiedCoordinates provides additional information about the measurement accuracy, expressed in meters.


For the concept and consolidation theory, I propose to write a small MIDlet (so-called J2ME app):
the
// import the necessary packages
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;

// declare our application class inherited from MIDlet
public class FirstGeo extends MIDlet implements CommandListener {

// declare the necessary objects
Display dsp;
Form frm;
StringItem data;
Command cmdExit;

// constructor of our MIDleta
public FirstGeo() {
dsp = Display.getDisplay(this);
frm = new Form("First Geo-located application");
data = new StringItem("Location: ","Unavailable");
cmdExit = new Command("Exit",Command.EXIT,1);
}

// start the app
// this method is required to declare in our class
public void startApp() throws MIDletStateChangeException {
dsp.setCurrent(frm);
frm.append(cmdExit);
frm.setCommandListener(this);
frm.append(data);
// check whether we any means of positioning
if (isLocationSupported()) {
Criteria cr = new Criteria();
cr.setHorizontalAccuracy(500);
LocationProvider lp = LocationProvider.getInstance(cr);
Location l = lp.getLocation(10);
QualifiedCoordinates qc = l.getQualifiedCoordinates();
data.setText("Latitude:" + qc.getLatitude() + "\n" + "Longitude:" + qc.getLongitude() + "\n" + "Altitude:" + qc.getAltitude() + "\n");
} else {
destroyApp(true);
}
}

// this method is also needed for ads
public void pauseApp() throws MIDletStateChangeException {
// do nothing
}

// and this
public void destroyApp(boolean uncond) throws MIDletStateChangeException {
if (uncond == true) {
notifyDestroyed();
}
}

public void commandAction(Command c, Displayable d) {
if (c == cmdExit &&d == dsp) {
destroyApp(true);
}
}

// function that checks whether we tools positioning
public boolean isLocationSupported() {
boolean isItTrue = true;
try {
Class.forName("javax.microedition.location.Location");
} catch (Exception e) {
isItTrue = false;
}
return isItTrue;
}
}


So for a couple of minutes you wrote the application that defines the width, longitude and altitude, which at the moment is redundant, our location.

The purpose of this topic was to introduce the topic of building location-based services, which was done. If you want to get acquainted with the full Location API, pozhaluista.

PS unfortunately, I am away from the desktop PC to compile the source code and show you the screenshots that promise to do this later.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

The release of the new version of the module modLivestreet 0.3.0-rc

mSearch: search + filter for MODX Revolution

Emulator data from GNSS receiver NMEA