I\'m trying to work on Firebase Realtime Dataase
access to my map\'s Markers which includes dob
, dod
, name
, latitude
Try the following:
Create a class that you will use to store and retrieve marker data in you database.
In this instance I suggest you create a java object that will be used to easily store and retrieve your marker info. The class will represent any data you want to use to create Google Map marker. You can name the class whatever makes sense to you. I'm going to call it FirebaseMarker for this example.
public class FirebaseMarker {
public String dob;
public String dod;
public String firstname;
public String lastname;
public double latitude;
public double longitude;
//required empty constructor
public FirebaseMarker() {
}
public FirebaseMarker(String firstname, String lastname, double latitude, double longitude, String dob, String dod) {
this.dob = dob;
this.dod = dod;
this.firstname = firstname;
this.lastname = lastname;
this.latitude = latitude;
this.longitude = longitude;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getDod() {
return dod;
}
public void setDod(String dod) {
this.dod = dod;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
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;
}
}
Whenever you want to save marker info to your database you can do it like this:
DatabaseReference mProfileRef = FirebaseDatabase.getInstance().getReference("Profile");
FirebaseMarker marker = new FirebaseMarker("Lincoln", "Hall", -34.506081, 150.88104, "24/12/1940", "02/07/2016" );
mProfileRef.push().setValue(marker);
Move your childEventListener to onMapReady()
Since you want to access your GoogleMap
object in your childEventListener
the safest place add the listener would be in the onMapReady
callback because you can be sure that your GoogleMap
object won't be null.
Move your code for adding markers to the map using data stored in your Firebase Database to the onChildAdded callback of your childEventListener
When you attach a childEventListener
to a location in your database the onChildAdded
callback will be called once for each child at that location and then again each time a new child is added to the location. Right now the code you use to add markers to a map using info stored in your database is placed in the onChildChanged
callback. OnChildChanged
is only called when a child at the location where you have attached a listener is updated (the dataSnapshot passed to that listener is the child which has been changed). Also, I'm going assume you'll use a Java class to store/retrieve your marker info as suggested above.
ChildEventListener mChildEventListener;
DatabaseReference mProfileRef = FirebaseDatabase.getInstance().getReference("Profile");
//....
@Override
public void onMapReady(GoogleMap googleMap){
googleMap.setOnMarkerClickListener(this);
LatLng wollongong = new LatLng(-34.404336, 150.881632);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(wollongong, 18));
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
//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(GoogleMap map){
mChildEventListener = mProfileRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
FirebaseMarker marker = dataSnapshot.getValue(FirebaseMarker.class);
String dob = marker.getDob();
String dod = marker.getDod();
String latitude = marker.getLatitude();
String longitude = marker.getLongitude();
String firstname = marker.getFirstname();
String lastname = marker.getLastname();
LatLng location = new LatLng(latitude,longitude);
map.addMarker(new MarkerOptions().position(location).title(firstname,lastname).snippet(dob,dod));
}
@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) {
}
});
}