Using GPS to get current location – Android tutorial

Today we are going to see how to get our current location from the GPS.

1. Create a new Android project called UseGps, under the folder “Firstdroid.Tutorial.Gps”

2. In order to be able to access the GPS, we will have to add the permission in

the AndroidManifest.xml file:

This is how the complete file looks like

<?xml version=“1.0″ encoding=“utf-8″?>

<manifest xmlns:android=“http://schemas.android.com/apk/res/android”

package=“Firstdroid.Tutorial.Gps”

android:versionCode=“1″

android:versionName=“1.0″>

<application android:icon=“@drawable/icon” android:label=“@string/app_name”>

<activity android:name=“.UseGps”

android:label=“@string/app_name”>

<intent-filter>

<action android:name=“android.intent.action.MAIN” />

<category android:name=“android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

</application>

<uses-permission android:name=“android.permission.ACCESS_FINE_LOCATION”></uses-permission>

</manifest>

Firstdroid Forum for: ” GPS Tutorial”

Firstdroid Forum Link

to   Now we need add a location listener, so each time the GPS senses a new location, this callback function will be called from the android system.

To do this, we need to add the fallowing code in the OnCreate method:

/* Use the LocationManager class to obtain GPS locations */

LocationManager mlocManager =

(LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

We will add our own personal Location listener, so we can override some of the functions to do what we want. In our case we will just print a message to the screen in the fallowing cases:

· onLocationChanged ( Location Update )

· onProviderDisabled ( GPS Off )

· onProviderEnabled (GPS On )

And add another mandatory method, which will do nothing.

· onStatusChanged (We are not adding nothing here).

The UseGps.java file should look as fallows:

package Firstdroid.Tutorial.Gps;

import android.app.Activity;

import android.content.Context;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.widget.Toast;

public class UseGps extends Activity

{

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

/* Use the LocationManager class to obtain GPS locations */

LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

}

/* Class My Location Listener */

public class MyLocationListener implements LocationListener

{

@Override

public void onLocationChanged(Location loc)

{

loc.getLatitude();

loc.getLongitude();

String Text = “My current location is: “ +

“Latitud = “ + loc.getLatitude() +

“Longitud = “ + loc.getLongitude();

Toast.makeText( getApplicationContext(),

Text,

Toast.LENGTH_SHORT).show();

}

@Override

public void onProviderDisabled(String provider)

{

Toast.makeText( getApplicationContext(),

“Gps Disabled”,

Toast.LENGTH_SHORT ).show();

}

@Override

public void onProviderEnabled(String provider)

{

Toast.makeText( getApplicationContext(),

“Gps Enabled”,

Toast.LENGTH_SHORT).show();

}

@Override

public void onStatusChanged(String provider, int status, Bundle extras)

{

}

}/* End of Class MyLocationListener */

}/* End of UseGps Activity */

Now we want to test our application, for this we have to options:

1 – We can run our application in the Phone.

2 – Run the application with the simulator.

For the second case, to simulate a location update we need to connect to the simulator by telnet, and do the fallowing:

telnet localhost 5554

geo fix 30 28

You should het something like this in the simulator:

I hope this tutorial was helpful for you.

Any question, problem or remark with this tutorial, please go to our forum:

Firstdroid Forum for: ” GPS Tutorial”

Firstdroid Forum Link

Enjoy,

Firstdroid.