Skip to content

Commit 4f744c4

Browse files
committed
feat: adding cluster size as title for Markers
1 parent a315e46 commit 4f744c4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

demo/src/main/java/com/google/maps/android/utils/demo/CustomMarkerClusteringDemoActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
import com.google.maps.android.ui.IconGenerator;
4040
import com.google.maps.android.utils.demo.model.Person;
4141

42+
import java.text.NumberFormat;
4243
import java.util.ArrayList;
4344
import java.util.List;
45+
import java.util.Locale;
4446
import java.util.Random;
4547

4648
/**
@@ -109,12 +111,14 @@ protected void onBeforeClusterRendered(@NonNull Cluster<Person> cluster, @NonNul
109111
// Draw multiple people.
110112
// Note: this method runs on the UI thread. Don't spend too much time in here (like in this example).
111113
markerOptions.icon(getClusterIcon(cluster));
114+
markerOptions.title(getLocalizedNumber(cluster.getSize()));
112115
}
113116

114117
@Override
115118
protected void onClusterUpdated(@NonNull Cluster<Person> cluster, Marker marker) {
116119
// Same implementation as onBeforeClusterRendered() (to update cached markers)
117120
marker.setIcon(getClusterIcon(cluster));
121+
marker.setTitle(getLocalizedNumber(cluster.getSize()));
118122
}
119123

120124
/**
@@ -250,4 +254,9 @@ private LatLng position() {
250254
private double random(double min, double max) {
251255
return mRandom.nextDouble() * (max - min) + min;
252256
}
257+
258+
private String getLocalizedNumber(int number) {
259+
NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
260+
return numberFormat.format(number);
261+
}
253262
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@
5656
import com.google.maps.android.ui.IconGenerator;
5757
import com.google.maps.android.ui.SquareTextView;
5858

59+
import java.text.NumberFormat;
5960
import java.util.ArrayList;
6061
import java.util.Collections;
6162
import java.util.HashMap;
6263
import java.util.LinkedList;
6364
import java.util.List;
65+
import java.util.Locale;
6466
import java.util.Map;
6567
import java.util.Queue;
6668
import java.util.Set;
@@ -906,6 +908,7 @@ protected void onClusterItemUpdated(@NonNull T item, @NonNull Marker marker) {
906908
protected void onBeforeClusterRendered(@NonNull Cluster<T> cluster, @NonNull MarkerOptions markerOptions) {
907909
// TODO: consider adding anchor(.5, .5) (Individual markers will overlap more often)
908910
markerOptions.icon(getDescriptorForCluster(cluster));
911+
markerOptions.title(getLocalizedNumber(cluster.getSize()));
909912
}
910913

911914
/**
@@ -960,6 +963,7 @@ protected void onClusterRendered(@NonNull Cluster<T> cluster, @NonNull Marker ma
960963
protected void onClusterUpdated(@NonNull Cluster<T> cluster, @NonNull Marker marker) {
961964
// TODO: consider adding anchor(.5, .5) (Individual markers will overlap more often)
962965
marker.setIcon(getDescriptorForCluster(cluster));
966+
marker.setTitle(getLocalizedNumber(cluster.getSize()));
963967
}
964968

965969
/**
@@ -1176,4 +1180,9 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
11761180
marker.setPosition(position);
11771181
}
11781182
}
1183+
1184+
private String getLocalizedNumber(int number) {
1185+
NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
1186+
return numberFormat.format(number);
1187+
}
11791188
}

0 commit comments

Comments
 (0)