“I like to drink to suit my location.” – Tom Jones 😀

Due to the widespread of smart-phones especially Blackberry all over Nigeria, location-based services and applications are generally becoming more  demanded by the large pool of users.

To be able to develop these applications, developers need to be armed with the basic skill of determining the GPS coordinates of the mobile device.

I am currently working on one of these kinds of applications. This is the block of code I used to scale through the hurdle. 🙂

NOTE: Determining GPS coordinates should be ran in a thread other than the app event thread to aviod the UI from being locked during the execution of the request.

import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

public class LocationHandler extends Thread {
QualifiedCoordinates qualifiedCoordinates;
public void run() {
//set parameters .. it varies depending the type of GPS technology you want to use
Criteria criteria = new Criteria();
criteria.setVerticalAccuracy(50);
criteria.setHorizontalAccuracy(50);
criteria.setCostAllowed(true);
criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
try {
//request location
LocationProvider provider =
LocationProvider.getInstance(criteria);
Location location = provider.getLocation(-1);
qualifiedCoordinates = location.getQualifiedCoordinates();
} catch (LocationException e) {

} catch (InterruptedException e) {

}
String latitude = qualifiedCoordinates.getLatitude();
String  longitude = qualifiedCoordinates.getLongitude();
}
}
//it doesn't require BIS or internet connection

 

QED – Quite Easily Done!

Hopefully developers will put this to use to develop really useful location-based applications.