下面顯示的代碼是用于從 Realtime Firebase 檢索多個用戶位置并分配給地圖上的標記的代碼:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference userLocationRef = db.child("User_Location");
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
try {
for (DataSnapshot ds : task.getResult().getChildren()) {
String userID = String.valueOf(ds.child("userID").getValue());
String encryptedLatitude = ds.child("You").child("l").child("0").getValue(String.class);
String encryptedLongitude = ds.child("You").child("l").child("1").getValue(String.class);
Log.d("encryptedLocation", encryptedLatitude ", " encryptedLongitude); //Check the values
Log.d("userid", userID); //Check the values
//decrypt
LocationEncryption locationEncryption = new LocationEncryption();
String decryptedLatitude = null;
String decryptedLongitude = null;
decryptedLatitude = locationEncryption.decrypt(encryptedLatitude);
decryptedLongitude = locationEncryption.decrypt(encryptedLongitude);
Log.d("Decrypted", decryptedLatitude ", " decryptedLongitude); //Check the values
double lat = Double.valueOf(decryptedLatitude);
double lng = Double.valueOf(decryptedLongitude);
//Add location on Google Map
LatLng location = new LatLng(lat, lng);
if (hm.containsKey(userID)) {
hm.get(userID).remove();
}
currentLocationMarker = mMap.addMarker(new MarkerOptions().position(location).title(userID));
currentLocationMarker.showInfoWindow();
hm.put(userID, currentLocationMarker);
}
}
catch (Exception e){
e.printStackTrace();
}
} else {
Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
}
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
實時firebase的結構如下:

代碼結果如下:

當我點擊標記時,它只顯示用戶的用戶 ID。我希望標記顯示用戶名和用戶 ID。但問題是多個用戶的名稱存盤在 Firestore 中。
Firestore 的結構如下:
管理員詳細資訊和用戶詳細資訊存盤在同一個集合“用戶”中。欄位“isAdmin”和“isUser”是區分它們的方法。我只需要用戶名,不需要管理員名稱。

那么,如何從 Firestore 檢索多個用戶的名稱并將名稱分配給地圖上的正確標記。
uj5u.com熱心網友回復:
當我點擊標記時,它只顯示用戶的用戶 ID。我希望標記顯示用戶名和用戶 ID。
您擁有的最佳選擇是將用戶名存盤在用戶 ID 附近。無需創建另一個資料庫呼叫來僅獲取用戶名。因此,您的新資料庫架構應如下所示:
Firebase-root
|
--- User_Location
|
--- $uid
|
--- userID: "Jk8i...1jy2"
|
--- userName: "Zi Zian Yeo" ??
在代碼中應該是這樣的:
String userID = ds.child("userID").getValue(String.class);
String userName = ds.child("userName").getValue(String.class);
并將這些值添加為標記的標題,請使用以下代碼行:
String title = userID "/" userName;
currentLocationMarker = mMap.addMarker(new MarkerOptions().position(location).title(title));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/388286.html
標籤:爪哇 安卓 火力基地 谷歌云平台 谷歌云firestore
