how to add google map marker from firebase database

后端 未结 1 1443
遇见更好的自我
遇见更好的自我 2021-01-07 15:56

I want to retrieve location markers from firebase database but I can\'t see any markers after running this code. I tried to make EventListener but the map runs

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-07 16:26

    You need to use a ValueEventListener to obtain data from the Firebase database. This is explained in the documentation.

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    double location_left = dataSnapshot.child("location_left").getValue(Double.class);
                    double location_right = dataSnapshot.child("location_right").getValue(Double.class);
                    LatLng sydney = new LatLng(location_left, location_right);
                    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,18));
                } else {
                    Log.e(TAG, "onDataChange: No data");
                }
    
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException();
            }
        });
    

    0 讨论(0)
提交回复
热议问题