How to determine location using GPS in Android Phones

In this tutorial we’ll create a small app which displays a Toast Notification whenever GPS Coordinates changes. Start a new Project.

Project Name: HelloGps
Android Level: Android 2.1
Application Name: HelloGps
Package Name: com.vineetdhanawat.hellogps
Create Activity: HelloGps
Min SDK Version: 7

1. Implementing Location Listener

Lets start with the code. Inside the onCreate() method include these lines

/* 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);

If asked, import all required packages like import android.location.LocationManager; etc The Location Manager Object is obtained by calling the method with Context.LOCATION_SERVICE parameter. 0, 0 in parameters to requestLocationUpdates is the min Time & min Distance respectively, after which onLocationChanged() is called. Lets implement a nested class for LocationListener now

public class MyLocationListener implements LocationListener
	public void onLocationChanged(Location loc)
		String Text="Location Not Set";
		Text = "My current location is: n" +
		"Latitude = " + loc.getLatitude() +
		"nLongitude = " + loc.getLongitude();

This will basically display a notification on screen (Toast), as soon as the GPS Coordinates changes Now just implement rest of the required methods as follows

public void onProviderDisabled(String provider)
	Toast.makeText( getApplicationContext(),"GPS Disabled",Toast.LENGTH_LONG ).show();
public void onProviderEnabled(String provider)
	Toast.makeText( getApplicationContext(),"GPS Enabled",Toast.LENGTH_LONG).show();
public void onStatusChanged(String provider, int status, Bundle extras)

2. Layout and Manifest File

Lets look at main.xml (UI) now. Just display a string on the main screen. GPS coordinates will splash anytime it changes

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

Define the app name and display text in string.xml file

<?xml version="1.0" encoding="utf-8"?>
    <string name="hello">GPS Coordinates will splash on screen</string>
    <string name="app_name">GPS</string>

One Major change in AndroidManifest.xml file this time is GPS Permission. <uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”></uses-permission>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">

3. Test Application


You can test application by using an Emulator Control. Window > Open Perspective > Other … > DDMS Now under Location Controls, pass any thing in decimal form in latitude and Longitude. The same will splash on screen.

b) Telnet

Emulator has an inbuilt Telnet Server. To connect, first make sure you have a telnet client.

>telnet localhost 5554
>geo fix 30 28

4. Location using Network Provider

You need to make a few changes.

/*mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);*/
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);

In AndroidManifest file, this works for both GPS and Network Provider

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">

In case you want to use only network Provider, use

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">

Location manager also allows to get the last known location

Tagged , , , , , , , , , ,