[英]how do I display all markers from the firebase database?
how do I display all markers from the firebase database?如何显示 firebase 数据库中的所有标记?
I have come into the database the data in this form:我已经以这种形式进入数据库:
but only what is highlighted in the drawing is displayed: I write the code like this:但只显示图中突出显示的内容:我这样编写代码:
@Override
public void onMapReady(GoogleMap googleMap) {
if (!isOnline() && !isNetworkAvailable()) {
} else {
Map1 = googleMap;
Map1.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Map1.setMapStyle(mapStyleOptions);
Map1.setOnMyLocationButtonClickListener(this);
Map1.setOnMyLocationClickListener(this);
if (Map1 == null) {
Map1.setMyLocationEnabled(true);
}
startCurrentLocationUpdates();
mHandler.postDelayed(new Runnable(){
public void run(){
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Markers");
Map.clear();
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()) {
String latitude = ds.child("latitude").getValue().toString();
String longitude = ds.child("longitude").getValue().toString();
String bus = ds.child("bus_station").getValue().toString();
Log.d("TAG", latitude + ", " + longitude);
LatLng location = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
Map.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.img_4662878)).position(location).title(bus));
}}}}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
mHandler.postDelayed(this, delay);
}
}, delay);
}}
tell me how to display all markers from the database correctly?告诉我如何正确显示数据库中的所有标记?
error:错误:
2020-04-22 23:10:12.031 18972-18972/by.sviter.stop E/AndroidRuntime: FATAL EXCEPTION: main
Process: by.sviter.stop, PID: 18972
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at by.sviter.stoprevizor.tabs.maps.MapsFragment$3$1.onDataChange(MapsFragment.java:337)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@19.2.1:179)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Assuming that dn4O... mgG2
is the uid
of the authenticated user, to get the values of the latitude and longitude properties, please use the following lines of code:假设
dn4O... mgG2
是认证用户的uid
,要获取纬度和经度属性的值,请使用以下代码行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Markers").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String latitude = dataSnapshot.child("latitude").getValue(String.class);
String longitude = dataSnapshot.child("longitude").getValue(String.class);
Log.d("TAG", latitude + ", " + longitude);
LatLng location = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
//Do what you need to do with the location
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logat will be:日志中的结果将是:
53.9148950..., 27.4380507...
Edit:编辑:
According to your comment:根据您的评论:
how to show all the tags?
如何显示所有标签?
Please use the following lines of code:请使用以下代码行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference markersRef = rootRef.child("Markers");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String latitude = ds.child("latitude").getValue(String.class);
String longitude = ds.child("longitude").getValue(String.class);
Log.d("TAG", latitude + ", " + longitude);
LatLng location = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
//Add the location to a list of locations
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
markersRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be: logcat 中的结果将是:
// ...
// ...
53.9148950..., 27.4380507...
After the scope of DataSnapshot for loop has ended you can manipulate the contents of the DataModel object using the getters and setters which you should have implemented.在 DataSnapshot for 循环的 scope 结束后,您可以使用您应该实现的 getter 和 setter 操作 DataModel object 的内容。
If this does not work.如果这不起作用。 Check if you mMarkers1 object is pointing to the correct class, or change the addListenerForSingleValueEvent to addValueEventListener.
检查您的 mMarkers1 object 是否指向正确的 class,或者将 addListenerForSingleValueEvent 更改为 addValueEventListener。
You should you use addValueEventListner instead of addListenerForSingleValueEvent你应该使用 addValueEventListner 而不是 addListenerForSingleValueEvent
You must run a for loop to get all data like...您必须运行一个 for 循环来获取所有数据,例如...
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); rootRef.child("Markers").addValueEventListner(new ValueEventListner{
rootRef.child("Markers").addValueEventListner(new ValueEventListner{
@Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot snap: dataSnapshot.getChildern()){ cast into your model class } } @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot snap: dataSnapshot.getChildern()){ 投射到您的 model class } }
@Override
public void onCancelled(DatabaseError databaseError) {
}
}); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.