如何从Firebase数据库检索数据以显示标记?

时间:2019-05-09 17:28:09

标签: java android firebase google-maps-markers

我正在尝试从Firebase数据库检索纬度和经度,但是这样做很困难。正如您在屏幕快照下看到的那样,每张照片都有自己的ID,这些ID附加在用户ID上。我想在带有标记的地图上显示数据库中的所有位置。附带的是我的代码和数据库的屏幕快照。

数据库屏幕截图:

My Firebase Database

编辑:我根据要求添加了更多代码。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener, GoogleMap.OnMarkerClickListener {
    private GoogleMap mMap;
    private ChildEventListener mChildEventListener;
    private DatabaseReference mUsersDataRef;
    private DatabaseReference mPhotosDataRef;

    private Map<String, User> mUsers = new HashMap<>(); //Map<(String) user-id, (User) user>
    private List<Photo> mPhotos = new ArrayList<>();

    private boolean usersFetched;
    private boolean photosFetched;

    private String photo_id;
    Marker marker;

第2部分


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        FirebaseApp.initializeApp(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        ChildEventListener mChildEventListener;


        mUsersDataRef = FirebaseDatabase.getInstance().getReference("photo_id");
        mPhotosDataRef = FirebaseDatabase.getInstance().getReference("users");
        mUsersDataRef.push().setValue(marker);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        googleMap.setOnMarkerClickListener(this);
        googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        mPhotosDataRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot s : dataSnapshot.getChildren()) {
                    User user = s.getValue(User.class);
                    mUsers.put(user.getUser_id(), user);
                }
                usersFetched = true;
                doneLoading();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        mPhotosDataRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot s : dataSnapshot.getChildren()) {
                    Photo photo = s.getValue(Photo.class);
                    mPhotos.add(photo);
                }
                photosFetched = true;
                doneLoading();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    private void doneLoading() {
        if (usersFetched && photosFetched) {
            for (Photo photo : mPhotos) {
                User thisPhotosUser = mUsers.get(photo.getUser_id());
LatLng location=new LatLng(user.latitude,user.longitude);
                    mMap.addMarker(new MarkerOptions().position(location).title(user.name)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));


            }
        }
    }

    //@Override
    public String getPhoto_id() {
        return photo_id;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用数据快照。可以通过以下方式修改代码:

    if (usersFetched && photosFetched) {
        for (Photo photo : mPhotos) {
            User thisPhotosUser = mUsers.get(photo.getUser_id());

            thisPhotoUser.addListenerForSingleValueEvent(new ValueEvent Listner()){

                public void onDataChange(DataSnapshot datasnapshot){
                    for(Datasnapshot photos : dataSnapshot.getChildren()){

                        if(photos.child(thisPhotoUser.exists())){

                             LatLng location = new LatLng(mPhotosDataRef.child(getPhoto_id(isChild(latitude))),user.longitude);
            mMap.addMarker(newMarkerOptions().position(location).title(photo.name)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));


                        }

                    }            


                }   

            }  

        }
    }