2

I'm adding a few (2-13) markers on the map. The markers do not gets added all at once, but instead the data change is listened from Firebase and if a new user has been added, a marker with his location gets added on the map.

Here's how:

public void addMarkers() {
        mMap.clear();
        vMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(vLat), Double.parseDouble(vLng))).title("Title" + v.trim()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
        markersList.add(venueMarker);
        for (int i=0; i<nP.size(); i++) {
            pMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(cLatP.get(i)), Double.valueOf(cLngP.get(i)))).title(nP.get(i).trim()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
            markersList.add(pMarker);
        }
}

Here's how nP, cLatP and cLngP are getting values assigned to them and also how I'm trying to remove the marker:

    aReference.child(rID).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.getValue() != null) {
                            aReference.child(rID).child(key).addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {

                                    if (dataSnapshot.getValue() != null) {

                                        if (dataSnapshot.hasChild("pName") && dataSnapshot.hasChild("currentLat") && dataSnapshot.hasChild("currentLng")) {
                                            Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();                                             
                                            nP.add(map.get("pName"));
                                            cLatP.add(map.get("currentLat").trim());
                                            cLngP.add(map.get("currentLng").trim());
                                            mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                                                @Override
                                                public void onMapLoaded() {
                                                    addMarkers();
                                                    mMap.getUiSettings().setZoomControlsEnabled(true);
                                                    mMap.getUiSettings().setMapToolbarEnabled(true);
                                                    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                                                    mMap.setMaxZoomPreference(19.0f);
                                                    mMap.setMyLocationEnabled(true);
                                                }
                                            });

                                        }

                                    } 

                                }
                                ...
                            });

                            if (pA != null) {
                                pA.clear();
                            }
                            // below is how I'm trying to remove markers
                            for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                                Map<String, String> map = (Map<String, String>) childSnapshot.getValue();
                                pA.add(map.get("pName"));
                                if (markersList.size() > 0) {
                                    for (int i = 0; i < pA.size(); i++) {
                                        if (!pMarker.getTitle().equals(pA.get(i))) {
                                            pMarker.remove();
                                        }
                                    }
                                } 
                            }

                        }

                    }
                    ...
                });

My logic/algorithm: Down here's I'm trying to fetch the the pName available and trying to match it with available pMarker's title, if it matches then pMarker should not get removed and if there is no string equal to pMarkers title then this marker should get removed.

Here's what data structure looks like:

-app
  -child
    -rID
      -uniqueID1
        -userID1
          -key: value
          -key: value
          -pName: value1
          -currentLat: value1
          -currentLng: value1
      -uniqueID2
        -userID2
          -key: value
          -key: value
          -pName: value2
          -currentLat: value2
          -currentLng: value2
      -uniqueID3
        -userID3
          -key: value
          -key: value
          -pName: value2
          -currentLat: value2
          -currentLng: value2

The problem is that instead of the marker whose title is not equal to any of strings available in ArrayList<String> pA, the marker which was added in latest is getting removed!

So, how to remove the marker whose title is not equal to any of the string available in ArrayList<String> pA?

Please forgive me for not so clear question, but I have described it in the best way I can. I would really appreciate it if you can give me a better and easier way to achieve what I want here.

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
  • In your code, you remove the marker if its title isn't equal to any one of the Strings from pA. That is, to not be removed, it'd need to be equal to all of the strings – Mehmet K Feb 02 '17 at 19:02

1 Answers1

3

You actually need to populate the entire list of titles first, and then iterate over all of the markers.

Then, while iterating over the Markers, check if the title list contains the Marker's title. If not, remove it.

This should work:

// below is how I'm trying to remove markers
// First, populate the title list
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
  Map<String, String> map = (Map<String, String>) childSnapshot.getValue();
  pA.add(map.get("pName"));
}

// Then, iterate through the Markers
// Remove the Markers whose title is not contained in the title list
if (markersList.size() > 0) {
  for (int i = 0; i < markersList.size(); i++) {
    Marker m = markersList.get(i);
    if (!pA.contains(m.getTitle())) {
        m.remove();
    }
  }
} 
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137