1
1
package com.mindorks.example.fusedlocation
2
2
3
+ import android.content.pm.PackageManager
3
4
import androidx.appcompat.app.AppCompatActivity
4
5
import android.os.Bundle
6
+ import android.os.Looper
7
+ import android.widget.Toast
8
+ import com.google.android.gms.location.LocationCallback
9
+ import com.google.android.gms.location.LocationRequest
10
+ import com.google.android.gms.location.LocationResult
11
+ import com.google.android.gms.location.LocationServices
12
+ import com.mindorks.example.fusedlocation.utils.PermissionUtils
13
+ import kotlinx.android.synthetic.main.activity_main.*
5
14
6
15
class MainActivity : AppCompatActivity () {
7
16
17
+ companion object {
18
+ private const val LOCATION_PERMISSION_REQUEST_CODE = 999
19
+ }
8
20
override fun onCreate (savedInstanceState : Bundle ? ) {
9
21
super .onCreate(savedInstanceState)
10
22
setContentView(R .layout.activity_main)
11
23
}
12
- }
24
+
25
+ private fun setUpLocationListener () {
26
+ val fusedLocationProviderClient = LocationServices .getFusedLocationProviderClient(this )
27
+ // for getting the current location update after every 2 seconds with high accuracy
28
+ val locationRequest = LocationRequest ().setInterval(2000 ).setFastestInterval(2000 )
29
+ .setPriority(LocationRequest .PRIORITY_HIGH_ACCURACY )
30
+ fusedLocationProviderClient.requestLocationUpdates(
31
+ locationRequest,
32
+ object : LocationCallback () {
33
+ override fun onLocationResult (locationResult : LocationResult ) {
34
+ super .onLocationResult(locationResult)
35
+ for (location in locationResult.locations) {
36
+ latTextView.text = location.latitude.toString()
37
+ lngTextView.text = location.longitude.toString()
38
+ }
39
+ // Few more things we can do here:
40
+ // For example: Update the location of user on server
41
+ }
42
+ },
43
+ Looper .myLooper()
44
+ )
45
+ }
46
+
47
+ override fun onStart () {
48
+ super .onStart()
49
+ when {
50
+ PermissionUtils .isAccessFineLocationGranted(this ) -> {
51
+ when {
52
+ PermissionUtils .isLocationEnabled(this ) -> {
53
+ setUpLocationListener()
54
+ }
55
+ else -> {
56
+ PermissionUtils .showGPSNotEnabledDialog(this )
57
+ }
58
+ }
59
+ }
60
+ else -> {
61
+ PermissionUtils .requestAccessFineLocationPermission(
62
+ this ,
63
+ LOCATION_PERMISSION_REQUEST_CODE
64
+ )
65
+ }
66
+ }
67
+ }
68
+
69
+ override fun onRequestPermissionsResult (
70
+ requestCode : Int ,
71
+ permissions : Array <out String >,
72
+ grantResults : IntArray
73
+ ) {
74
+ super .onRequestPermissionsResult(requestCode, permissions, grantResults)
75
+ when (requestCode) {
76
+ LOCATION_PERMISSION_REQUEST_CODE -> {
77
+ if (grantResults.isNotEmpty() && grantResults[0 ] == PackageManager .PERMISSION_GRANTED ) {
78
+ when {
79
+ PermissionUtils .isLocationEnabled(this ) -> {
80
+ setUpLocationListener()
81
+ }
82
+ else -> {
83
+ PermissionUtils .showGPSNotEnabledDialog(this )
84
+ }
85
+ }
86
+ } else {
87
+ Toast .makeText(
88
+ this ,
89
+ getString(R .string.location_permission_not_granted),
90
+ Toast .LENGTH_LONG
91
+ ).show()
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
0 commit comments