Android Kotlin:是否可以将LastLocation存储在变量中?

我正在尝试将Android用户的最后一个已知位置用作单独的协程API调用的变量

 private fun getLocation()  {
        lateinit var latLong: String
        val client: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getApplicationContext())
        client.lastLocation
        val location = client.lastLocation
        location.addOnSuccessListener {
            latLong = "${it.latitude},${it.longitude}"
         }
         makeApiCall(latLong)}
}

Is it possible to force a wait for the addOnSuccessListener to ensure the variable is updated accordingly?

评论