Android Series: Using GPS data

Android Series: Using GPS data

In this tutorial i will show you how to quickly get the gps data using your android device version 1.6+.

All you have to do is to implement one interface and acquire LocationMangager from the adnroid system service.

public class LocationExampleAct extends Activity implements LocationListener{

    private double latitude;
    private double longitude;
    private LocationManager locMgr = null;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        locationTxt = (TextView)findViewById(;
        locMgr = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    public void onLocationChanged(Location location) {
        this.latitude = location.getLatitude();
        this.longitude = location.getLongitude();
        this.locationTxt.setText("Location: "+this.latitude +" : " + this.longitude);
        Log.i("Location change",""+latitude+ " : "+longitude) ;
    public void onProviderDisabled(String provider) {
    public void onProviderEnabled(String provider) {
    public void onStatusChanged(String provider, int status, Bundle extras) {

4 responses on “Android Series: Using GPS data

  1. ravi August 1, 2011 at 6:10 pm


    I need some clarification regarding this.
    I am developing a web application using This will be used by some people on tablets. Can we use code from a web application to interact with the GPS on the tablet for geocodes without using the HTML5 browser code?

    We need accurate values so, can we connect to the GPS on tablet to get these geocode values?

    Note: Ours is not a android application.


  2. Jean-Baptiste DUPONT December 19, 2012 at 2:57 pm

    A few bits are missing to get this up and running :
    – import directives
    import android.os.Bundle;
    import android.view.Menu;
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.util.Log;
    import android.widget.TextView;
    – .xml TextView to print the result with @+id/the location_id
    – <permissions to be added in the Manifest files

    Hope this will help someone out there.

  3. Jean-Baptiste DUPONT December 19, 2012 at 2:59 pm


    Well the permissions xml did not show up in my previous message – so here they are.

Leave a Reply