1616
1717package com.google.android.gms.location.sample.basiclocationsample
1818
19+ import android.Manifest
1920import android.Manifest.permission.ACCESS_COARSE_LOCATION
21+ import android.Manifest.permission.ACCESS_FINE_LOCATION
2022import android.annotation.SuppressLint
2123import android.content.Intent
24+ import android.content.pm.PackageManager
2225import android.content.pm.PackageManager.PERMISSION_GRANTED
26+ import android.location.Location
2327import android.net.Uri
2428import android.os.Bundle
29+ import android.os.Looper
2530import android.provider.Settings
2631import android.util.Log
2732import android.view.View
2833import androidx.appcompat.app.AppCompatActivity
2934import androidx.core.app.ActivityCompat
3035import androidx.viewbinding.BuildConfig
31- import com.google.android.gms.location.FusedLocationProviderClient
32- import com.google.android.gms.location.LocationServices
36+ import com.google.android.gms.location.*
3337import com.google.android.gms.location.sample.basiclocationsample.databinding.MainActivityBinding
3438import com.google.android.material.snackbar.Snackbar
3539import com.google.android.material.snackbar.Snackbar.LENGTH_INDEFINITE
@@ -58,7 +62,7 @@ class MainActivity: AppCompatActivity() {
5862 binding = MainActivityBinding .inflate(layoutInflater)
5963 val view = binding.root
6064 setContentView(view)
61-
65+
6266 fusedLocationClient = LocationServices .getFusedLocationProviderClient(this )
6367 }
6468
@@ -75,9 +79,11 @@ class MainActivity: AppCompatActivity() {
7579 * Return the current state of the permissions needed.
7680 */
7781 private fun checkPermissions () = ActivityCompat .checkSelfPermission(this ,
78- ACCESS_COARSE_LOCATION ) == PERMISSION_GRANTED
82+ ACCESS_COARSE_LOCATION ) == PERMISSION_GRANTED && ActivityCompat .checkSelfPermission(
83+ this , ACCESS_FINE_LOCATION ) == PERMISSION_GRANTED
7984 private fun requestPermissions () {
80- if (ActivityCompat .shouldShowRequestPermissionRationale(this , ACCESS_COARSE_LOCATION )) {
85+ if (ActivityCompat .shouldShowRequestPermissionRationale(this , ACCESS_COARSE_LOCATION )
86+ && ActivityCompat .shouldShowRequestPermissionRationale(this , ACCESS_FINE_LOCATION )) {
8187 // Provide an additional rationale to the user. This would happen if the user denied the
8288 // request previously, but didn't check the "Don't ask again" checkbox.
8389 Log .i(TAG , " Displaying permission rationale to provide additional context." )
@@ -95,8 +101,8 @@ class MainActivity: AppCompatActivity() {
95101 }
96102 }
97103 private fun startLocationPermissionRequest () {
98- ActivityCompat .requestPermissions(this , arrayOf(ACCESS_COARSE_LOCATION ) ,
99- REQUEST_PERMISSIONS_REQUEST_CODE )
104+ ActivityCompat .requestPermissions(this , arrayOf(ACCESS_COARSE_LOCATION ,
105+ ACCESS_FINE_LOCATION ), REQUEST_PERMISSIONS_REQUEST_CODE )
100106 }
101107 /* *
102108 * Callback received when a permissions request has been completed.
@@ -152,21 +158,63 @@ class MainActivity: AppCompatActivity() {
152158 */
153159 @SuppressLint(" MissingPermission" )
154160 private fun getLastLocation () {
155- fusedLocationClient.lastLocation
156- .addOnCompleteListener { taskLocation ->
157- if (taskLocation.isSuccessful && taskLocation.result != null ) {
158-
159- val location = taskLocation.result
160-
161- binding.currentLatitude.text = resources
162- .getString(R .string.latitude_label, location?.latitude)
163- binding.currentLongitude.text = resources
164- .getString(R .string.longitude_label, location?.longitude)
165- } else {
166- Log .w(TAG , " getLastLocation:exception" , taskLocation.exception)
167- showSnackbar(R .string.no_location_detected)
168- }
169- }
161+ Log .d(TAG , " getLastLocation" )
162+ fusedLocationClient.lastLocation.addOnCompleteListener { taskLocation ->
163+ if (taskLocation.isSuccessful && taskLocation.result != null ) {
164+ updateViews(taskLocation.result)
165+ } else {
166+ requestNewLocationData()
167+ /* Log.w(TAG, "getLastLocation:exception", taskLocation.exception)
168+ showSnackbar(R.string.no_location_detected)*/
169+ }
170+ }
171+ }
172+ fun updateViews (currentLocation : Location ) {
173+ Log .d(TAG , " updateViews" )
174+ binding.currentLatitude.text = resources
175+ .getString(R .string.latitude_label, currentLocation.latitude)
176+ binding.currentLongitude.text = resources
177+ .getString(R .string.longitude_label, currentLocation.longitude)
178+ }
179+ fun requestNewLocationData () {
180+ Log .d(TAG , " requestNewLocationData" )
181+ // Initializing LocationRequest
182+ // object with appropriate methods
183+ val locationRequest = LocationRequest ().apply {
184+ // For a high level accuracy use PRIORITY_HIGH_ACCURACY argument.
185+ // For a low level accuracy (city), use PRIORITY_LOW_POWER
186+ priority = LocationRequest .PRIORITY_HIGH_ACCURACY
187+ interval = 3
188+ fastestInterval = 1
189+ numUpdates = 2
190+ }
191+
192+ // setting LocationRequest on a FusedLocationClient
193+ fusedLocationClient = LocationServices .getFusedLocationProviderClient(this )
194+
195+ /* if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
196+ == PackageManager.PERMISSION_DENIED && ActivityCompat.checkSelfPermission(this,
197+ Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) {
198+ ActivityCompat.requestPermissions(
199+ requireActivity(), arrayOf(Manifest.permission_group.LOCATION), PERMISSIONS_ALLOW_USING_LOCATION_ID
200+ )
201+ } else {
202+ locationClient?.requestLocationUpdates(locationRequest,
203+ locationCallback, Looper.myLooper())
204+ }*/
205+
206+ if (ActivityCompat .checkSelfPermission(this , ACCESS_FINE_LOCATION )
207+ == PERMISSION_GRANTED && ActivityCompat .checkSelfPermission(this ,
208+ ACCESS_COARSE_LOCATION ) == PERMISSION_GRANTED ) {
209+ fusedLocationClient.requestLocationUpdates(locationRequest,
210+ locationCallback, Looper .myLooper())
211+ }
212+ }
213+ private val locationCallback: LocationCallback = object : LocationCallback () {
214+ override fun onLocationResult (locationResult : LocationResult ) {
215+ Log .d(TAG , " onLocationResult" )
216+ updateViews(locationResult.lastLocation)
217+ }
170218 }
171219 /* *
172220 * Shows a [Snackbar].
0 commit comments