0% found this document useful (0 votes)
8 views12 pages

Adi mc4

The document outlines an experiment conducted by Yashashree Dhake to develop an Android application that utilizes GPS location information. It details the steps for setting up the project in Android Studio, including designing the layout and writing Java code for location permissions and fetching the current location. The application successfully retrieves and displays the user's latitude and longitude upon permission granted.

Uploaded by

dhakeyashashree
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)
8 views12 pages

Adi mc4

The document outlines an experiment conducted by Yashashree Dhake to develop an Android application that utilizes GPS location information. It details the steps for setting up the project in Android Studio, including designing the layout and writing Java code for location permissions and fetching the current location. The application successfully retrieves and displays the user's latitude and longitude upon permission granted.

Uploaded by

dhakeyashashree
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/ 12

Name: YASHASHREE DHAKE Roll No: 8 Batch: T1

Experiment No: -
AIM: Develop an application that uses GPS location information.

Software/Hardware Used: Android Studio.

Theory: Steps to use and create application using Android Studio.

Step1: Install Android studio and complete the setup.


Name: YASHASHREE DHAKE Roll No: 8 Batch: T1

Step2: Start a New Android Project.

Step3: Choose Project Template and Configure Your Project.


Name: YASHASHREE DHAKE Roll No: 8 Batch: T1

Step4: Design the Layout that includes the button.

Step5: Write java code to handle button clicks and permissions for location.
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1

Step6: Running the project to see the output appear on the screen.

Conclusion: Thus, we successfully implemented the application that uses GPS location
information.
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1

Code: MainActivity.java
package com.example.gps;

import android.Manifest;

import android.content.pm.PackageManager;

import android.location.Location;

import android.os.Bundle;

import android.widget.Button;

import android.widget.Toast;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

import com.example.gps.R;

import com.google.android.gms.location.FusedLocationProviderClient;

import com.google.android.gms.location.LocationServices;

import com.google.android.gms.tasks.OnSuccessListener;

public class MainActivity extends AppCompatActivity {

private FusedLocationProviderClient fusedLocationClient;

private Button buttonGetLocation;

private static final int PERMISSION_REQUEST_CODE = 101;

@Override

protected void onCreate(Bundle savedInstanceState)

{ super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); // Make sure this points to the correct layout


Name: YASHASHREE DHAKE Roll No: 8 Batch: T1

// Initialize the FusedLocationProviderClient

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

// Find views by ID

buttonGetLocation = findViewById(R.id.button); // Button to trigger location fetch

// Set click listener for the "Get Location" button

buttonGetLocation.setOnClickListener(v -> {

// Check if permission is already granted

if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {

// Request permission if not granted

ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_CODE);

} else {

// Permission granted, get the location

getLocation();
}

});

// Method to fetch the current location

private void getLocation() {


if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !
= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {

// TODO: Consider calling

// ActivityCompat#requestPermissions

// here to request the missing permissions, and then overriding

// public void onRequestPermissionsResult(int requestCode, String[] permissions,

// int[] grantResults)
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1

// to handle the case where the user grants the permission. See the documentation

// for ActivityCompat#requestPermissions for more details.

return;
}

fusedLocationClient.getLastLocation()

.addOnSuccessListener(this, new OnSuccessListener<Location>()

{ @Override
public void onSuccess(Location location) {

// Check if location is not null

if (location != null) {

double latitude = location.getLatitude();

double longitude = location.getLongitude();

// Show location in a Toast message

Toast.makeText(MainActivity.this, "Latitude: " + latitude + ", Longitude: " + longitude,


Toast.LENGTH_LONG).show();

} else {

// If no location is found, show a message

Toast.makeText(MainActivity.this, "Location not available.", Toast.LENGTH_SHORT).show();

});

// Handle permission request result (for location permission)

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{

// Permission granted, now get the location

Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();


Name: YASHASHREE DHAKE Roll No: 8 Batch: T1

getLocation();

} else {

// Permission denied, show a message to the user

Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();

}}Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- Button to trigger location fetch -->

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Location"
android:textColor="#F4E10B"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.602" />

<ImageView
android:id="@+id/imageView8"
android:layout_width="182dp"
android:layout_height="257dp"
android:layout_marginStart="114dp"
android:layout_marginTop="128dp"
android:layout_marginEnd="115dp"
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/google_maps_logo_2020" />

</androidx.constraintlayout.widget.ConstraintLayout>
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1

OUTPUT:
Generated Button Asking For Permission

Permission Granted Location Detected


Name: YASHASHREE DHAKE Roll No: 8 Batch: T1
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1

You might also like