我正在开发一个聊天应用程序,它可以显示附近的人。对于每个用户,我都会保存firestore中最后记录的lat和long。我正在将所有用户加载到一个recyclerview中,并希望一个textview显示登录用户和其他用户之间的距离。我可以加载到文本视图的距离,但距离是所有用户的方式。我为此尝试了许多不同的方法,但都不管用。有什么不对劲的建议吗?这是我的recyclerview和distance方法。
class UserItem(val user: User) : Item<ViewHolder>() {
var thisUser = User()
override fun getLayout(): Int {
return R.layout.user_row
}
override fun bind(viewHolder: ViewHolder, position: Int) {
fetchCurrentUser()
viewHolder.itemView.tv_user_name.text = "${user.userName}, ${user.userAge}"
viewHolder.itemView.tv_user_about.text = "\"${user.userInterest}\""
viewHolder.itemView.tv_distance_from.text = getDistance(thisUser.lastLocationLat, thisUser.lastLocationLong,
user.lastLocationLat, user.lastLocationLong)
Picasso.get().load(user.userImageUrl).into(viewHolder.itemView.iv_user_photo_toolbar)
}
private fun fetchCurrentUser() {
val uid = FirebaseAuth.getInstance().currentUser!!.uid
val db = FirebaseFirestore.getInstance().collection("users").document(uid)
db.get()
.addOnSuccessListener { documentSnapshot ->
if (documentSnapshot != null) {
thisUser = documentSnapshot.toObject(User::class.java)!!
}
}
}
private fun getDistance(latOne: Double, longOne: Double, latTwo: Double, longTwo: Double): kotlin.String {
val mallLoc = Location("")
mallLoc.latitude = longOne
mallLoc.longitude = latOne
val userLoc = Location("")
userLoc.latitude = longTwo
userLoc.longitude = latTwo
val distance = mallLoc.distanceTo(userLoc) / 1000
return String.format(Locale.getDefault(), "%.2f", distance)
.toString() + " km Away"
}
}
Thankful for any help.
暂无答案!
目前还没有任何答案,快来回答吧!