Skip to content Skip to sidebar Skip to footer

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 mar

Solution 1:

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.

publicclassFirebaseMarker {

    publicString dob;
    publicString dod;
    publicString firstname;
    publicString lastname;
    public double latitude;
    public double longitude;


    //required empty constructorpublicFirebaseMarker() {
    }

    publicFirebaseMarker(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;
    }

    publicStringgetDob() {
        return dob;
    }

    publicvoidsetDob(String dob) {
        this.dob = dob;
    }

    publicStringgetDod() {
        return dod;
    }

    publicvoidsetDod(String dod) {
        this.dod = dod;
    }

    publicStringgetFirstname() {
        return firstname;
    }

    publicvoidsetFirstname(String firstname) {
        this.firstname = firstname;
    }

    publicStringgetLastname() {
        return lastname;
    }

    publicvoidsetLastname(String lastname) {
        this.lastname = lastname;
    }

    public double getLongitude() {
        return longitude;
    }

    publicvoidsetLongitude(double longitude) {
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    publicvoidsetLatitude(double latitude) {
        this.latitude = latitude;
    }
}

Whenever you want to save marker info to your database you can do it like this:

DatabaseReferencemProfileRef= FirebaseDatabase.getInstance().getReference("Profile");
FirebaseMarkermarker=newFirebaseMarker("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");

    //....@OverridepublicvoidonMapReady(GoogleMap googleMap){
            googleMap.setOnMarkerClickListener(this);

            LatLng  wollongong = newLatLng(-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 mapaddMarkersToMap(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);
        }

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

        privatevoidaddMarkersToMap(GoogleMap map){

             mChildEventListener = mProfileRef.addChildEventListener(newChildEventListener() {
                 @OverridepublicvoidonChildAdded(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 = newLatLng(latitude,longitude);
                     map.addMarker(newMarkerOptions().position(location).title(firstname,lastname).snippet(dob,dod));
                 }

                 @OverridepublicvoidonChildChanged(DataSnapshot dataSnapshot, String s) {

                 }

                 @OverridepublicvoidonChildRemoved(DataSnapshot dataSnapshot) {

                 }

                 @OverridepublicvoidonChildMoved(DataSnapshot dataSnapshot, String s) {

                 }

                 @OverridepublicvoidonCancelled(DatabaseError databaseError) {

                 }
             });
      }    

Post a Comment for "How To Use Firebase Realtime Database For Android Google Map App?"