Get user location in Android

Overview

Steps to follow

  • Add the following permissions in AndroidManifest.xml file
 <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"/>
  • Add the following dependencies in gradle.build (Module) file
 implementation 'com.google.android.gms:play-services-location:19.0.0'
  • Create a layout
  • Write the logic
 private lateinit var fusedLocationProviderClient : FusedLocationProviderClient
private val PERMISSION_ID = 44
override fun onCreate(savedInstanceState : Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fusedLocationProviderClient = FusedLocationProviderClient(this)
getLastLocation()
}

@SuppressLint("MissingPermission")
private fun getLastLocation()
{
if (checkPermissions())
{
if (isLocationEnable())
{
fusedLocationProviderClient.lastLocation.addOnCompleteListener {
val location = it.result
if (location == null)
{
requestNewLocationData()
} else
{
tvLatitude.text = location.latitude.toString()
tvLongitude.text = location.longitude.toString()
}
}
} else
{
Toast.makeText(this , "Please turn on your location" , Toast.LENGTH_LONG).show()
/*Intent is created to go to System Settings screen*/
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS).apply {
startActivity(this)
}
}
} else
{
requestPermissions()
}

}

private fun checkPermissions() : Boolean
{
return ActivityCompat.checkSelfPermission(
this , android.Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this , android.Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
}

private fun isLocationEnable() : Boolean
{
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(
LocationManager.NETWORK_PROVIDER
)
}

override fun onRequestPermissionsResult(
requestCode : Int , permissions : Array<out String> , grantResults : IntArray
)
{
super.onRequestPermissionsResult(requestCode , permissions , grantResults)
if (requestCode == PERMISSION_ID)
{
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
getLastLocation()
}
}
}

@SuppressLint("MissingPermission")
private fun requestNewLocationData()
{
val locationRequest = LocationRequest()
locationRequest.interval = 50000
locationRequest.fastestInterval = 50000
locationRequest.smallestDisplacement = 170f // 170m = 0.1 mile
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.numUpdates = 1
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationProviderClient.requestLocationUpdates(
locationRequest , locationCallback , Looper.myLooper() !!
)
}

private val locationCallback = object : LocationCallback()
{
override fun onLocationResult(locationResult : LocationResult)
{
val location = locationResult.lastLocation
tvLatitude.text = location.latitude.toString()
tvLongitude.text = location.longitude.toString()
}

override fun onLocationAvailability(locationAvailability : LocationAvailability)
{
super.onLocationAvailability(locationAvailability)
}
}

private fun requestPermissions()
{
ActivityCompat.requestPermissions(
this , arrayOf(
android.Manifest.permission.ACCESS_COARSE_LOCATION ,
android.Manifest.permission.ACCESS_FINE_LOCATION
) , PERMISSION_ID
)
/*Note: these permissions must be declared in AndroidManifest.xml file*/
}

Reference

  1. https://www.geeksforgeeks.org/how-to-get-user-location-in-android/
  2. https://www.journaldev.com/13347/android-location-google-play-services
  3. https://stackoverflow.com/questions/45958226/get-location-android-kotlin
  4. https://github.com/iamraajkanchan/GeoLocation_Android





Comments

Popular posts from this blog

Architecture Components in Android

DataBinding in Android

SSLSocketFactory in Android