Kotlin – FusedLocationClient.requestLocationUpdates 位置请求参数错误

huangapple go评论68阅读模式
英文:

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.

我尝试将LocationRequest作为fusedLocationClient.requestLocationUpdates的第一个参数,但是我一直收到一个错误。

英文:

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.

Kotlin – FusedLocationClient.requestLocationUpdates 位置请求参数错误

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 &#39;com.google.android.gms:play-services-location:21.0.1&#39;
implementation &#39;com.google.android.gms:play-services-location:21.0.1@aar&#39;

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)
    }
}

答案1

得分: 1

你导入错误了。

首先,更正导入:

import android.location.LocationRequest ❌

然后使用以下代码进行位置请求:

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

此外,我建议使用我的开源库。

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

你可以在GitHub上找到General Location Service,链接如下:https://github.com/GatewayLegends/general-location-service

我鼓励你探索这个仓库,查看文档,并在你的项目中尝试使用它。我相信你会发现它是一个有价值的资源,可以节省你在基于位置的操作中的时间和精力。如果你觉得有用,请在GitHub上点星,并点赞这个答案。

如果你觉得General Location Service有用,请考虑在GitHub上给它点星支持。此外,如果你欣赏这个建议,也欢迎点赞。感谢你的考虑!

英文:

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(&quot;com.github.GatewayLegends:general-location-service:0.1.4&quot;)

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!

huangapple
  • 本文由 发表于 2023年5月31日 23:54:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定