You are on page 1of 8

MOBILE APPLICATION DEVELOPMENT LAB

CS-6611 / IT-6611

Exercise - 8

Develop a native application that uses GPS Location Information

New No.1, Vembuliamman Koil Street


Pazhavanthangal, Chennai – 600 114
Phone: 98841-65649, 98847-36775
E-mail: baskar@linuxpert.in
Android GPS Location  1 
 

Android GPS location


 
In this chapter we will learn to create an app to get our location using GPS. 
For this we will use: 
 System location service 
 LocationManager and LocationListener 
 Permission to access FINE_LOCATION 
 
Start a new project “GeoLocation” with an empty activity. 
Edit the activity_main.xml to change the parent layout to LinearLayout (Verical) 
and to Add the TextViews to display the location data such as longitude, latitude 
and altitude. 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.blogspot.shuttereditz.geolocation.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Latitude"
android:id="@+id/textView" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtLat" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Longitude"
android:id="@+id/textView2" />

<TextView
android:layout_width="wrap_content"

Copyright © Arjun P (Arjun Atlast)    fb.com/arjunatlast 
 
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location  2 
 

android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtLong" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Altitude"
android:id="@+id/textView3" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtAlt" />

</LinearLayout>
 
We have added 6 textViews three textViews as labels and other three to display 
values. 

 
   

Copyright © Arjun P (Arjun Atlast)    fb.com/arjunatlast 
 
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location  3 
 

Adding permission to Manifest


 
Open AndroidManifest.xml file and add permission to access fine location 
­ Fine Location locates smaller changes in position. 
­ <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blogspot.shuttereditz.geolocation">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>
 

Creating location listener


 
Now open the MainActivity.java 
Implement the LocationListener to the MainActivity class. 
public class MainActivity extends AppCompatActivity implements LocationListener
 
An error would be displayed now. Open the error popup and select implement 
methods and Select all Methods Click OK from the dialog box.  
Four Methods onLocationChanged(), onStatusChanged(), onProviderEnabled() 
and onProviderDisabled() are generated. We will use the onLocationChanged 
Method later to detect movement.   

Copyright © Arjun P (Arjun Atlast)    fb.com/arjunatlast 
 
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location  4 
 

@Override
public void onLocationChanged(Location location) {

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

@Override
public void onProviderEnabled(String provider) {

@Override
public void onProviderDisabled(String provider) {

}
 
Add the following data members to MainActivity class. 
TextView txtLat, txtLong, txtAlt;
//textviews that display latitude, longitude, altitude values

LocationManager manager;
//location manager to acces location service

String provider;
//the location provider. Here the provider would be GPS
//You can also use network provider as provider
//GPS can only be used outdoors
 
Inside the onCreate() method add: 
txtLat = (TextView)findViewById(R.id.txtLat);
txtLong = (TextView)findViewById(R.id.txtLong);
txtAlt = (TextView)findViewById(R.id.txtAlt);
 
This is used to get the TextViews from the layout into the java platform. 
SETTING LOCATION MANAGER 
 
//getting location manager object
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
This will set the default system location service to be used as location manager. 
Set the provider to GPS 
provider = LocationManager.GPS_PROVIDER;

Copyright © Arjun P (Arjun Atlast)    fb.com/arjunatlast 
 
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location  5 
 

GETTING LOCATION FROM PROVIDER 
 
//get location from the provider
Location location = manager.getLastKnownLocation(provider);

manager.requestLocationUpdates(provider, 0, 0, this);

if (location != null) {
onLocationChanged(location);
} else
Toast.makeText(this, "Location cannot be accessed", Toast.LENGTH_SHORT).show();
­ This will get location from the location manager and the location manager 
requests updates for location after min 0 time and min 0 distance and uses 
the MainActivity as location listener. 
Now call the onLocationChanged method 
if (location != null) {
onLocationChanged(location);
} else
Toast.makeText(this, "Location cannot be accessed", Toast.LENGTH_SHORT).show();
 
Final onCreate Method: 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtLat = (TextView) findViewById(R.id.txtLat);


txtLong = (TextView) findViewById(R.id.txtLong);
txtAlt = (TextView) findViewById(R.id.txtAlt);

//getting location manager object


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

provider = LocationManager.GPS_PROVIDER;

//get location from the provider


Location location = manager.getLastKnownLocation(provider);

manager.requestLocationUpdates(provider,0,0,this);

if (location != null) {
onLocationChanged(location);
} else
Toast.makeText(this, "Location cannot be accessed",
Toast.LENGTH_SHORT).show();
}
   

Copyright © Arjun P (Arjun Atlast)    fb.com/arjunatlast 
 
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location  6 
 

Now inside the onLocationChanged method set the TextView texts to Latitude, 
Longitude and Altitude of the location we got. 
@Override
public void onLocationChanged(Location location) {

txtLat.setText(Double.toString(location.getLatitude()));
txtLong.setText(Double.toString(location.getLongitude()));
txtAlt.setText(Double.toString(location.getAltitude()));
}
 
Final MainActivity class 
package com.blogspot.shuttereditz.geolocation;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements LocationListener {

TextView txtLat, txtLong, txtAlt;


//textviews that display latitude, longitude, altitude values

LocationManager manager;
//location manager to acces location service

String provider;
//the location provider. Here the provider would be GPS
//You can also use network provider as provider
//GPS can only be used outdoors

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtLat = (TextView) findViewById(R.id.txtLat);


txtLong = (TextView) findViewById(R.id.txtLong);
txtAlt = (TextView) findViewById(R.id.txtAlt);

//getting location manager object


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

provider = LocationManager.GPS_PROVIDER;

//get location from the provider


Location location = manager.getLastKnownLocation(provider);

manager.requestLocationUpdates(provider,0,0,this);

if (location != null) {
onLocationChanged(location);
} else

Copyright © Arjun P (Arjun Atlast)    fb.com/arjunatlast 
 
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location  7 
 

Toast.makeText(this, "Location cannot be accessed",


Toast.LENGTH_SHORT).show();
}

@Override
public void onLocationChanged(Location location) {

txtLat.setText(Double.toString(location.getLatitude()));
txtLong.setText(Double.toString(location.getLongitude()));
txtAlt.setText(Double.toString(location.getAltitude()));
}

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

@Override
public void onProviderEnabled(String provider) {

@Override
public void onProviderDisabled(String provider) {

}
}
 

Testing
 
While testing you should know that 
­ GPS locating cannot be used indoors. 
­ If using an emulator you need to pass the location values yourself using the 
settings of the emulator. 
­ So, it is better to test it in a device outdoor. 
 

Copyright © Arjun P (Arjun Atlast)    fb.com/arjunatlast 
 
Copyright (c) LinuXpert Systems, Chennai

You might also like