LBS using J2ME

Getting Coordinates

Where am I? â€“ The basic Location Question

It is the most basic and the most important question in a Location Based Service application. The ultimate motive of the user might be to drive to a place, visit a monument, track his friends or relax in the nearest cafeteria. But the problem always starts with where a particular person (rather a device) is?

Coordinate Alert Program

This program pops an alert message showing your current location. You need to be familiar with basic LCD UI for this and the following programs. If you do not already know about it, first learn about basic LCD UI and come back.

Create a new project named â€˜CoordinateAlertâ€™ and the MIDlet class name same as that of the project. In the source folder place CoordinateAlert.java. Build and run the program. Note that in the device drop-down list, you need to select â€˜Nokia Prototype SDKâ€™ or any other SDK that supports the location API else you will get compilation error while building the project.

Now letâ€™s understand the code. We will concentrate on the givePositionAlert() function, the rest of it is simple j2me. The essence of the location extraction is in the following lines â€“

To get the co-ordinates of current location we basically follow three logical steps â€“

Set the criteria of the location provider

Get the location provider instance according to set criteria

Get the location object from the location provider

Get the co-ordinates from the returned location object

Criteria

Criteria class included in the location package gives the application to set constraints based on which a provider is selected. It lets you specify accuracy or refresh time limits as required by the application. An application providing precise route direction wonâ€™t work if the position accuracy is off by say 30m. With such inaccurate provider, the application might ask you to take a left turn while you are driving on a bridge over a river. The application must ascertain specific criteria before choosing a location provider.

Remember while choosing a provider among different options, there is no preference given to any particular criteria and in case none of the available providers meet all requirements, the closest match is returned where â€˜closestâ€™ is platform-dependent.

However there is an exception to the general case. If the application has set the cost field to indicate that the returned location provider is not allowed to incur financial cost to the end user, the implementation MUST guarantee that the returned location provider does not incur cost.

All fields in the Criteria class are initialized to default values, you need to specifically set them if you need the values to be different like here the horizontal accuracy is set to 500 meters.

LocationProvider

A location provider can be in three states AVAILABLE (ready to provide the location), TEMPORARILY_UNAVAILABLE (not ready due to temporary reason e.g. blocked signal while inside building), OUT_OF_SERVICE (not ready and not likely to recover in near future).

The static method of LocationProvider class is passed the criteria and it returns the instance of a suitable location provider, a module that returns the location when asked for. LocationProvider.getInstance(Criteria cr) throws a LocationException if all providers are out of service.

When asked for the current location i.e. getLocation() is called, the location provider returns a location object with data corresponding to the current location measurement. If the provider is temporarily unavailable, it waits. However if it doesnâ€™t get the location in the specified time limit or if the provider is out of service, it throws a LocationException. In this program the time-out is set to 60 seconds.

The provider adheres to the set criteria used for its choice. However note that the individual location returned might not fulfill exactly the criteria used for selecting this provider. The criteria are used to select a location provider that typically is able to meet the defined criteria, but not necessarily for every individual location measurement.

Location

It contains all information of a location e.g. the co-ordinates, time stamp at which it was measured, speed etc. Methods can be invoked on the object to get the data. Sometimes the location returned by the provider might be invalid. The isValid() method can be called to check for its validity.

Coordinates and QualifiedCoordinates

Latitudes vary from +90(North) to â€“90(South) and longitude from +180(East) to â€“180(West).

Latitudes and longitudes are represented either as a double or in degree-minute or in degree-minute-second format.
12.52 degree (double) = 12 degree 31.2 minute (DD_MM) = 12 degree 31 minute 12.0 second (DD_MM_SS)

Coordinates class as you have guessed contains the latitude, longitude and altitude information, which can be accessed using setter and getter methods.

In addition it gives the functions to calculate distance between two points and also convert latitudes or longitudes from DD_MM or DD_MM_SS format to double values of vice-versa.

QualifiedCoordinates extends Coordinates and includes the horizontal and vertical accuracies of the measurement.

The Location object returned by the location provider contains the qualified coordinates.

Other Notes of this Program

The sign of the latitude and longitude (+ve or -ve) is used to determine whether it is North or South and East or West.

An alert is generated in case an Exception occurs. Never, I repeat NEVER, leave catch an exception just to compile the code, always generate an error message. A simple System.out.println message might do while developing the application. But think about actually running it on a device. In that case you must generate an Alert to detect the error.

Note that null coordinates are generated if the location provider is available but it canâ€™t get the location e.g. if you are inside a building. You must provide a check for that else your application throws a NullPointerException.

It is bad practice to use string concatenation using the â€˜+â€™ operator on strings as it takes up more resources at run time. It is better to use a StringBuffer. However for such a small program you might indulge in such luxury.

Limitations of this Program

While getting the location you canâ€™t interact with the UI. Try clicking the exit button and it wonâ€™t work while the application is busy getting the location info.

Every time you want to get the coordinates, the device asks for permission. This is annoying if you want the location continuously e.g. to continuously track your position on a map.

Want to do it smarter? Letâ€™s go to the next part!

Use the menu at top-left corner for navigation.

Created on 01/21/2007 12:38 AM by admin
Updated on 02/16/2007 05:26 AM by admin