Stanley Tutu Dinala Stanley Tutu Dinala - 1 month ago 7
Android Question

How to allow user to create and save marker on Firebase Database

I would like to users to add and save markers on Firebase database.
i am unsure of how to do it, i currently have the user adding the marker and saving it but it does not appear on others users app.

public void onMapLongClick(final LatLng latLng) {
LayoutInflater li = LayoutInflater.from(context);
final View v = li.inflate(R.layout.alert_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(v);
builder.setCancelable(false);

builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText title = (EditText)v.findViewById(R.id.ettittle);
EditText snittle = (EditText)v.findViewById(R.id.etsnittle);

Marker marker0 = mMap.addMarker(new MarkerOptions()
.position(latLng)
.title(title.getText().toString()).snippet(snittle.getText().toString())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.tackshops)));
hash.put(marker0, R.drawable.agriculture);
prefs.edit().putString("Lat",String.valueOf(latLng.latitude)).apply();
prefs.edit().putString("Lng",String.valueOf(latLng.longitude)).apply();

locationCount++;



editor.putString("title"+(locationCount-1), title.getText().toString());
editor.putString("snippet"+(locationCount-1),snittle.getText().toString());




}
});


this is my method for saving the marker on the Firebase Database

private void drawMarker(final LatLng latLng){


myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
Agriculture marker = userSnapshot.getValue(Agriculture.class);

mMap.addMarker(new MarkerOptions().position(latLng))
.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.tackshops));
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
// Failed to read value


}
});

Answer

You should save coordinates of your location (Lat Long) on Firebase. And then you can retrieve and show markers on Map. Firebase Realtime Database this will let you know how to save and retrieve data.

Comments