0% found this document useful (0 votes)
14 views4 pages

Exp 31

The document outlines the implementation of an Android application that retrieves the user's current location using location permissions and the Fused Location Provider. It includes the necessary XML files for the AndroidManifest, strings, and the layout, as well as the Java code for the MainActivity that handles location fetching and displaying it. Additionally, it provides functionality to open the user's location in Google Maps via a button click.

Uploaded by

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

Exp 31

The document outlines the implementation of an Android application that retrieves the user's current location using location permissions and the Fused Location Provider. It includes the necessary XML files for the AndroidManifest, strings, and the layout, as well as the Java code for the MainActivity that handles location fetching and displaying it. Additionally, it provides functionality to open the user's location in Google Maps via a button click.

Uploaded by

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

MAD Experiment 31

1. Write a program to locate user’s current location.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Indecx"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

strings.xml:

<resources>
<string name="app_name">Exp31</string>
<string name="google_api_key">AIzaSyD4KfDcLVjVPatpIKtrPXot024Z4UHVZQ8</string>
</resources>

activity_main.xml:

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center">
<TextView
android:id="@+id/locationTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fetching location..."
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
<Button
android:id="@+id/openMapButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open in Google Maps"/>
</LinearLayout>

MainActivity.java:

package com.example.exp_31;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
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 static
final int LOCATION_PERMISSION_REQUEST = 1; private
TextView locationTextView;
private Button openMapButton;
private double latitude, longitude;
@SuppressLint("MissingInflatedId")
@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationTextView = findViewById(R.id.locationTextView);
openMapButton = findViewById(R.id.openMapButton);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
// Check location permissions
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},

LOCATION_PERMISSION_REQUEST);
} else {
getLastLocation();
}
// Open Google Maps when button is clicked
openMapButton.setOnClickListener(view -> {
if (latitude != 0 && longitude != 0) {
String uri = "geo:" + latitude + "," + longitude + "?q=" + latitude + "," + longitude +
"(Your Location)";

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));


intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
}
});
}
// Fetch last known location
private void getLastLocation() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
fusedLocationClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();

locationTextView.setText("Latitude: " + latitude + "\nLongitude: " + longitude);

} else {
locationTextView.setText("Unable to get location.");
}
}
});
}
}
// Handle permission request result
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,

@NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);


if (requestCode == LOCATION_PERMISSION_REQUEST) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
getLastLocation();
} else {
locationTextView.setText("Permission denied.");
}
}
}
}
Output:

You might also like