简体   繁体   English

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

[英]How can I retrieve data from my Firebase Database to display markers?

I am trying to retrieve the latitude and longitude from my Firebase Database but I have difficulties doing it. 我正在尝试从Firebase数据库检索纬度和经度,但是这样做很困难。 As you can see under the screenshot, each photos have their own ID which are attached to a user ID. 正如您在屏幕快照下看到的那样,每张照片都有自己的ID,这些ID附加在用户ID上。 I want to display on a map with a marker all the locations from the database. 我想在带有标记的地图上显示数据库中的所有位置。 Attached to this are my code and a screenshot of my database. 附带的是我的代码和数据库的屏幕快照。

Database screenshot: 数据库截图:

我的Firebase数据库

EDIT: I added more of my code as requested. 编辑:我根据要求添加了更多代码。

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;

Part 2 第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;
    }
}

Try using Data Snapshot. 尝试使用数据快照。 The code can be modified in this way: 可以通过以下方式修改代码:

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


                        }

                    }            


                }   

            }  

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM