kotlinparametersgoogle-location-services

Kotlin -FusedLocationClient.requestLocationUpdates LocationRequest Param Error


I'm trying to add a LocationRequest as the first parameter to fusedLocationClient.requestLocationUpdates but I keep getting an error

None of the following function can be called with the arguments supplied.

• requestLocationUpdates(LocationRequest, LocationCallback, Looper?) defined in com.google.android.gms.location.FusedLocationProviderClient

None of the following functions can be called with the arguments supplied:

public abstract fun requestLocationUpdates(p0: LocationRequest, p1: LocationCallback, p2: Looper?): Task<Void!> defined in com.google.android.gms.location.FusedLocationProviderClient

public abstract fun requestLocationUpdates(p0: LocationRequest, p1: LocationListener, p2: Looper?): Task<Void!> defined in com.google.android.gms.location.FusedLocationProviderClient

public abstract fun requestLocationUpdates(p0: LocationRequest, p1: Executor, p2: LocationCallback): Task<Void!> defined in com.google.android.gms.location.FusedLocationProviderClient

public abstract fun requestLocationUpdates(p0: LocationRequest, p1: Executor, p2: LocationListener): Task<Void!> defined in com.google.android.gms.location.FusedLocationProviderClient

As you can see in the picture, LocationCallback and Looper? work fine. I'm using the first suggestion, which LocationRequest is red.

enter image description here

I followed the google doc code for startLocationUpdates(), and that's where the error is located below.

In the Manifest I tried both of these separately:

implementation 'com.google.android.gms:play-services-location:21.0.1'
implementation 'com.google.android.gms:play-services-location:21.0.1@aar'

Code:

import android.content.Context
import android.content.Intent
import android.location.*
import android.location.LocationRequest
import android.os.Build
import android.os.Looper
import androidx.annotation.RequiresApi
import com.google.android.gms.location.*
import java.util.*
import android.provider.Settings

class LocationSupervisor(val context: Context) {

    private var fusedLocationClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context.applicationContext)
    private lateinit var locationRequest: LocationRequest
    private lateinit var locationCallback: LocationCallback

    init {

        fusedLocationClient.lastLocation.addOnSuccessListener { }

        locationRequest = LocationRequest.Builder(60000L).apply {
            this.setQuality(LocationRequest.QUALITY_HIGH_ACCURACY)
        }.build()

        locationCallback = object : LocationCallback() {
            override fun onLocationResult(locationResult: LocationResult) {
                for (location in locationResult.locations) { ... }
            }
        }
    }

    fun startLocationUpdates() {
                                     // Error is on this 1st Param
        fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
    }

    fun stopLocationUpdates() {
        fusedLocationClient.removeLocationUpdates(locationCallback)
    }
}

Solution

  • You have wrong import.

    At first correct the import

    import android.location.LocationRequest ❌

    import com.google.android.gms.location.LocationRequest

    Then use this code for location request:

    locationRequest = LocationRequest.Builder(60000L)
                .setPriority(Priority.PRIORITY_HIGH_ACCURACY)
                .build()
    

    Also, I suggest using my open source library.

    implementation("com.github.GatewayLegends:general-location-service:0.1.4")
    
    

    You can find the General Location Service on GitHub at the following link: https://github.com/GatewayLegends/general-location-service

    I encourage you to explore the repository, review the documentation, and try it out in your projects. I am confident that you will find it to be a valuable asset that saves you time and effort in your location-based operations. put star on Github if you see it useful, and upvote this answer.

    If you find the General Location Service useful, I kindly request that you consider showing your support by starring it on GitHub. Additionally, if you appreciate this suggestion, an upvote would be greatly appreciated. Thank you for your consideration!