显示数据从Firebase到自定义信息窗口



我试图从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;
}
}

根据你最后的评论:

在"用户"节点中,"名称"、"紧急类型"one_answers"告警级别">

为了能够获得与特定用户对应的指定字段的值并将其添加到Google Map中,请使用以下代码行:

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!
}
}
});

相关内容

  • 没有找到相关文章

最新更新