Try using LocationRequest.Builder

Below code for creating location request

Kotlin

LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
            .apply {
                setWaitForAccurateLocation(false)
                setMinUpdateIntervalMillis(IMPLICIT_MIN_UPDATE_INTERVAL)
                setMaxUpdateDelayMillis(100000)
            }.build()

Java

new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
    .setWaitForAccurateLocation(false)
    .setMinUpdateIntervalMillis(IMPLICIT_MIN_UPDATE_INTERVAL)
    .setMaxUpdateDelayMillis(100000)
    .build()

Read more about here https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.Builder

Answer from yoman796 on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › android.locations.locationrequest.builder.setminupdateintervalmillis
LocationRequest.Builder.SetMinUpdateIntervalMillis(Int64) Method (Android.Locations) | Microsoft Learn
[<Android.Runtime.Register("setMinUpdateIntervalMillis", "(J)Landroid/location/LocationRequest$Builder;", "", ApiSince=31)>] member this.SetMinUpdateIntervalMillis : int64 -> Android.Locations.LocationRequest.Builder
🌐
Google
developers.google.com › google play services › locationrequest
LocationRequest | Google Play services | Google for Developers
October 31, 2024 - Clients should consider using LocationRequest.Builder.setMinUpdateIntervalMillis(long) to limit the fastest rate at which their location callbacks may be invoked (and thus limit the amount of power usage they may be blamed for).
🌐
Stack Overflow
stackoverflow.com › questions › 79085950 › get-location-updates-based-on-time-interval-or-min-distance-in-androidwhichever
google maps - Get location updates based on time interval or min distance in Android(whichever comes first) - Stack Overflow
October 14, 2024 - private const val LOCATION_UPDATE_INTERVAL = 10000L private fun createRequest(): LocationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, LOCATION_UPDATE_INTERVAL) .apply { setGranularity(Granularity.GRANULARITY_PERMISSION_LEVEL) setMinUpdateIntervalMillis(LOCATION_UPDATE_INTERVAL) setWaitForAccurateLocation(true) }.build()
🌐
Stack Overflow
stackoverflow.com › questions › 78779001 › gps-updates-satellites-in-background-fusedlocationprovider-vs-locationmanage
GPS updates (satellites) in background - FusedLocationProvider vs LocationManager, Device Power Saving vs App's Battery Unrestricted, Android version - Stack Overflow
July 22, 2024 - private val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context) fusedLocationClient.requestLocationUpdates( /* p0 = */ getLocationRequest(), /* p1 = */ this, /* p2 = */ looper ) private fun getLocationRequest(): LocationRequest { return LocationRequest.Builder(250L).apply { setMinUpdateIntervalMillis(100L) setPriority(Priority.PRIORITY_HIGH_ACCURACY) }.build() }
🌐
GitHub
github.com › ionic-team › capacitor-plugins › issues › 2062
[Geolocation / Android] The GPS position is only updated every 5 seconds when using "watchPosition" function on Android · Issue #2062 · ionic-team/capacitor-plugins
March 20, 2024 - LocationRequest locationRequest = new LocationRequest.Builder(10000) .setMaxUpdateDelayMillis(timeout) .setMinUpdateIntervalMillis(5000) .setPriority(priority) .build();
Author   ionic-team
Find elsewhere
🌐
Chegg
chegg.com › engineering › computer science › computer science questions and answers › i’m making a program in android studio with kotlin, where i need to get the user’s updated location. according to the documentation, this is how one should make a locationrequest:val locationrequest =
Solved I’m making a program in Android Studio with Kotlin, | Chegg.com
March 13, 2024 - .setMinUpdateIntervalMillis · ( 1 · 0 · 0 · )  · .build · (  · When I change the emulator · ’s location in Android Studio, the location · (which is displayed on the app screen using composable functions · ) is not updated. This only happens if the user chooses ·
🌐
Stack Overflow
stackoverflow.com › questions › 76772066 › android-onlocationchanged-not-waiting-for-best-gpx-fix
geolocation - Android onLocationChanged not waiting for best GPX fix - Stack Overflow
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); LocationRequest locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, UPDATE_TIME) .setWaitForAccurateLocation(true) .setMinUpdateIntervalMillis(LocationRequest.Builder.IMPLICIT_MIN_UPDATE_INTERVAL) .setMaxUpdateDelayMillis(1000) .build(); locationCallback = new LocationCallback() { @Override public void onLocationResult(@NonNull LocationResult locationResult) { List <Location> locations = locationResult.getLocations(); Log.i("*****", String.format("%d locations",locations.size())); for (Loca
🌐
Stack Overflow
stackoverflow.com › questions › 78475666 › keep-gps-location-alive-w-device-owner-app
android - Keep GPS Location Alive w/ Device Owner App - Stack Overflow
May 14, 2024 - LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, IntervalManager.FIVE_SECONDS) .setIntervalMillis(interval) .setMinUpdateIntervalMillis(interval) .setMaxUpdateDelayMillis(interval) .build()
🌐
Stack Overflow
stackoverflow.com › questions › 79159709 › how-to-launch-location-resolution-through-launcher-and-get-result-in-composable
android - How to Launch Location Resolution Through Launcher and get Result in Composable Screen not Activity (Location Service GPS On) - Stack Overflow
val LOCATION_SETTINGS_REQUEST = 245 fun enableLoc(context: Context, locationSettingsLauncher: ActivityResultLauncher<Intent>) { val activity = context.findActivity() // Create a LocationRequest object val locationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 30 * 1000L) .setWaitForAccurateLocation(false) .setMinUpdateIntervalMillis(5 * 1000L) .setMaxUpdateDelayMillis(100) .build() // Build LocationSettingsRequest val builder = LocationSettingsRequest.Builder().apply { addLocationRequest(locationRequest) setAlwaysShow(true) // Always show the prompt if location is not enabl
Top answer
1 of 1
1

I haven't used location services in a long, long time, so I'm just looking at the documentation for 'LocationRequest.Builder` and guessing at your equivalent code because it looks self-explanatory. Builders are a common pattern, used more often in Java-based APIs like this one than they are used in pure-Kotlin APIs. You can look up "java builder pattern" to read about it.

private fun NewLocation() { 
    val locationRequest = LocationRequest.Builder()
        .setPriority(Priority.PRIORITY_HIGH_ACCURACY)
        .setIntervalMillis(0L)
        .setMinUpdateIntervalMillis(0L)
        .setMaxUpdates(1)
        .build()
    mfusedlocation = LocationServices.getFusedLocationProviderClient(this)
    mfusedlocation.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper())
}

FYI since you're working on a portfolio:

  • Function names in Kotlin start with a verb and lower-case letter by convention. Or if it returns a modified copy of an object, you can use a past participle instead of a phrase starting with a verb. For example, I would rename NewLocation() to something like beginLocationRequest().

  • The mSomething pattern of naming variables (putting abbreviations in front of variable names) is called Hungarian notation. m stands for "member", but Kotlin properties are not even called member variables. I've never seen Hungarian notation used in Kotlin before, and it is rarely used in Java. It is widely regarded as making code less readable, especially with modern IDEs. I would advise against using it in a portfolio project as it is more likely to damage the impression you want to give than it is to help, especially if you are using it inconsistently.

🌐
Stack Overflow
stackoverflow.com › questions › 79179071 › location-foreground-service-using-fusedlocationproviderclient-only-collects-data
android - Location Foreground Service using FusedLocationProviderClient only collects data one time when location permission option "Ask every time" is selected - Stack Overflow
= null private lateinit var locationCallback: LocationCallback ... locationCallback = object : LocationCallback() { override fun onLocationResult(locRes: LocationResult) { // I perform the permission check here, and it still says granted, even though it's acting as if it is not } } val locationRequest = LocationRequest.Builder(60000) // 1 minute .setPriority(HIGH_ACCURACY) .setMinUpdateIntervalMillis(60000) // 1 minute .build() val locationBuilder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest) val settingsResult = LocationServices.getSettingsClient(appContext).checkLocationSettings(locationBuilder.build()) settingsResult.addOnCompleteListener { locationSettingsTask -> fusedLocationClient!!.requestLocationUpdates(locationRequest, locationCallback, null) }
🌐
Stack Overflow
stackoverflow.com › questions › 79613030 › fused-location-client-location-turns-stale-provider-repeating-coordinates-coo
android - Fused Location Client: Location turns stale, provider repeating coordinates, coordinates get stuck - Stack Overflow
val locationRequest = LocationRequest.Builder(10000L) // Interval in milliseconds .setPriority(Priority.PRIORITY_HIGH_ACCURACY) .setIntervalMillis(1000L) .setMinUpdateIntervalMillis(1000L) .build()