Skip to content

Commit f7b8a56

Browse files
committed
fix: animating or removing marker depending on distance
1 parent 4acec4e commit f7b8a56

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

library/src/main/java/com/google/maps/android/clustering/view/ClusterRendererMultipleItems.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.google.android.gms.maps.model.MarkerOptions;
5454
import com.google.maps.android.R;
5555
import com.google.maps.android.RendererLogger;
56+
import com.google.maps.android.SphericalUtil;
5657
import com.google.maps.android.clustering.Cluster;
5758
import com.google.maps.android.clustering.ClusterItem;
5859
import com.google.maps.android.clustering.ClusterManager;
@@ -438,6 +439,8 @@ private class RenderTask implements Runnable {
438439
private SphericalMercatorProjection mSphericalMercatorProjection;
439440
private float mMapZoom;
440441

442+
private static final double EPSILON = 1e-9;
443+
441444
private RenderTask(Set<? extends Cluster<T>> clusters) {
442445
this.clusters = clusters;
443446
}
@@ -455,6 +458,10 @@ public void setMapZoom(float zoom) {
455458
this.mSphericalMercatorProjection = new SphericalMercatorProjection(256 * Math.pow(2, Math.min(zoom, mZoom)));
456459
}
457460

461+
private boolean areClose(double a, double b) {
462+
return Math.abs(a - b) < EPSILON;
463+
}
464+
458465
@SuppressLint("NewApi")
459466
public void run() {
460467
final MarkerModifier markerModifier = new MarkerModifier();
@@ -532,9 +539,17 @@ public void run() {
532539
final Point point = mSphericalMercatorProjection.toPoint(marker.position);
533540
final Point closest = findClosestCluster(newClustersOnScreen, point);
534541
if (closest != null) {
535-
markerModifier.remove(true, marker.marker);
536-
RendererLogger.d("ClusterRenderer", "Animating then removing marker at position: " + marker.position);
537-
} else if (mClusterMarkerCache.mCache.keySet().iterator().hasNext() && mClusterMarkerCache.mCache.keySet().iterator().next().getItems().contains(marker.clusterItem)) {
542+
LatLng animateTo = mSphericalMercatorProjection.toLatLng(closest);
543+
if (areClose(marker.position.latitude, animateTo.latitude) && areClose(marker.position.longitude, animateTo.longitude)) {
544+
// Treat them as the same, no need for animation
545+
markerModifier.remove(true, marker.marker);
546+
RendererLogger.d("ClusterRenderer", "Removing marker without animation (coordinates are very close)");
547+
} else {
548+
// If they are not close, proceed with the original logic
549+
markerModifier.animateThenRemove(marker, marker.position, animateTo);
550+
}
551+
}
552+
else if (mClusterMarkerCache.mCache.keySet().iterator().hasNext() && mClusterMarkerCache.mCache.keySet().iterator().next().getItems().contains(marker.clusterItem)) {
538553
T foundItem = null;
539554
for (Cluster<T> cluster : mClusterMarkerCache.mCache.keySet()) {
540555
for (T clusterItem : cluster.getItems()) {

0 commit comments

Comments
 (0)