0% found this document useful (0 votes)
17 views8 pages

Practical-6 MAD

This document outlines the steps to develop an Android application named 'GeoLocation' that utilizes GPS to retrieve and display location data such as latitude, longitude, and altitude. It includes instructions for setting up the layout, obtaining necessary permissions, implementing the LocationListener interface, and updating the UI with location information. The final code for the MainActivity class is also provided, demonstrating how to manage location updates and display the retrieved data.

Uploaded by

vhoratanvir1610
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

Practical-6 MAD

This document outlines the steps to develop an Android application named 'GeoLocation' that utilizes GPS to retrieve and display location data such as latitude, longitude, and altitude. It includes instructions for setting up the layout, obtaining necessary permissions, implementing the LocationListener interface, and updating the UI with location information. The final code for the MainActivity class is also provided, demonstrating how to manage location updates and display the retrieved data.

Uploaded by

vhoratanvir1610
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Mobile Application Development (202046712) 12202080501060

Practical-6
Aim : Develop an android application that uses GPS location information.
In this Practical 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"

GCET 32
Mobile Application Development (202046712) 12202080501060

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.

Open AndroidManifest.xml file and add permission to access fine location


Fine Location locates smaller changes in position.

GCET 33
Mobile Application Development (202046712) 12202080501060

<?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>

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.

@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) {

GCET 34
Mobile Application Development (202046712) 12202080501060

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;
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();

GCET 35
Mobile Application Development (202046712) 12202080501060

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(); }

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

GCET 36
Mobile Application Development (202046712) 12202080501060

@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();
}

@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) {

}
}

Output :

GCET 37
Mobile Application Development (202046712) 12202080501060

GCET 38

You might also like