0
private final HashMap<String, Marker> hm=new HashMap<String, Marker>();
 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.getUiSettings().setZoomControlsEnabled(true);


    DatabaseReference db = FirebaseDatabase.getInstance().getReference();
    DatabaseReference userLocationRef = db.child("UserLocation");

    db.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DataSnapshot> task) {
                    if (task.isSuccessful()) {


                        for (DataSnapshot ds : task.getResult().getChildren()) {
                            String userID = String.valueOf(ds.child("userID").getValue());

                            double lat = ds.child("You").child("l").child("0").getValue(Double.class);
                            double lng = ds.child("You").child("l").child("1").getValue(Double.class);
                            Log.d("TAG", lat + ", " + lng); //Check the values
                            Log.d("TAG", userID+"hi"); //Check the values

                            //Add location on Google Map
                            LatLng location = new LatLng(lat, lng);


                            if (hm.containsKey(userID)) {
                                hm.get(userID).remove();
                            }

                            currentLocationMarker = mMap.addMarker(new MarkerOptions().position(location).title(userID));
                            currentLocationMarker.showInfoWindow();
                            hm.put(userID, currentLocationMarker);







                        }
                    } else {
                        Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
                    }
                }
            });

        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });}

The above code is used to retrieve multiple users' real-time locations from a real-time firebase. The structure of the real-time firebase is shown below:

structure of firebase

When the location of the multiple user's changes, the markers start to update the location of the users, but the problem is when the markers begin to update the new location of users, the old markers of the users were not removed.

The problem happened as the image shown below: marker problem

So, the problem is how to remove the old markers of multiple markers, and the map can only show one marker for each user? Thanks if you can help!

  • 1
    Try saving your `markers` returned by `addMarker` in a `map` object(I suggest `hashmap`) and using `marker.remove` to remove duplicate markers. Source: [Remove old markers on Google Map](https://stackoverflow.com/a/13692845/16653700)- Antony – Alias Cartellano Nov 25 '21 at 19:41
  • If you use a `hashmap` you can keep track if which user already has a marker and remove previous markers using a conditional with`.contains` or another implementation. – Alias Cartellano Nov 25 '21 at 19:42
  • Have you tried Alias Cartellano's suggestion? Does it work? – Alex Mamo Nov 26 '21 at 08:05
  • I still trying on it!!! – Zi Zian Yeo Nov 26 '21 at 15:01
  • Now i can remove the old marker of the user from (Source: Remove old markers) on Map. But, I m facing some problems when saving the markers in the hashmap! I can only save the marker as Marker currentLocationMarker = mMap.addMarker(new MarkerOptions().position(location).title(lat + ", " + lng)). So, the map will only show and update one user's real-time location. – Zi Zian Yeo Nov 27 '21 at 12:20
  • Can you add your updated code to the question? – Alias Cartellano Nov 27 '21 at 19:56
  • I suggest you save markers in the hashmap by using g as a key(or a userid if it isn't the userid) and you can then determine which user's marker to update. – Alias Cartellano Nov 27 '21 at 19:58
  • I have updated the code! Hope you can help me with the problem of hashmap. Thanks – Zi Zian Yeo Nov 29 '21 at 14:00
  • 1
    Declare `private HashMap hm=new HashMap()<>`, get g as a string with `String g=ds.child("you").child("g")`(add `getValue`), check `if(hm.contains(g))`(replace your original if)then `hm.get(g).remove()` in the if to remove the marker if it is. Finally, create the marker as usual and put it in the hashmap using `hm.put(g, marker)`. – Alias Cartellano Nov 29 '21 at 14:47
  • Thanks a lot, the multiple user's locations can be stored in hashmap and shown in the map in real-time for now. But the same problem happened here which is the old markers of multiple users still cannot be removed. I have updated the code above. Hope you can take a look and help. Thanks – Zi Zian Yeo Nov 30 '21 at 07:56
  • I have stored the marker by using userID instead of g because the g will be changed once the user is moving. – Zi Zian Yeo Nov 30 '21 at 07:57
  • The if(hm.contains(g)) and hm.get(g).remove() cannot function to remove the old markers of multiple users! – Zi Zian Yeo Nov 30 '21 at 07:58
  • 1
    What happens when you use `hm.get(userid).remove()` instead of `hm.remove(userID)`?. – Alias Cartellano Nov 30 '21 at 09:24
  • 1
    It works!!! Thank you so much! – Zi Zian Yeo Nov 30 '21 at 18:22

1 Answers1

0

You are trying to store and retrieve user's each location information (lat, long) in the database. But I think for the display on the map, you may want to show only the user's latest or current location. This way you will only have at max one marker per user in the map. When the user's location changes you can either update that same marker or delete the existing one and add the new. For this you can check how @alias suggested in the above comments to your question. To identify the latest location per user, you can store the date and time with each record. So that you can query the database accordingly.

Dhananjay M
  • 1,851
  • 22
  • 18
  • By using the method suggested by @alias, the old marker can be removed now. Can you explain more about how to store the date and time with each record? So that I can query the database accordingly and show multiple markers (location of users) on map. – Zi Zian Yeo Nov 27 '21 at 12:21