Show Multiple Registered User`s Location On The Same Map (android Studio, Firebase, Kotlin)
Solution 1:
Your current path to location is:
/users/userlocation
Which doesn't seem to be correct, because every User object should have its own location. To achieve that, you can go ahead with one of the following three approaches:
Firebase-root
|
--- users
|
--- $uid
|
--- profileImageUrl: "https://..."
|
--- uid: "h9xZs...bV82"
|
--- username: "Bad Pug"
|
--- location
|
--- latitude: 53.37...
|
--- longitude: -0.90...
Meaning that "location" is a Map containing the latitude and longitude. The second approach might be to add both latitude and longitude directly under the User object:
Firebase-root
|
--- users
|
--- $uid
|
--- profileImageUrl: "https://..."
|
--- uid: "h9xZs...bV82"
|
--- username: "Bad Pug"
|
--- latitude: 53.37...
|
--- longitude: -0.90...
If you consider at some point in time to try using Cloud Firestore, you can use a geographical point, as it's a supported data type. Your Firestore schema might look like this:
Firestore-root
|
--- users (collection)
|
--- $uid (document)
|
--- profileImageUrl: "https://..."
|
--- uid: "h9xZs...bV82"
|
--- username: "Bad Pug"
|
--- geoPoint: [53.37...° N, -0.90...° E]
The key to solving this issue is to add either a Map (for the first solution), two new double properties (for the second solution), or a GeoPoint object (for the third solution), and populate with the corresponding latitude and longitude values. Once you write the User object to the database, you'll have one of the schema from above.
Edit:
In code, to get for example the latitude and longitude in the second example, please use the following lines of code:
val uid = FirebaseAuth.getInstance().currentUser?.uid
val rootRef = FirebaseFirestore.getInstance()
val usersRef = rootRef.collection("users")
val uidRef = usersRef.document(uid)
uidRef.get()
.addOnSuccessListener { document ->
if (document != null) {
val latitude = document.getDouble("latitude")
val longitude = document.getDouble("longitude")
Log.d(TAG, latitude + ", " + longitude)
} else {
Log.d(TAG, "No such document")
}
}
.addOnFailureListener { exception ->
Log.d(TAG, "get failed with ", exception)
}
The result in the logcat will be:
53.37..., -0.90...
Post a Comment for "Show Multiple Registered User`s Location On The Same Map (android Studio, Firebase, Kotlin)"