📜 ⬆️ ⬇️

All ingenious is simple. Writing geolocation services for J2ME

Good day!

Introduction


Today, geolocation services such as GPS and Cell ID have become an integral part of our life. Through them, we can find out where we are, for example, if we are lost, or just share our location on social networks, for example, on Foursquare.
Many J2ME mobile phones support the Location API ( JSR-179 ). Using it, we can easily write some useful and, most importantly, interesting application for this platform.
In this article, I propose to consider the possibilities of the Location API for J2ME and write a small but very interesting application. But first things first.


What are geolocation services used for?


Geolocation services allow you to quickly answer three questions, namely:

')
For example, you feel very bad (pah-pah-pah), not a single passerby is by your side and you cannot say a word. You will be assisted by GPS , which will provide the ambulance with the coordinates of your location. Unfortunately, this principle only works in the United States when calling 911.

Closer to the point


In order to use our application, we need a phone that will meet the following requirements:

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

Device location


To determine the location of the device, the Location API uses real-time positioning methods available to it. Accuracy depends on the methods used.
Usually, built-in GPS devices are used that give us the necessary information, namely width, longitude and height.

The width is expressed as 0-90 degrees (north or south).
Longitude is expressed as 0-180 degrees (west or east).
The height is expressed, respectively, in meters above sea level.

Applications can use multiple positioning methods.


From theory to practice


JSR-179 is nothing more than a javax.microedition.location package that is wired inside your mobile phone. It provides developers with the opportunity to develop geolocation services for J2ME , providing all the necessary information (starting from the coordinates and ending with the storage for your marks).
The hardware platform determines which methods of real-time positioning are available to you at the moment. You absolutely do not need to worry about which method to use, because J2ME will solve it for you. Accordingly, the most accurate method available ( GPS , Cell ID ) will be used.

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


So, go further


To write a geolocation application, we need to work with the following classes:


For the concept and consolidation of the theory, I propose to write us a small MIDlet (this is the name of the J2ME application):
 //     import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.location.*; //    ,   MIDlet public class FirstGeo extends MIDlet implements CommandListener { //     Display dsp; Form frm; StringItem data; Command cmdExit; //   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); } //     //          public void startApp() throws MIDletStateChangeException { dsp.setCurrent(frm); frm.append(cmdExit); frm.setCommandListener(this); frm.append(data); // ,    -   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); } } //        public void pauseApp() throws MIDletStateChangeException { //    } //   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); } } // ,       public boolean isLocationSupported() { boolean isItTrue = true; try { Class.forName("javax.microedition.location.Location"); } catch (Exception e) { isItTrue = false; } return isItTrue; } } 


So in a couple of minutes you wrote an application that determines the width, longitude and height, which is currently redundant, of our location.

The purpose of this topic was to introduce you to the topic of building geolocation services, which was done. If you want to read the full Location API , please .

PS Unfortunately, I am far away from the desktop PC to compile the source code and show you screenshots that I promise to do later .

Source: https://habr.com/ru/post/133427/


All Articles