This is a simple application that shows the device's current location coordinates on the screen. It can be built in four easy steps -
1)Creating a canvas for displaying results.
2)Starting a new thread so that it doesn't slow down our application.
3)Using Location Provider object to retrieve the coordinates.
4)Displaying the coordinates on the canvas.
MyCanvas a canvas inherited class, which has a paintMe() function that takes 3 parameters, can be called whenever we need to refresh the screen with new data. In this case we are not using google maps so image passed can be anything else or null.
LocationProvider is a class that is used to obtain gps data from the device. Object of this class is initialized by setting it to LocationProvider.getInstance(Criteria) , where Criteria is a class that holds the options for the LocationProvider object. Examples of the options would be horizontal accuracy, vertical accuracy etc. See how these objects are initialized below in the GpsTracker() constructor.
startApp() is the entry point for the MIDlet where we create and start a new thread. run() contains the code for the thread, where we call getLocation() of Locationprovider object which returns Location object. We then retrieve coordinates from the Location object by calling its getQualifiedCoordinates() method.
Once we have the coordinates we can call paintMe() method of MyCanvas to refresh the page.
GpsTracker.java code :
Adding Google Maps to the Application
With one more simple step google map can be displayed on the screen showing your current location. For that you need this GoogleMaps.java file, download it and include it in your project.
Next you need to create an object of type GoogleMaps in you MIDlet, and you can call its retrieveStaticImage(height,width,latitude,longitude,zoom_level,image_format) method which will return the map image.
You can pass this image to paintMe() to draw it on the screen.
If you want to display additional details like marker, path etc on the map, modify GoogleMaps.java and modify the return url in getMapUrl() method.