In 2019 Best Offical Solution in Kotlin

Google API Client/FusedLocationApi are deprecated and Location Manager is not useful at all. So Google prefer Fused Location Provider Using the Google Play services location APIs "FusedLocationProviderClient" is used to get location and its better way for battery saving and accuracy

Here is sample code in kotlin to get the last known location /one-time location( equivalent to the current location)

 // declare a global variable of FusedLocationProviderClient
    private lateinit var fusedLocationClient: FusedLocationProviderClient

// in onCreate() initialize FusedLocationProviderClient
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!)

 /**
     * call this method for receive location
     * get location and give callback when successfully retrieve
     * function itself check location permission before access related methods
     *
     */
    fun getLastKnownLocation() {
            fusedLocationClient.lastLocation
                .addOnSuccessListener { location->
                    if (location != null) {
                       // use your location object
                        // get latitude , longitude and other info from this
                    }

                }

    }

If your app can continuously track the location then you have to receive Receive location updates

Check the sample for that in kotlin

// declare a global variable FusedLocationProviderClient
        private lateinit var fusedLocationClient: FusedLocationProviderClient

    // in onCreate() initialize FusedLocationProviderClient
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!)


      // globally declare LocationRequest
        private lateinit var locationRequest: LocationRequest

        // globally declare LocationCallback    
        private lateinit var locationCallback: LocationCallback


        /**
         * call this method in onCreate
         * onLocationResult call when location is changed 
         */
        private fun getLocationUpdates()
        {

                fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!)
                locationRequest = LocationRequest()
                locationRequest.interval = 50000
                locationRequest.fastestInterval = 50000
                locationRequest.smallestDisplacement = 170f // 170 m = 0.1 mile
                locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY //set according to your app function
                locationCallback = object : LocationCallback() {
                    override fun onLocationResult(locationResult: LocationResult?) {
                        locationResult ?: return

                        if (locationResult.locations.isNotEmpty()) {
                            // get latest location 
                            val location =
                                locationResult.lastLocation
                            // use your location object
                            // get latitude , longitude and other info from this
                        }


                    }
                }
        }

        //start location updates
        private fun startLocationUpdates() {
            fusedLocationClient.requestLocationUpdates(
                locationRequest,
                locationCallback,
                null /* Looper */
            )
        }

        // stop location updates
        private fun stopLocationUpdates() {
            fusedLocationClient.removeLocationUpdates(locationCallback)
        }

        // stop receiving location update when activity not visible/foreground
        override fun onPause() {
            super.onPause()
            stopLocationUpdates()
        }

        // start receiving location update when activity  visible/foreground
        override fun onResume() {
            super.onResume()
            startLocationUpdates()
        }

Make sure you take care about Mainfaist permission and runtime permission for location

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

and for Gradle add this

implementation 'com.google.android.gms:play-services-location:17.0.0'

For more details follow these official documents

https://developer.android.com/training/location/retrieve-current

https://developer.android.com/training/location/receive-location-updates

https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient

Answer from Mayank Sharma on Stack Overflow
🌐
Android Developers
developer.android.com › api reference › locationmanager
LocationManager | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Top answer
1 of 8
56

In 2019 Best Offical Solution in Kotlin

Google API Client/FusedLocationApi are deprecated and Location Manager is not useful at all. So Google prefer Fused Location Provider Using the Google Play services location APIs "FusedLocationProviderClient" is used to get location and its better way for battery saving and accuracy

Here is sample code in kotlin to get the last known location /one-time location( equivalent to the current location)

 // declare a global variable of FusedLocationProviderClient
    private lateinit var fusedLocationClient: FusedLocationProviderClient

// in onCreate() initialize FusedLocationProviderClient
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!)

 /**
     * call this method for receive location
     * get location and give callback when successfully retrieve
     * function itself check location permission before access related methods
     *
     */
    fun getLastKnownLocation() {
            fusedLocationClient.lastLocation
                .addOnSuccessListener { location->
                    if (location != null) {
                       // use your location object
                        // get latitude , longitude and other info from this
                    }

                }

    }

If your app can continuously track the location then you have to receive Receive location updates

Check the sample for that in kotlin

// declare a global variable FusedLocationProviderClient
        private lateinit var fusedLocationClient: FusedLocationProviderClient

    // in onCreate() initialize FusedLocationProviderClient
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!)


      // globally declare LocationRequest
        private lateinit var locationRequest: LocationRequest

        // globally declare LocationCallback    
        private lateinit var locationCallback: LocationCallback


        /**
         * call this method in onCreate
         * onLocationResult call when location is changed 
         */
        private fun getLocationUpdates()
        {

                fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!)
                locationRequest = LocationRequest()
                locationRequest.interval = 50000
                locationRequest.fastestInterval = 50000
                locationRequest.smallestDisplacement = 170f // 170 m = 0.1 mile
                locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY //set according to your app function
                locationCallback = object : LocationCallback() {
                    override fun onLocationResult(locationResult: LocationResult?) {
                        locationResult ?: return

                        if (locationResult.locations.isNotEmpty()) {
                            // get latest location 
                            val location =
                                locationResult.lastLocation
                            // use your location object
                            // get latitude , longitude and other info from this
                        }


                    }
                }
        }

        //start location updates
        private fun startLocationUpdates() {
            fusedLocationClient.requestLocationUpdates(
                locationRequest,
                locationCallback,
                null /* Looper */
            )
        }

        // stop location updates
        private fun stopLocationUpdates() {
            fusedLocationClient.removeLocationUpdates(locationCallback)
        }

        // stop receiving location update when activity not visible/foreground
        override fun onPause() {
            super.onPause()
            stopLocationUpdates()
        }

        // start receiving location update when activity  visible/foreground
        override fun onResume() {
            super.onResume()
            startLocationUpdates()
        }

Make sure you take care about Mainfaist permission and runtime permission for location

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

and for Gradle add this

implementation 'com.google.android.gms:play-services-location:17.0.0'

For more details follow these official documents

https://developer.android.com/training/location/retrieve-current

https://developer.android.com/training/location/receive-location-updates

https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient

2 of 8
32

When GetUserLocation returns, locationManager goes out of scope and presumably is destroyed, preventing onLocationChanged from being called and providing updates.

Also, you've defined mylocation inside of GetUserLocation so it also goes out of scope and further kills any chance or your getting an update.

You have not shown where and how the outer mylocation is declared (outside of GetUserLocation), but how ever it is declared, it is being shadowed by the one inside of GetUserLocation. So you aren't getting much.

Here is an example of how you might do it. (The variable thetext is defined within the layout xml and accessed with Kotlin extensions.)

// in the android manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
// allow these through Appliation Manager if necessary

// inside a basic activity
private var locationManager : LocationManager? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)

    // Create persistent LocationManager reference
    locationManager = getSystemService(LOCATION_SERVICE) as LocationManager?

    fab.setOnClickListener { view ->
        try {
            // Request location updates
            locationManager?.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, locationListener)
        } catch(ex: SecurityException) {
            Log.d("myTag", "Security Exception, no location available")
        }
    }
}

//define the listener
private val locationListener: LocationListener = object : LocationListener {
    override fun onLocationChanged(location: Location) {
        thetext.text = ("" + location.longitude + ":" + location.latitude)
    }
    override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
    override fun onProviderEnabled(provider: String) {}
    override fun onProviderDisabled(provider: String) {}
}
🌐
Medium
medium.com › @psarakisnick › android-location-manager-with-kotlin-flows-082c992d1b31
Android location manager with Kotlin flows | by Nick Psarakis | Medium
January 21, 2024 - One of the most common device resources used in mobile applications is the current device location, lets see how we can get it in a clean way using Kotlin flow. ... interface LocationManager { fun listenToLocation(): Flow<Location> fun hasLocationPermission(): Boolean }
🌐
Kotlin Codes
kotlincodes.com › home › blog › locationlistener with kotlin
LocationListener With Kotlin - Kotlin Codes
May 15, 2025 - { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) mLocationRequest = LocationRequest() btnStartupdate = findViewById(R.id.btn_start_upds) btnStopUpdates = findViewById(R.id.btn_stop_upds) txtLat = findViewById(R.id.txtLat); txtLong = findViewById(R.id.txtLong); txtTime = findViewById(R.id.txtTime); val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { buildAlertMessageNoGps() } btnStartupdate.setOnClickListener { if (checkPermissionForLocation(this)) { startLocationUp
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-the-current-gps-location-programmatically-on-android-using-kotlin
How to get the current GPS location programmatically on Android using Kotlin?
November 28, 2020 - import android.Manifest import android.content.Context import android.content.pm.PackageManager import android.location.Location import android.location.LocationListener import android.location.LocationManager import android.os.Bundle import android.widget.Button import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat class MainActivity : AppCompatActivity(), LocationListener { private lateinit var locationManager: LocationManager private lateinit var tvGpsLocat
🌐
Android Developers
stuff.mit.edu › afs › sipb › project › android › docs › training › basics › location › locationmanager.html
Using the Location Manager | Android Developers
LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER); Alternatively, you can provide some input criteria such as accuracy, power requirement, monetary cost, and so on, and let Android decide a closest match location provider. The snippet below asks for a location provider with fine accuracy and no monetary cost.
Find elsewhere
🌐
Medium
medium.com › pickme-engineering-blog › building-a-modern-android-location-manager-from-legacy-approaches-to-clean-architecture-excellence-3e3e4590533e
Building a Modern Android Location Manager: From Legacy Approaches to Clean Architecture Excellence | by Chamod Lakmal | The PickMe Engineering Blog | Medium
September 16, 2025 - My LocationManager project represents a paradigm shift in how we approach location services in Android applications. Built with Kotlin Coroutines, Jetpack Compose, and Clean Architecture principles, it transforms location handling from a necessary ...
🌐
Tealium
docs.tealium.com › platforms › android-kotlin › api › location-manager
LocationManager | API Reference | Tealium Docs
September 5, 2025 - The LocationManager class provides methods for gathering location data, creating and monitoring geofences. The following summarizes the commonly used methods of the LocationManager class for Tealium Kotlin.
🌐
Tealium
docs.tealium.com › platforms › android-kotlin › module-list › location
Location Manager Module | Modules | Tealium Docs
June 13, 2025 - Provides device location data for your events and the ability to add geofences around points of interest.
🌐
GitHub
gist.github.com › gentra › 45cc639d8ff518fa8b4ff86ace6bc79b
Android Location Tracking Service in Kotlin language. Referenced from: http://stackoverflow.com/a/28535885/1441324 · GitHub
June 14, 2018 - Android Location Tracking Service in Kotlin language. Referenced from: http://stackoverflow.com/a/28535885/1441324 ... This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Google
codelabs.developers.google.com › codelabs › while-in-use-location
Receive location updates in Android with Kotlin | Google Codelabs
March 27, 2026 - In this codelab, you learn how to receive location updates and how to support location on any version of Android, particularly Android 10 and 11.
🌐
GitHub
github.com › topics › locationmanager
locationmanager · GitHub Topics · GitHub
android xamarin android-application ... locationmanager gps-coordinates imei-number axml background-service ... Location services using FusedLocation Api and handle location updates as a LiveData using Android architecture components and Jetpack libraries. kotlin-android ...
🌐
Stack Overflow
stackoverflow.com › questions › 71534504 › how-to-use-locationmanager-with-maps-sdk-for-android-simulating-an-uber-app-like
kotlin - How to use locationManager with Maps SDK for Android simulating an uber app like map - Stack Overflow
March 19, 2022 - = null val locationStateFlow = MutableStateFlow(Location(LocationManager.GPS_PROVIDER)) val gpsProviderState = mutableStateOf(false) val isStart: MutableState<Boolean> = mutableStateOf(false) private val locHandlerThread = HandlerThread("LocationUtil Thread") init { locHandlerThread.start() } @SuppressLint("MissingPermission") fun start(minTimeMs: Long = min_time, minDistanceM: Float = min_distance) { locationListener().let { locationListener = it locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTimeMs, minDistanceM, it, locHandlerThread.looper) } gpsProviderState.value
🌐
Medium
medium.com › @boobalaninfo › accessing-users-location-guide-android-2023-60a6f018a718
Accessing User’s Location Guide Android 2023 | by Boobalan Munusamy | Medium
June 22, 2023 - // Create a LocationManager instance val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager // Request location updates from GPS provider locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, locationListener) // Define a LocationListener to handle location updates val locationListener = object : LocationListener { override fun onLocationChanged(location: Location) { // Handle received location updates val latitude = location.latitude val longitude = location.longitude // ...
🌐
Medium
medium.com › @manuaravindpta › getting-current-location-in-kotlin-30b437891781
Getting Current Location in Kotlin | by Manu Aravind | Medium
January 28, 2022 - Getting Location using Fused Location Provider. “Getting Current Location in Kotlin” is published by Manu Aravind.
🌐
Medium
medium.com › @hasperong › get-current-location-with-latitude-and-longtitude-using-kotlin-2ef6c94c7b76
Get current location with latitude and longtitude using kotlin | by Hasper Ong | Medium
January 28, 2022 - Get current location with latitude and longtitude using kotlin This tutorial about get current location with latitude and longtitude using kotlin. 2. Add permission in manifest