I am new to Kotlin Flow. Currently I am using LocationManager to get user Location updates like below:
private var locationManager: LocationManager? = null
locationManager = mContext?.getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (locationManager != null) {
mLocation = locationManager!!
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
locationManager!!.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), locationProviderListener
)
return mLocation
}
private var locationProviderListener: LocationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
try {
val latitude = location.latitude
val longitude = location.longitude
updateLatitudeLongitude(latitude, longitude)
updateLocation(location)
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun onStatusChanged(s: String, i: Int, bundle: Bundle) {}
override fun onProviderEnabled(s: String) {}
override fun onProviderDisabled(s: String) {}
}
And this works fine. But now I want to get location updates using Kotlin Flow. Any help will be appreciated!