0% found this document useful (0 votes)
4 views

Location with GeoCoder

The document outlines the implementation of a location-based Android application using Kotlin. It includes necessary permissions in the AndroidManifest.xml, layout definitions in activity_location.xml, and the main logic for fetching and displaying location data in LocationActivity.kt. The app utilizes Google's location services to retrieve and display the user's latitude, longitude, address, city, and country upon button click, while also handling permission requests for location access.

Uploaded by

Mg Zin
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)
4 views

Location with GeoCoder

The document outlines the implementation of a location-based Android application using Kotlin. It includes necessary permissions in the AndroidManifest.xml, layout definitions in activity_location.xml, and the main logic for fetching and displaying location data in LocationActivity.kt. The app utilizes Google's location services to retrieve and display the user's latitude, longitude, address, city, and country upon button click, while also handling permission requests for location access.

Uploaded by

Mg Zin
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/ 6

Geo Location

build.gradle.kts
implementation("com.google.android.gms:play-services-location:21.3.0")

AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

activity_location.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".LocationActivity"
android:orientation="vertical">

<TextView
android:id="@+id/txtLatitude"
android:background="#FFEB3B"
android:textColor="#000"
android:padding="10dp"
android:textSize="25sp"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txtLongitude"
android:background="#FFEB3B"
android:textColor="#000"
android:padding="10dp"
android:textSize="25sp"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/txtAddress"
android:background="#FFEB3B"
android:textColor="#000"
android:padding="10dp"
android:textSize="25sp"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/txtCity"
android:background="#FFEB3B"
android:textColor="#000"
android:padding="10dp"
android:textSize="25sp"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/txtCountry"
android:background="#FFEB3B"
android:textColor="#000"
android:padding="10dp"
android:textSize="25sp"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/btnGetLocation"
android:text="Get Location"
android:backgroundTint="#000"
android:padding="20dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

LocationActivity.kt

package com.example.locationapp

import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.location.Address
import android.location.Geocoder
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.locationapp.databinding.ActivityLocationBinding
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import java.io.IOException
import java.util.Locale

class LocationActivity : AppCompatActivity() {


private lateinit var binding: ActivityLocationBinding

private val REQUEST_CODE = 100

private lateinit var fusedLocationProviderClient: FusedLocationProviderClient

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)

binding = ActivityLocationBinding.inflate(layoutInflater)

setContentView(binding.root)

fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)

binding.btnGetLocation.setOnClickListener{
getLastLocation()
}

}//onCreate
@SuppressLint("SetTextI18n")
private fun getLastLocation() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
fusedLocationProviderClient.lastLocation.addOnSuccessListener { location ->
location?.let {
try {

Log.d("Location***","Location: "+it.latitude+"..."+it.longitude)
val geocoder = Geocoder(this, Locale.getDefault())
val addresses: List<Address>? = geocoder.getFromLocation(it.latitude, it.longitude, 1)
val address = addresses?.get(0)
binding.txtLatitude.text = "Lattitude: ${address!!.latitude}"
binding.txtLongitude.text = "Longitude: ${address.longitude}"
binding.txtAddress.text = "Address: ${address.getAddressLine(0)}"
binding.txtCity.text = "City: ${address.locality}"
binding.txtCountry.text = "Country: ${address.countryName}"
} catch (e: IOException) {
e.printStackTrace()
}
}
}
} else {
askPermission()
}

}//getLastLocation
private fun askPermission() {
ActivityCompat.requestPermissions(this,
arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), REQUEST_CODE)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults:
IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("Location***","Last Location")
getLastLocation()
} else {
Toast.makeText(this, "Please provide the required permission", Toast.LENGTH_LONG).show()
}
}
}
}//LocationActivity

=====================================================================================

You might also like