I am creating this app and I need to make sure that the marker that the user added is also visible to all other users. For now, only what the individual user entered is shown. So for example if 3 users each put a marker at the end of the map all 3 must be shown. How can i change the code to do this thing? If you need github let me know
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = FragmentsTrovamicoBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());
reference.child("gender").get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
gender = task.getResult().getValue().toString();
}
});
reference.child("address").get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
user_address = task.getResult().getValue().toString();
}
});
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (user_address != null && !user_address.equals("To be added")) {
latLng = getLocationFromAddress(user_address);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("You");
markerOptions.icon(BitmapFromVector(getApplicationContext(), (gender.equals("Male")) ? R.drawable.male : R.drawable.female));
googleMap.clear();
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 5));
googleMap.addMarker(markerOptions);
}
}
public LatLng getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(this);
List<Address> address;
LatLng p1;
try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
p1 = new LatLng(location.getLatitude(), location.getLongitude());
return p1;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private BitmapDescriptor BitmapFromVector(Context context, int vectorResId) {
// below line is use to generate a drawable.
Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorResId);
// below line is use to set bounds to our vector drawable.
vectorDrawable.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight());
// below line is use to create a bitmap for our
// drawable which we have added.
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
// below line is use to add bitmap in our canvas.
Canvas canvas = new Canvas(bitmap);
// below line is use to draw our
// vector drawable in canvas.
vectorDrawable.draw(canvas);
// after generating our bitmap we are returning our bitmap.
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
}
Textbox to put user address
add_address.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final EditText editText = new EditText(requireContext());
alertDialog = new AlertDialog.Builder(requireContext());
alertDialog.setView(editText);
alertDialog.setTitle("Address");
alertDialog.setPositiveButton("Ok", (dialogInterface, i) -> {
String address = editText.getText().toString();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid()).child("address");
reference.setValue(address);
});
alertDialog.setNegativeButton("Cancel", null);
alertDialog.create().show();
//startAutoCompleteIntent();
}
});