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. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   
        locationTxt = (TextView)findViewById(R.id.location_id);
       
        locMgr = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
       
    }
   
    @Override
    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) ;
    }
    @Override
    public void onProviderDisabled(String provider) {
       
    }
    @Override
    public void onProviderEnabled(String provider) {
       
    }
    @Override
    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

    Hi,

    I need some clarification regarding this.
    I am developing a web application using vb.net. This will be used by some people on tablets. Can we use vb.net 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.

    Thanks

  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.app.Activity;
    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.
    Cheers,
    jb

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

    “android.permission.ACCESS_COARSE_LOCATION”
    “android.permission.ACCESS_FINE_LOCATION”
    “android.permission.ACCESS_LOCATION_EXTRA_COMMANDS”
    “android.permission.ACCESS_MOCK_LOCATION”
    “android.permission.INTERNET”

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

Leave a Reply