Android’s Location Manager API

Before diving into the location manager and its listener firstly we need to understand that what is providers are available in our android device for location. There are two provider’s available on any device:

  1. GPS Provider
  2. Network Provider

Both of them have their unique advantages, but we have to use both to get precise location coordinates. Because in some cases like in-door situations Network works and in other GPS works. The network gives the location based on the nearest mobile towers while GPS gives us the exact location.

First of all, before implementing any method we have to take user permission:

Location Permission

To protect user privacy, apps that use location services must request location permissions. Therefore we have to set the two permissions in the Android Manifest file before starting anything.

<!-- To request foreground location access, declare one of these permissions. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Required only when requesting background location access on Android 10 (API level 29). -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

We may also ask for permission for Internet access as we are using the Network
provider and also saving the coordinates in Firestore
<uses-permission android:name="android.permission.INTERNET"/>

We can check that if permissions are granted or not. If not granted then we can request permission in real time.

if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED            && ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
 {
ActivityCompat.requestPermissions(this,arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION,                    android.Manifest.permission.ACCESS_COARSE_LOCATION)
,requestcode
 }
<!--Here above we checks first that if permission is not granted through ActivityCompat and then asks for permission in realtime by requestpermission which takes three arguement - Context, permission , RequestCode. -->
Location Manager Instance

Now that we have checked for the permissions, it’s time for getting location by creating the location manager instance in the context of Location service

lateinit var locationManager: LocationManager
<!-- lateinitialize a variable locationManager -->
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager

 

Now we will check that if the GPS is on or not and we also check for network 
and if both then we use one with greater accuracy.

If any of the GPS or Network provider is enabled then we will request a current location update from the location manager like this :

locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,minTimeinMS,minDistanceinM,locationlistener)
In the above code snippet, you will see the location listener and the other two 
arguments for time and distance
We will receive location updates from LocationListener on change of location.
LocationListener will be notified when the distance interval specified or the
number of seconds changes.

And LocationListner has some callbacks methods to provide location-based on some behaviors in the android device, but in kotlin, we use only one that is onLocationChanged which will update in the specified time and distance provided in locationmanager.requestlocationupdate .

override fun onLocationChanged(p0: Location) {
    TODO("Not yet implemented")
}

onLocationChanged(p0: Location) — Run’s when location is changed.

override fun onProviderDisabled(provider: String) {
super.onProviderDisabled(provider)
}
override fun onProviderEnabled(provider: String) {
super.onProviderEnabled(provider)
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
super.onStatusChanged(provider, status, extras)
}

There are also three other callbacks like :

onProviderDisabled(provider: String— Run’s when the provider is disabled in Device.

onProviderEnabled(provider: String) — Run’s when the provider is enabled in Device.

onStatusChanged(provider: String?, status: Int, extras: Bundle?) — This callback will never be invoked on Android Q and above, and providers can be considered as always in the LocationProvider#AVAILABLE state.

Discussion
Nice article rohit

2

13

1