在從資料庫中檢索位置資料后,我試圖將 Firebase 中的一些用戶詳細資訊顯示到自定義地圖示記中,但我不知道是否應該實作 FirebaseRecyclerAdapter 或普通的。我該怎么做?
我試圖至少檢索用戶的姓名、緊急情況型別和警報級別,并在單擊標記時顯示在資訊視窗上:
{
"Blocked Users" : {
"RCX2HZXIwlSmMHFgDytf1DgZBgi2" : 0,
"vqP3H4maEfQxCBHEWqsH9q4O1M52" : 0
},
"User Location" : {
"RCX2HZXIwlSmMHFgDytf1DgZBgi2" : {
"latitude" : 15.506605,
"longitude" : 120.86838
},
"vqP3H4maEfQxCBHEWqsH9q4O1M52" : {
"latitude" : 37.3259633,
"longitude" : -121.898475
}
},
"Users" : {
"RCX2HZXIwlSmMHFgDytf1DgZBgi2" : {
"Alert Level" : "High",
"Emergency Type" : "Natural Disaster",
"address" : "Lapaz Tarlac",
"emergencyNum" : "0981232387346",
"name" : "Rafael Campos",
"phoneNum" : "0981233445675"
},
"vqP3H4maEfQxCBHEWqsH9q4O1M52" : {
"Alert Level" : "High",
"Emergency Type" : "Natural Disaster",
"address" : "Lapaz Tarlac",
"emergencyNum" : "9876876650987",
"name" : "Rafael Campos",
"phoneNum" : "089098786876"
}
}
}
這是我的 onMapReady():
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationRef = rootRef.child("User Location");
locationRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
LocationSend send = snapshot.getValue(LocationSend.class);
LatLng location = new LatLng(send.getLatitude(), send.getLongitude());
mMap.addMarker(new MarkerOptions().position(location).title(getCompleteAddress(send.getLatitude(), send.getLongitude())));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 14F));
mMap.setInfoWindowAdapter(new InfoWindowAdapter(Retrieve.this));
Log.d("TAG", "latitude, longitude");
notification();
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
LocationSend send = snapshot.getValue(LocationSend.class);
notification();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
我的配接器:
public class InfoWindowAdapter implements GoogleMap.InfoWindowAdapter{
Context context;
public InfoWindowAdapter(Context context){
this.context = context;
}
@Nullable
@Override
public View getInfoWindow(@NonNull Marker marker) {
View infoView = LayoutInflater.from(context).inflate(R.layout.infowindow, null);
TextView title = infoView.findViewById(R.id.title);
TextView alert = infoView.findViewById(R.id.alert);
TextView type = infoView.findViewById(R.id.type);
title.setText(marker.getTitle());
alert.setText(marker.getSnippet());
type.setText(marker.getSnippet());
return infoView;
}
@Nullable
@Override
public View getInfoContents(@NonNull Marker marker) {
return null;
}
}
uj5u.com熱心網友回復:
根據你最后的評論:
在用戶節點中,名稱、緊急情況型別和警報級別
為了能夠獲取與特定用戶對應的指示欄位的值并將它們添加到谷歌地圖,請使用以下代碼行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userLocationRef = rootRef.child("User Location").child(uid);
userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> userLocationTask) {
if (userLocationTask.isSuccessful()) {
DataSnapshot userLocationSnapshot = userLocationTask.getResult();
double latitude = userLocationSnapshot.child("latitude").getValue(Double.class);
double longitude = userLocationSnapshot.child("longitude").getValue(Double.class);
DatabaseReference uidRef = rootRef.child("Users").child(uid);
uidRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> userTask) {
if (userTask.isSuccessful()) {
DataSnapshot userSnapshot = userTask.getResult();
String name = userSnapshot.child("Name").getValue(String.class);
String emergencyType = userSnapshot.child("Emergency Type").getValue(String.class);
String alertLevel = userSnapshot.child("Alert Level").getValue(String.class);
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(latitude, longitude);
markerOptions.position(latLng);
markerOptions.title(name "/" emergencyType "/" alertLevel);
googleMap.addMarker(markerOptions);
} else {
Log.d("TAG", userTask.getException().getMessage()); //Don't ignore potential errors!
}
}
});
} else {
Log.d("TAG", userLocationTask.getException().getMessage()); //Don't ignore potential errors!
}
}
});
以下代碼的結果將是添加一個與 Google 地圖上的用戶位置相對應的標記。標記的標題將包含名稱、緊急情況型別和警報級別。
這確實適用于單個用戶,如果您需要顯示所有用戶,那么只需使用 getChildren() 方法回圈遍歷 DataSnapshot 物件,如下所示:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userLocationRef = rootRef.child(uid).child("User Location");
userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> userLocationTask) {
if (userLocationTask.isSuccessful()) {
for (DataSnapshot userLocationSnapshot : userLocationTask.getResult().getChildren()) {
double latitude = userLocationSnapshot.child("latitude").getValue(Double.class);
double longitude = userLocationSnapshot.child("longitude").getValue(Double.class);
DatabaseReference usersRef = rootRef.child("Users").child(uid);
usersRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> userTask) {
if (userTask.isSuccessful()) {
for (DataSnapshot userSnapshot : userTask.getResult().getChildren()) {
String name = userSnapshot.child("name").getValue(String.class);
String emergencyType = userSnapshot.child("Emergency Type").getValue(String.class);
String alertLevel = userSnapshot.child("Alert Level").getValue(String.class);
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(latitude, longitude);
markerOptions.position(latLng);
markerOptions.title(name "/" emergencyType "/" alertLevel);
googleMap.addMarker(markerOptions);
}
} else {
Log.d("TAG", userTask.getException().getMessage()); //Don't ignore potential errors!
}
}
});
}
} else {
Log.d("TAG", userLocationTask.getException().getMessage()); //Don't ignore potential errors!
}
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/343810.html
