hanyu li hanyu li - 2 months ago 75
Android Question

How to use Firebase Realtime Database for Android Google Map app?

I'm trying to work on Firebase

Realtime Dataase
access to my map's Markers which includes
dob
,
dod
,
name
,
latitude
,
longitude
, etc. And I want to use Full name as the title of marker, as well as dob and dod for the snippet of marker.

My problem is don't kown how to get the value of
latitude
,
longitude
as the position of Marker and also
firstname
and
lastname
as the title ...

Here is my
Realtime Dataase


Profile


0

dob: "24/12/1940"

dod: "02/07/2016"

firstname:"Lincon"

lastname:"Hall"

latitude: -34.506081

longitude:150.88104

1

dob: "05/06/1949"

dod: "08/08/2016"

firstname:"Ben"

lastname:"Roberts"

latitude:-34.50609

longitude:150.8811


The code of my map app:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, OnMarkerClickListener {

private Marker myMarker;
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference mProfileRef = firebaseDatabase.getReference();

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

}

@Override
protected void onStart(){
super.onStart();

mProfileRef.child("Profile").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

}

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

for (DataSnapshot child : dataSnapshot.child("0").getChildren()){
String dob = child.child("dob").getValue().toString();
String dod = child.child("dod").getValue().toString();

String latitude = child.child("latitude").getValue().toString();
String longitude = child.child("longitude").getValue().toString();

String firstname = child.child("firstname").getValue().toString();
String lastname = child.child("lastname").getValue().toString();

LatLng location = new LatLng(latitude,longitude);

googleMap.addMarker(new MarkerOptions().position(location).title(firstname,lastname).snippet(dob,dod));
}

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

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

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

}



@Override
public void onMapReady(GoogleMap googleMap) {

googleMap.setOnMarkerClickListener(this);

LatLng wollongong = new LatLng(-34.404336, 150.881632);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(wollongong)
.title("Ben Roberts")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
.snippet("05/06/1949 - 08/08/2016")
.anchor(0.0f,1.0f )

);

LatLng test2 = new LatLng(-34.404464, 150.881892);
googleMap.addMarker(new MarkerOptions()
.position(test2)
.title("Graveyard")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
.snippet("This is a test graveyard too")
.anchor(0.0f,1.0f )
);

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(wollongong, 18));
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

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 boolean onMarkerClick(Marker marker) {

if (marker.equals(myMarker))
{
Intent intent = new Intent(MapsActivity.this,Info_window.class);
startActivity(intent);
}
else
{
Intent intent = new Intent(MapsActivity.this,Info_window.class);
startActivity(intent);
}
return false;
}


}

Can anyone help me with that? Thanks a lot!

The value of Dob and dod is null.

map

Answer

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

                 }
             });
      }