0% found this document useful (0 votes)
38 views3 pages

User Location Finder App Code

The document outlines the implementation of an Android application that retrieves the user's current location using the Fused Location Provider. It includes the XML layout for the main activity and the Java code for handling location permissions and displaying the latitude and longitude. The program prompts the user to grant location permissions and displays the location once retrieved.
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)
38 views3 pages

User Location Finder App Code

The document outlines the implementation of an Android application that retrieves the user's current location using the Fused Location Provider. It includes the XML layout for the main activity and the Java code for handling location permissions and displaying the latitude and longitude. The program prompts the user to grant location permissions and displays the location once retrieved.
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

Experiment 31

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


Activity_main.xml tivity;
<?xml version="1.0" encoding="utf-8"?> import [Link];
<LinearLayout import [Link];
xmlns:android="[Link] import
/apk/res/android" [Link];
import [Link];
xmlns:app="[Link]
k/res-auto"
import [Link];
import [Link];
xmlns:tools="[Link] import [Link];
ols" import [Link];
android:layout_width="match_parent" import
android:layout_height="match_parent" [Link]
android:gravity="center" LocationProviderClient;
android:orientation="vertical" import
android:padding="16dp"> [Link]
onServices;
<TextView
import
android:id="@+id/tvLocation"
android:text="Location will appear
[Link]
here" leteListener;
android:textSize="18sp" import
android:layout_width="wrap_content" [Link];
android:layout_height="wrap_content"
android:padding="10dp" public class MainActivity extends
android:textColor="#000000" /> AppCompatActivity {
private FusedLocationProviderClient
<Button fusedLocationClient;
android:id="@+id/btnGetLocation" private TextView tvLocation;
android:text="Get Current Location" private Button btnGetLocation;
android:layout_width="wrap_content"
private final int
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
LOCATION_PERMISSION_REQUEST_
android:padding="10dp" /> CODE = 1;
</LinearLayout> @Override
protected void onCreate(Bundle
[Link] savedInstanceState) {

package [Link].exp_23; [Link](savedInstanceState);


import [Link];
import setContentView([Link].activity_main);
[Link] tvLocation =
findViewById([Link]); [Link]();
btnGetLocation =
findViewById([Link]);
fusedLocationClient = [Link]("Latitude: " + latitude
[Link] + "\nLongitude: " + longitude);
iderClient(this); } else {

[Link](v -> [Link]([Link],


getCurrentLocation()); "Failed to get location",
} Toast.LENGTH_SHORT).show();
@SuppressLint("MissingPermission") }
private void getCurrentLocation() { }
if });
(checkSelfPermission([Link] }
on.ACCESS_FINE_LOCATION) != @Override
PackageManager.PERMISSION_GRAN public void
TED && onRequestPermissionsResult(int
requestCode, @NonNull String[]
checkSelfPermission([Link] permissions, @NonNull int[]
n.ACCESS_COARSE_LOCATION) != grantResults) {
PackageManager.PERMISSION_GRAN
TED) { [Link](req
requestPermissions(new String[] uestCode, permissions, grantResults);
{[Link].ACCESS_FINE_L if (requestCode ==
OCATION, LOCATION_PERMISSION_REQUEST_
[Link].ACCESS_COARS CODE) {
E_LOCATION}, if ([Link] > 0 &&
grantResults[0] ==
LOCATION_PERMISSION_REQUEST_ PackageManager.PERMISSION_GRAN
CODE); TED) {
return; getCurrentLocation();
} } else {
[Link](this,
[Link]() "Permission Denied",
.addOnCompleteListener(new Toast.LENGTH_SHORT).show();
OnCompleteListener<Location>() { }
@Override }
public void }
onComplete(@NonNull Task<Location> }
task) {
if ([Link]() &&
[Link]() != null) {
Location location =
[Link]();
double latitude =
[Link]();
double longitude =
Output

You might also like