From f64d6bf5a03d53e927bd5a58df605d9e05dba828 Mon Sep 17 00:00:00 2001 From: dombroks Date: Wed, 23 Jun 2021 21:29:34 +0100 Subject: [PATCH] fixing location request creation --- .../example/fusedlocation/MainActivity.kt | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/mindorks/example/fusedlocation/MainActivity.kt b/app/src/main/java/com/mindorks/example/fusedlocation/MainActivity.kt index 8d6c569..88efc19 100644 --- a/app/src/main/java/com/mindorks/example/fusedlocation/MainActivity.kt +++ b/app/src/main/java/com/mindorks/example/fusedlocation/MainActivity.kt @@ -1,5 +1,6 @@ package com.mindorks.example.fusedlocation +import android.annotation.SuppressLint import android.content.pm.PackageManager import androidx.appcompat.app.AppCompatActivity import android.os.Bundle @@ -12,38 +13,43 @@ import com.google.android.gms.location.LocationServices import com.mindorks.example.fusedlocation.utils.PermissionUtils import kotlinx.android.synthetic.main.activity_main.* +const val LOCATION_PERMISSION_REQUEST_CODE = 999 + class MainActivity : AppCompatActivity() { - companion object { - private const val LOCATION_PERMISSION_REQUEST_CODE = 999 - } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } + @SuppressLint("MissingPermission") private fun setUpLocationListener() { val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this) // for getting the current location update after every 2 seconds with high accuracy - val locationRequest = LocationRequest().setInterval(2000).setFastestInterval(2000) - .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) + val locationRequest = LocationRequest.create().also { + it.interval = 2000 + it.fastestInterval = 2000 + it.priority = LocationRequest.PRIORITY_HIGH_ACCURACY + } fusedLocationProviderClient.requestLocationUpdates( - locationRequest, - object : LocationCallback() { - override fun onLocationResult(locationResult: LocationResult) { - super.onLocationResult(locationResult) - for (location in locationResult.locations) { - latTextView.text = location.latitude.toString() - lngTextView.text = location.longitude.toString() - } - // Few more things we can do here: - // For example: Update the location of user on server + locationRequest, + object : LocationCallback() { + override fun onLocationResult(locationResult: LocationResult) { + super.onLocationResult(locationResult) + for (location in locationResult.locations) { + latTextView.text = location.latitude.toString() + lngTextView.text = location.longitude.toString() } - }, - Looper.myLooper() + // Few more things we can do here: + // For example: Update the location of user on server + } + }, + Looper.myLooper() ) } + override fun onStart() { super.onStart() when { @@ -59,17 +65,17 @@ class MainActivity : AppCompatActivity() { } else -> { PermissionUtils.requestAccessFineLocationPermission( - this, - LOCATION_PERMISSION_REQUEST_CODE + this, + LOCATION_PERMISSION_REQUEST_CODE ) } } } override fun onRequestPermissionsResult( - requestCode: Int, - permissions: Array, - grantResults: IntArray + requestCode: Int, + permissions: Array, + grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) when (requestCode) { @@ -85,12 +91,13 @@ class MainActivity : AppCompatActivity() { } } else { Toast.makeText( - this, - getString(R.string.location_permission_not_granted), - Toast.LENGTH_LONG + this, + getString(R.string.location_permission_not_granted), + Toast.LENGTH_LONG ).show() } } } } -} \ No newline at end of file +} +