2

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!

2
  • Please provide enough code so others can better understand or reproduce the problem. Commented Mar 5, 2023 at 14:25
  • 4
    Maybe this post could give you some indications using Flow with LocationManager: conorsmith.dev/post/kotlin_flow_location_updates Commented Mar 28, 2023 at 15:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.