0

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();
        }
    });
Forin
  • 15
  • 4
  • Get all the users' locations from firebase and then try adding markers for them. – Alias Cartellano Jun 02 '22 at 22:21
  • 1
    Could you edit the code above? So I can be sure I'm not doing something wrong – Forin Jun 03 '22 at 07:35
  • [Retrieve the addresses](https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection) of all users and then reuse the function you made to add markers for each user. – Alias Cartellano Jun 03 '22 at 16:32
  • Can you change the code? so sure i don't miss anything @AliasCartellano – Forin Jun 03 '22 at 16:34
  • I use Realtime Database, the location of address is this: https://imgur.com/a/Foi6P7l – Forin Jun 03 '22 at 16:42
  • `get()` is still used to retrieve the data. Call `get()` on `Users` then on each `User` retrieve their address and make a marker. [Listen to updates](https://stackoverflow.com/questions/47118571/how-to-get-the-list-of-child-nodes-from-firebase-database-reference) and use the key obtained to retrieve the `User` that changed and then get their address then turn it into a marker. – Alias Cartellano Jun 03 '22 at 17:00
  • I don't know how to modify my code to make it work as you said .. Could you please? – Forin Jun 03 '22 at 17:04
  • Try looking at [How to retrieve multiple location which set by using geofire from Realtime Firebasebase and show them on map in android studio](https://stackoverflow.com/questions/70082024/how-to-retrieve-multiple-location-which-set-by-using-geofire-from-real-time-fire). – Alias Cartellano Jun 03 '22 at 17:05
  • Could you try it yourself? so i'm getting confused with all these things – Forin Jun 03 '22 at 17:07
  • I tried to do something but I don't know where to start... have you tried?@AliasCartellano – Forin Jun 03 '22 at 19:24
  • Have you checked?@AliasCartellano – Forin Jun 04 '22 at 11:50
  • I tried but it's getting messy, could you help me?@AliasCartellano – Forin Jun 04 '22 at 16:21
  • Can you edite my codce above? – Forin Jun 04 '22 at 22:27

0 Answers0