Android

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
{
	@Override
	public void onLocationChanged(Location loc)
	{
		String Text="Location Not Set";
		Text = "My current location is: n" +
		"Latitude = " + loc.getLatitude() +
		"nLongitude = " + loc.getLongitude();
		Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_LONG).show();
	}
}

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

@Override
public void onProviderDisabled(String provider)
{
	Toast.makeText( getApplicationContext(),"GPS Disabled",Toast.LENGTH_LONG ).show();
}
@Override
public void onProviderEnabled(String provider)
{
	Toast.makeText( getApplicationContext(),"GPS Enabled",Toast.LENGTH_LONG).show();
}
@Override
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="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
</LinearLayout>

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

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

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">
</uses-permission>

3. Test Application

a) DDMS

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">
</uses-permission>

In case you want to use only network Provider, use

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

Location manager also allows to get the last known location

LocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
Tagged , , , , , , , , , ,