在android中获取位置(回调问题)

在android中获取位置(回调问题),android,kotlin,location,Android,Kotlin,Location,我正在尝试获取用户的新位置。 这是我目前掌握的代码: private fun getNewLocation() { Log.d(TAG, "getNewLocation: started") locationRequest = LocationRequest() locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY locationRequest.interval

我正在尝试获取用户的新位置。 这是我目前掌握的代码:

 private fun getNewLocation() {
    Log.d(TAG, "getNewLocation: started")
    locationRequest = LocationRequest()
    locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    locationRequest.interval = 0
    locationRequest.fastestInterval = 0
    locationRequest.numUpdates = 1
    fusedLocationProviderClient.requestLocationUpdates(
        locationRequest,
        locationCallback,
        Looper.myLooper()
    )
    Log.d(TAG, "getNewLocation: finished")
}

private val locationCallback = object : LocationCallback() {
    override fun onLocationResult(locationResult: LocationResult) {
        Log.d(TAG, "locationCallback: onLocationResult started")
        val newLocation = locationResult.lastLocation
        Log.d(TAG, "locationCallback: before setting new data latitude = $latitude, longitude = $longitude")
        // set new location
        latitude = newLocation.latitude.toString()
        longitude = newLocation.longitude.toString()
        locationName = getLocationName(newLocation.latitude, newLocation.longitude)
        Log.d(TAG, "getLastLocation: new location were set latitude = $latitude, longitude = $longitude")
        requestAPI()
    }
}
出于某种原因,当我在物理设备上测试它时,
getNewLocation()
不会调用
onLocationResult
因此,我的日志如下所示:

D/MainActivity: getNewLocation: started
D/MainActivity: getNewLocation: finished
val newLocation = locationResult.locations[0]
而不是:

D/MainActivity: getNewLocation: started
D/MainActivity: getNewLocation: finished
D/MainActivity: locationCallback: onLocationResult started
D/MainActivity: locationCallback: latitude = 64.49273, longitude = 40.596625
我还尝试在
locationCallback
中设置
newLocation
,如下所示:

D/MainActivity: getNewLocation: started
D/MainActivity: getNewLocation: finished
val newLocation = locationResult.locations[0]
但是我得到了相同的结果,因为问题是
getNewLocation()
不调用
onLocationResult

有人能帮我理解为什么会发生这种情况,我该如何解决


谢谢

在请求新位置之前,您应该检查位置设置

private suspend fun checkSettings(locationRequest: LocationRequest) {
    val settingsRequest = LocationSettingsRequest.Builder()
        .addLocationRequest(locationRequest)
        .build()
    try {
        LocationServices
            .getSettingsClient(this)
            .checkLocationSettings(settingsRequest)
            .await()
        // location is turned on
    } catch (e: ResolvableApiException) {
        // location is turned off. Prompt the user to enable it
        e.startResolutionForResult(this, CODE)
    }
}
LocationServices
    .getFusedLocationProviderClient(this)
    .getCurrentLocation(LocationRequest.PRIORITY_HIGH_ACCURACY, null)
此外,如果您使用的是最新版本的play services location,则有一种获取新位置的简单方法

private suspend fun checkSettings(locationRequest: LocationRequest) {
    val settingsRequest = LocationSettingsRequest.Builder()
        .addLocationRequest(locationRequest)
        .build()
    try {
        LocationServices
            .getSettingsClient(this)
            .checkLocationSettings(settingsRequest)
            .await()
        // location is turned on
    } catch (e: ResolvableApiException) {
        // location is turned off. Prompt the user to enable it
        e.startResolutionForResult(this, CODE)
    }
}
LocationServices
    .getFusedLocationProviderClient(this)
    .getCurrentLocation(LocationRequest.PRIORITY_HIGH_ACCURACY, null)