From 73f5200c2da0d6a7bb87ecfa08a2bd0b290af5ae Mon Sep 17 00:00:00 2001 From: Jason-Whitmore Date: Fri, 30 May 2025 20:05:33 -0700 Subject: [PATCH] ExploreMapFragment.java: fix removeMarker code to correctly find the specified marker (#6325) Before this change, removeMarker() would search for the correct Overlay by doing a linear search and matching the BaseMarker's Place name with the Overlay's title. This stopped working once the Overlay's title text was changed to be more user friendly. This change implements a more robust solution. A map is used to directly associate BaseMarkers with the matching Overlay. The overlay can now be removed from the overlay list without using any of the information contained within the BaseMarker or Overlay. Co-authored-by: Nicolas Raoul --- .../explore/map/ExploreMapFragment.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java index 194ed5d24..585415562 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java @@ -60,7 +60,9 @@ import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.inject.Inject; import javax.inject.Named; import org.osmdroid.events.MapEventsReceiver; @@ -98,6 +100,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment private GeoPoint mapCenter; private GeoPoint lastMapFocus; IntentFilter intentFilter = new IntentFilter(MapUtils.NETWORK_INTENT_ACTION); + private Map baseMarkerOverlayMap; @Inject LiveDataConverter liveDataConverter; @@ -788,6 +791,11 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment } }, getContext()); + if (this.baseMarkerOverlayMap == null) { + this.baseMarkerOverlayMap = new HashMap<>(); + } + this.baseMarkerOverlayMap.put(nearbyBaseMarker, overlay); + overlay.setFocusItemsOnTap(true); binding.mapView.getOverlays().add(overlay); // Add the overlay to the map } @@ -819,24 +827,22 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment * @param nearbyBaseMarker The NearbyBaseMarker object representing the marker to be removed. */ private void removeMarker(BaseMarker nearbyBaseMarker) { - if (nearbyBaseMarker == null || nearbyBaseMarker.getPlace().getName() == null) { + if (nearbyBaseMarker == null || nearbyBaseMarker.getPlace().getName() == null || + baseMarkerOverlayMap == null || !baseMarkerOverlayMap.containsKey(nearbyBaseMarker)) { return; } - String target = nearbyBaseMarker.getPlace().getName(); + Overlay target = baseMarkerOverlayMap.get(nearbyBaseMarker); List overlays = binding.mapView.getOverlays(); - ItemizedOverlayWithFocus item; for (int i = 0; i < overlays.size(); i++) { - if (overlays.get(i) instanceof ItemizedOverlayWithFocus) { - item = (ItemizedOverlayWithFocus) overlays.get(i); - OverlayItem overlayItem = item.getItem(0); + Overlay overlay = overlays.get(i); - if (overlayItem.getTitle().equals(target)) { - binding.mapView.getOverlays().remove(i); - binding.mapView.invalidate(); - break; - } + if (overlay.equals(target)) { + binding.mapView.getOverlays().remove(i); + binding.mapView.invalidate(); + baseMarkerOverlayMap.remove(nearbyBaseMarker); + break; } } }