Skip to content Skip to sidebar Skip to footer

Retrieve Location From Firebase And Put Marker On Google Map Api For Android

I am trying to create app to store location on firebase when save button is pressed and retrieve locations from firebase and display all pins in the map. I have been able to save l

Solution 1:

First : you nedd to change the structure to be like this :

DataLocation-Kidp45TdInOM3Bsyu2blatitude:19.772613777905196longitude:-9.92741011083126-KidsmTExZY2KjnS7S-blatitude:18.221073689785065longitude:-6.573890447616577-KidvmAgV0bm2uT_Pcdrlatitude:14.44608051870992longitude:-6.510856859385967

The way to do that is :

        mSaveButton.setOnClickListener(newView.OnClickListener(){
            @OverridepublicvoidonClick(View view){
                //change to refDatabase or mDatabase.child("Location")DatabaseReferencenewPost= refDatabase.push();
                //the push() command is already creating unique key
                newPost.setValue(latlng);

            }
        });

And the code for put marker to the map from your refDatabase is :

@OverridepublicvoidonMapReady(GoogleMap googleMap) {
   mMap = googleMap;

   ...

   refDatabase.addChildEventListener(newChildEventListener() {
     @OverridepublicvoidonChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
          LatLng newLocation = newLatLng(
                        dataSnapshot.child("latitude").getValue(Long.class),
                        dataSnapshot.child("longitude").getValue(Long.class)
                    );
          mMap.addMarker(newMarkerOptions()
              .position(newLocation)
              .title(dataSnapshot.getKey()));
     }

     @OverridepublicvoidonChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}

     @OverridepublicvoidonChildRemoved(DataSnapshot dataSnapshot) {}

     @OverridepublicvoidonChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}

     @OverridepublicvoidonCancelled(DatabaseError databaseError) {}
  });
}

Post a Comment for "Retrieve Location From Firebase And Put Marker On Google Map Api For Android"