Blog

Using GPS in Java - MIDlet

During my work on PRiMMA project I need to do some testing of GPS in mobile phones. Test number one is How get geographical coordinates using built in Nokia N95 GPS receiver using Java ME - Java Micro Edition ?.
There are numbers of phones on the market with built in GPS receiver. Of course my phone does it as well, therefore I developed small MIDlet that can track user's moves. It connects to the GPS satellite, gets geo coordinates: Logitude, Latitude and Altitude and posts it on the server.

Here is code of main function:

public void getPosition(){
       
    LocationProvider lp=null;
    javax.microedition.location.Location location=null;
       
        try{
            lp= LocationProvider.getInstance(null);
        }
        catch(LocationException e){
            err(e.getMessage());   
        }
        try{                       
            location = lp.getLocation(10);
        }
        catch(LocationException e){
            err(e.getMessage());       
        }
        catch(InterruptedException e){
            err(e.getMessage());       
        }

        String res="c:";
        try{
            Coordinates coordinates = location.getQualifiedCoordinates();
            res+=""+coordinates.getAltitude()+";";
            res+=""+coordinates.getLatitude()+";";
            res+=""+coordinates.getLongitude()+";";

           
        }catch(Exception e){
            res = e.getMessage();       
        }
       
}


don't forget about location library:

import javax.microedition.location.*;

Result of my tracking: picture below presents my walk around Jenie Lee Building at The Open University :)
Mobile GPS tracking example
27th August 2008