Skip to content

Commit 076118a

Browse files
committed
fix: animating or removing marker depending on distance
1 parent 4f329f5 commit 076118a

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;
@@ -435,6 +436,8 @@ private class RenderTask implements Runnable {
435436
private SphericalMercatorProjection mSphericalMercatorProjection;
436437
private float mMapZoom;
437438

439+
private static final double EPSILON = 1e-9;
440+
438441
private RenderTask(Set<? extends Cluster<T>> clusters) {
439442
this.clusters = clusters;
440443
}
@@ -452,6 +455,10 @@ public void setMapZoom(float zoom) {
452455
this.mSphericalMercatorProjection = new SphericalMercatorProjection(256 * Math.pow(2, Math.min(zoom, mZoom)));
453456
}
454457

458+
private boolean areClose(double a, double b) {
459+
return Math.abs(a - b) < EPSILON;
460+
}
461+
455462
@SuppressLint("NewApi")
456463
@Override
457464
public void run() {
@@ -530,9 +537,17 @@ public void run() {
530537
final Point point = mSphericalMercatorProjection.toPoint(marker.position);
531538
final Point closest = findClosestCluster(newClustersOnScreen, point);
532539
if (closest != null) {
533-
markerModifier.remove(true, marker.marker);
534-
RendererLogger.d("ClusterRenderer", "Animating then removing marker at position: " + marker.position);
535-
} else if (mClusterMarkerCache.mCache.keySet().iterator().hasNext() && mClusterMarkerCache.mCache.keySet().iterator().next().getItems().contains(marker.clusterItem)) {
540+
LatLng animateTo = mSphericalMercatorProjection.toLatLng(closest);
541+
if (areClose(marker.position.latitude, animateTo.latitude) && areClose(marker.position.longitude, animateTo.longitude)) {
542+
// Treat them as the same, no need for animation
543+
markerModifier.remove(true, marker.marker);
544+
RendererLogger.d("ClusterRenderer", "Removing marker without animation (coordinates are very close)");
545+
} else {
546+
// If they are not close, proceed with the original logic
547+
markerModifier.animateThenRemove(marker, marker.position, animateTo);
548+
}
549+
}
550+
else if (mClusterMarkerCache.mCache.keySet().iterator().hasNext() && mClusterMarkerCache.mCache.keySet().iterator().next().getItems().contains(marker.clusterItem)) {
536551
T foundItem = null;
537552
for (Cluster<T> cluster : mClusterMarkerCache.mCache.keySet()) {
538553
for (T clusterItem : cluster.getItems()) {

0 commit comments

Comments
 (0)