You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用LocationRequest.Builder请求位置遇构造函数参数不匹配错误,如何修复?

修复LocationRequest.Builder构造函数调用错误

错误原因

你的代码调用了不存在的LocationRequest.Builder(Priority, Int)构造函数,当前可用的构造函数仅两种:

  • Builder(LocationRequest):基于已有LocationRequest实例创建Builder
  • Builder(Long):仅传入位置更新间隔时间(毫秒,需为Long类型)

同时你定义的locationInterval、locationFastestInterval、locationMaxWaitTime都是Int类型,而相关API要求传入Long类型参数,这也会引发类型不匹配错误。

修复方案

  1. 将时间间隔变量转为Long类型(或传参时显式转换)
  2. 先通过间隔时间创建Builder,再调用setPriority()方法设置精度优先级

修改后的代码示例:

private fun requestLocationUpdates() {
    val locationInterval = 1000L // 直接定义为Long类型
    val locationFastestInterval = 500L
    val locationMaxWaitTime = 500L
    val locationRequest = LocationRequest.Builder(locationInterval)
        .setPriority(Priority.PRIORITY_HIGH_ACCURACY) // 单独设置优先级
        .setWaitForAccurateLocation(false)
        .setMinUpdateIntervalMillis(locationFastestInterval)
        .setMaxUpdateDelayMillis(locationMaxWaitTime)
        .build()
    
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        locationClient.requestLocationUpdates(locationRequest, locationCallback, null)
    }
}

如果不想修改变量类型,也可以在传参时进行类型转换:

val locationInterval = 1000
// ...
val locationRequest = LocationRequest.Builder(locationInterval.toLong())
    .setPriority(Priority.PRIORITY_HIGH_ACCURACY)
    // 其余设置保持不变

内容的提问来源于stack exchange,提问作者Daniel Lip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 23:34:59