Current Location Android

06-Dec-2024

Current Location Android


1/  implement the code


implementation ("com.google.android.gms:play-services-location:21.0.1")



1/  use the parmission


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




1/  XML Code


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


<TextView
android:id="@+id/tx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="tx1" />

<TextView
android:id="@+id/tx2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tx1"
android:layout_centerInParent="true"
android:text="tx2"

/>

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tx2"
android:layout_centerInParent="true"
android:text="Click Here"

/>


</RelativeLayout>



1/  Kotlin Code


package com.tutorialb.testkotlin

import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import android.Manifest


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


class MainActivity : AppCompatActivity() {

private lateinit var fusedLocationClient: FusedLocationProviderClient
lateinit var latitude: TextView
lateinit var longitude: TextView


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)

latitude = findViewById(R.id.tx1)
longitude = findViewById(R.id.tx2)
val bt1: Button = findViewById<Button>(R.id.bt1)

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)



bt1.setOnClickListener {

getLocation()

}








}


fun getLocation() {

if (
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
100
)
return

}


fusedLocationClient.lastLocation
.addOnSuccessListener { location ->
if (location != null) {


val latitudeText = "Latitude:" + location.latitude.toString()
val longitudeText = "Longitude: " + location.latitude.toString()
this.latitude.text = latitudeText
this.longitude.text = longitudeText

// Toast.makeText(
// this,
// "Latitude: $latitude, Longitude: $longitude",
// Toast.LENGTH_SHORT
// ).show()
} else {
// Toast.makeText(this, "Unable to fetch location", Toast.LENGTH_SHORT).show()
}
}
}









}




Comments