简体   繁体   中英

Having problems reading google maps marker data from Google Firebase Realtime Database

I have a Firebase Realtime database, where I store Google Maps Marker data. It looks like this: Firebase Database

My application has the option to add your own marker to the database, my problem is that my application only reads the info from Studio 1 and T1, not from the random key added by .push(). when i add a marker via the app. Any ideas on how to get it to read the marker info that is under the random key? My code looks as follows:

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback, GoogleMap.OnMarkerClickListener {

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference mProfileRef = firebaseDatabase.getReference("Studios");
ChildEventListener mChildEventListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    Button addAStudio = (Button) findViewById(R.id.addAStudio);
    addAStudio.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MapsActivity.this, Rent.class);
            startActivity(intent);
        }
    });
}

@Override
public void onMapReady(GoogleMap googleMap){
    googleMap.setOnMarkerClickListener(this);
    LatLng  Copenhagen = new LatLng(55.67, 12.56);
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Copenhagen, 18));
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    //get marker info from Firebase Database and add to map
    addMarkersToMap(googleMap);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    googleMap.setMyLocationEnabled(true);
}

@Override
public void onStop(){
    if(mChildEventListener != null)
        mProfileRef.removeEventListener(mChildEventListener);
    super.onStop();
}

private void addMarkersToMap(final GoogleMap map){

    mChildEventListener = mProfileRef.addChildEventListener(new ChildEventListener() {



        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                FirebaseMarker marker = dataSnapshot.getValue(FirebaseMarker.class);
                String StudioName = marker.getStudioName();
                String StudioAdress = marker.getStudioAddress();
                String StudioDescription = marker.getStudioDescription();
                double latitude = marker.getLatitude();
                double longitude = marker.getLongitude();
                LatLng location = new LatLng(latitude, longitude);

                map.addMarker(new MarkerOptions()
                        .position(location)
                        .title(StudioName)
                        .snippet(StudioAdress)
                        .snippet(StudioDescription))
                        .showInfoWindow();

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });
}

@Override
public boolean onMarkerClick(Marker marker) {
    return false;
}

And

public class FirebaseMarker {

public String studioName;
public String studioDescription;
public String studioAddress;
public double latitude;
public double longitude;


//required empty constructor
public FirebaseMarker() {
}

public FirebaseMarker(String studioName, String studioDescription, String studioAdress, double latitude, double longitude) {
    this.studioName = studioName;
    this.studioDescription = studioDescription;
    this.studioAddress = studioAdress;
    this.latitude = latitude;
    this.longitude = longitude;
}

public String getStudioName() {
    return studioName;
}

public void setStudioName(String studioName) {
    this.studioName = studioName;
}

public String getStudioDescription() {
    return studioDescription;
}

public void setStudioDescription(String studioDescription) {
    this.studioDescription = studioDescription;
}

public String getStudioAddress() {
    return studioAddress;
}

public void setStudioAddress(String studioAddress) {
    this.studioAddress = studioAddress;
}

public double getLongitude() {
    return longitude;
}

public void setLongitude(double longitude) {
    this.longitude = longitude;
}

public double getLatitude() {
    return latitude;
}

public void setLatitude(double latitude) {
    this.latitude = latitude;
}

}

and finally where add a new marker to the database:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rent);
    saveStudio = findViewById(R.id.saveStudio);
    studioNameTextField = findViewById(R.id.studioNameTextField);
    studioInfoTextField = findViewById(R.id.studioInfoTextField);
    studioAdressTextField = findViewById(R.id.studioAdressTextField);
    mDatabase = FirebaseDatabase.getInstance();
    mDataBaseRef = mDatabase.getReference("Studios");
    saveStudio = findViewById(R.id.saveStudio);


    saveStudio.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {

                Map<String, FirebaseMarker> newStudioAdd = new HashMap<>();

                newStudioAdd.put(studioNameTextField.getText().toString(),
                        new FirebaseMarker(
                                studioNameTextField.getText().toString(),
                                studioInfoTextField.getText().toString(),
                                studioAdressTextField.getText().toString(),
                                53.669115, 12.560311
                        )
                );

                mDataBaseRef.push().setValue(newStudioAdd);

          Intent intent = new Intent(Rent.this, MapsActivity.class);
          startActivity(intent);

        }
    });

}

as of now the latitude and longitude are hardcoded, as i want it to read the markers before i continue.

This is happening because Studio 1 and T1 have different paths than the T2 . As I see in your database schema, your T2 as an extra child ( -LH-3 ... GbsF ) which is generated by the push() method. In order to display all those objects correctly, you need to have the same paths for all objects. So you can achieve this, either by adding Studio 1 and T1 in the same way, using the push() method or by adding the T2 without using the push() method.

FirebaseMarker firebaseMarker = new FirebaseMarker(
    studioNameTextField.getText().toString(),
    studioInfoTextField.getText().toString(),
    studioAdressTextField.getText().toString(),
    53.669115, 12.560311);
mDataBaseRef.push().setValue(firebaseMarker);

Assuming that the Studios node is a direct child of your Firebase database, here is how you can add the markers on the map:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference studiosRef = rootRef.child("Studios");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            FirebaseMarker marker = dataSnapshot.getValue(FirebaseMarker.class);
            String StudioName = marker.getStudioName();
            String StudioAdress = marker.getStudioAddress();
            String StudioDescription = marker.getStudioDescription();
            double latitude = marker.getLatitude();
            double longitude = marker.getLongitude();
            LatLng location = new LatLng(latitude, longitude);

            map.addMarker(new MarkerOptions()
                    .position(location)
                    .title(StudioName)
                    .snippet(StudioAdress)
                    .snippet(StudioDescription))
                    .showInfoWindow();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
studiosRef.addListenerForSingleValueEvent(valueEventListener);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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