【发布时间】:2021-07-29 18:13:18
【问题描述】:
这个问题反映了上一个问题:Show multiple registered user`s location on the same map (Android Studio, Firebase, Kotlin)
我的主要问题是我在 Android Studio 中创建了一个聊天应用程序,并且还使用 Google Api 添加了一个地图活动。我正在使用 Firebase 实时数据库,这就是我的树目前的样子:
我希望“用户位置”出现在每个注册用户的下方,因此所有注册用户的位置都会作为标记出现在我的 Google 地图上。
这是我的 MapsActivity:
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
companion object {
var currentUser: User? = null
val TAG = "MapsActivity"
}
private lateinit var map: GoogleMap
private val LOCATION_PERMISSION_REQUEST = 1
private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var locationRequest: LocationRequest
private lateinit var locationCallback: LocationCallback
private fun getLocationAccess() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
map.isMyLocationEnabled = true
getLocationUpdates()
startLocationUpdates()
}
else
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_REQUEST)
}
private fun getLocationUpdates() {
locationRequest = LocationRequest()
locationRequest.interval = 30000
locationRequest.fastestInterval = 20000
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
if (locationResult.locations.isNotEmpty()) {
val location = locationResult.lastLocation
// val uid = FirebaseAuth.getInstance().currentUser?.uid
// val rootRef = FirebaseFirestore.getInstance()
// val usersRef = rootRef.collection("users")
// val uidRef = uid?.let { usersRef.document(it) }
// if (uidRef != null) {
// uidRef.get()
// .addOnSuccessListener { document ->
// if (document != null) {
// val latitude = document.getDouble("latitude")
// val longitude = document.getDouble("longitude")
// Log.d(TAG, ", " + location.latitude + location.longitude)
// } else {
// Log.d(TAG, "No such document")
// }
// }
// .addOnFailureListener { exception ->
// Log.d(TAG, "get failed with ", exception)
// }
// }
lateinit var databaseRef: DatabaseReference
databaseRef = Firebase.database.reference
val locationlogging = LocationLogging(location.latitude, location.longitude)
databaseRef.child("/userlocation").setValue(locationlogging)
.addOnSuccessListener {
Toast.makeText(applicationContext, "Locations written into the database", Toast.LENGTH_LONG).show()
}
.addOnFailureListener {
Toast.makeText(applicationContext, "Error occured while writing your location to the database", Toast.LENGTH_LONG).show()
}
}
}
}
}
@SuppressLint("MissingPermission")
private fun startLocationUpdates() {
fusedLocationClient.requestLocationUpdates(locationRequest,locationCallback, null)
}
@SuppressLint("MissingPermission")
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
if (requestCode == LOCATION_PERMISSION_REQUEST) {
if (grantResults.contains(PackageManager.PERMISSION_GRANTED)) {
map.isMyLocationEnabled = true
} else { Toast.makeText(this, "User has not granted location access permission", Toast.LENGTH_LONG).show()
finish()
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
}
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
getLocationAccess()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.nav_menu_map, menu)
return super.onCreateOptionsMenu(menu)
}
我注释掉了一些代码,因为我无法正确实现它。当我运行代码时,经纬度出现在我的 Logcat 中,但在我的实时数据库中,它清除了 /users 下的所有数据,并被经纬度替换。
这是我的位置记录:
import com.google.firebase.database.IgnoreExtraProperties
@IgnoreExtraProperties
data class LocationLogging(
var Latitude: Double? = 0.0,
var Longitude: Double? = 0.0
)
我正在寻找一种简单的方法,将坐标放入我的每个注册用户下的 Firebase 实时数据库中,并同时在地图上显示所有位置。
【问题讨论】:
标签: android firebase google-maps kotlin firebase-realtime-database