Skip to content

feat: adding cluster size as title for Markers #1542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
import com.google.maps.android.ui.IconGenerator;
import com.google.maps.android.utils.demo.model.Person;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

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

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

/**
Expand Down Expand Up @@ -250,4 +254,9 @@ private LatLng position() {
private double random(double min, double max) {
return mRandom.nextDouble() * (max - min) + min;
}

private String getLocalizedNumber(int number) {
NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
return numberFormat.format(number);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@
import com.google.maps.android.ui.IconGenerator;
import com.google.maps.android.ui.SquareTextView;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
Expand Down Expand Up @@ -906,6 +908,7 @@ protected void onClusterItemUpdated(@NonNull T item, @NonNull Marker marker) {
protected void onBeforeClusterRendered(@NonNull Cluster<T> cluster, @NonNull MarkerOptions markerOptions) {
// TODO: consider adding anchor(.5, .5) (Individual markers will overlap more often)
markerOptions.icon(getDescriptorForCluster(cluster));
markerOptions.title(getLocalizedNumber(cluster.getSize()));
}

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

/**
Expand Down Expand Up @@ -1176,4 +1180,9 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
marker.setPosition(position);
}
}

private String getLocalizedNumber(int number) {
NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
return numberFormat.format(number);
}
}
Loading