android


Change scale factor of cluster marker to my own attribute


I have an android app with map populated with models having an attribute of sales.
I am able to implement a custom ClusterRenderer to display total sales instead of cluster size on the Cluster icon.
The problem that I have is eg.
Cluster A has 5 ClusterItems with total sales of 1M
Cluster B has 15 ClusterItems with total sales of 100k
Because of the default behavior of clustering, Cluster B is BIGGER than cluster A (because it has more ClusterItems)
Is there a way for me to highjack the Renderer so that the size of the Cluster marker scales with my TOTAL SALES instead of the Cluster size?
Any help will be very much appreciated!
Adding my clusterrenderer code
public class MyClusterRenderer extends DefaultClusterRenderer<Store> {
private final NavigableMap<Long, String> suffixes = new TreeMap<>();
private IconGenerator mClusterIconGenerator;
private Context context;
//for cluster icon
private ShapeDrawable mColoredCircleBackground;
public MyClusterRenderer(Context context, GoogleMap map, ClusterManager<Store> clusterManager) {
super(context, map, clusterManager);
this.context = context;
//for label prefix processing
suffixes.put(1_000L, "k");
suffixes.put(1_000_000L, "M");
suffixes.put(1_000_000_000L, "G");
suffixes.put(1_000_000_000_000L, "T");
suffixes.put(1_000_000_000_000_000L, "P");
suffixes.put(1_000_000_000_000_000_000L, "E");
mClusterIconGenerator = new IconGenerator(context);
//pre processing for cluster icons
mClusterIconGenerator.setContentView(makeSquareTextView(context));
mClusterIconGenerator.setTextAppearance(R.style.amu_ClusterIcon_TextAppearance);
mClusterIconGenerator.setBackground(makeClusterBackground());
}
private SquareTextView makeSquareTextView(Context context) {
SquareTextView squareTextView = new SquareTextView(context);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
squareTextView.setLayoutParams(layoutParams);
squareTextView.setId(R.id.amu_text);
int twelveDpi = 12 * getDensity();
squareTextView.setPadding(twelveDpi, twelveDpi, twelveDpi, twelveDpi);
return squareTextView;
}
#Override
protected void onBeforeClusterItemRendered(Store item, MarkerOptions markerOptions) {
//individual markers here
markerOptions.icon(getIcon(item));
super.onBeforeClusterItemRendered(item, markerOptions);
}
#Override
protected void onBeforeClusterRendered(Cluster<Store> cluster, MarkerOptions markerOptions) {
Bitmap icon = mClusterIconGenerator.makeIcon(getTotalSalesWithFormat((List<Store>) cluster.getItems()));
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(icon);
markerOptions.icon(descriptor);
}
private int getDensity(){
return (int) context.getResources().getDisplayMetrics().density;
}
private LayerDrawable makeClusterBackground() {
mColoredCircleBackground = new ShapeDrawable(new OvalShape());
mColoredCircleBackground.setColorFilter(context.getResources().getColor(R.color.My_green), PorterDuff.Mode.SRC_ATOP);
ShapeDrawable outline = new ShapeDrawable(new OvalShape());
outline.getPaint().setColor(context.getResources().getColor(android.R.color.transparent)); // Transparent white.
LayerDrawable background = new LayerDrawable(new Drawable[]{outline, mColoredCircleBackground});
int strokeWidth = getDensity() * 3;
background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
return background;
}
private String getTotalSalesWithFormat(List<Store> list){
return format(getTotalSales(list));
}
private int getTotalSales(List<Store> list){
int sum = 0;
for(Store s : list){
sum += s.getSales();
}
return sum;
}
private BitmapDescriptor getIcon(Store item){
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.circle);
Bitmap mutable = bmp.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutable);
Paint p = new Paint();
p.setColor(Color.WHITE);
canvas.drawText(String.valueOf(item.getSales()), 0, 50, p);
return BitmapDescriptorFactory.fromBitmap(mutable);
}
private String format(long value) {
//Long.MIN_VALUE == -Long.MIN_VALUE so we need an adjustment here
if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1);
if (value < 0) return "-" + format(-value);
if (value < 1000) return Long.toString(value); //deal with easy case
Map.Entry<Long, String> e = suffixes.floorEntry(value);
Long divideBy = e.getKey();
String suffix = e.getValue();
long truncated = value / (divideBy / 10); //the number part of the output times 10
boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10);
return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix;
}
}
To better illustrate my issue, please find attached image. Notice that 6.3k is BIGGER than 23k (pardon my english)

Related Links

pass array data as parameter volley
Android test recording for third party app and webview support
Back Navigation in Android
increase resolution of image in android programming
can't get gps location
Android: How to get JSON object from Zomato API?
Android deep linking schema: match both http and https
How to re-install production Android app without leaving the beta test?
OpenCV 3.1.0 in android studio (NDK) - updateMotionHistory unresolved
Android Ring Shape with divisions
Firebase loading data for android quiz app
Android soft keyboard hides RecyclerView
How to make android debug mode share the same signature across different machines
Vertical centering imageview in relativelayout
Android Videoview strech not fit in portrait mode
I want to send an message from my Android Watch to my Android Phone. Other way around is working

Categories

HOME
batch-file
google-app-engine
developer-tools
eslint
ng-idle
wakanda
phpmyadmin
formal-languages
ns-3
mysql-workbench
battery
serialization
sh
alsa
nuget-package
google-admin-sdk
jax-ws
crystal-lang
commonjs
mod-security
project-intu
speech-synthesis
alpacajs
aws-sdk
red5
soci
pyramid
boost-icl
asciidoctor-pdf
guzzle
backpack-for-laravel
parcelable
monit
strongname
draft-js-plugins
mount
bulletphysics
android-things
nsurl
magento-1.8
paragraph
kodi
code-snippets
classcastexception
installscript
python-import
fiware-cygnus
hivemq
c9.io
numberpicker
magento-1.9.3
laravel-elixir
wmp
signal-strength
apache-directory
stylecop
pom.xml
mschart
payara-micro
stdmap
azure-cli
context-sensitive-grammar
spring-data-couchbase
renaming
sandcastle
cudd
atlas
pytables
stax
uicolor
installshield-2010
shopping
aurora
phpgrid
highland.js
custom-errors
assertion
amiga
graphics2d
uvc
zend-currency
j-security-check
actionfilterattribute
java-security
gamekit
code-documentation
infosphere-spl
iiop
pseudo-class
webfinger
jama
lambda-architecture
gwt-openlayers
sphinxql
onload-event
asplinkbutton
qi4j
usertype
floating-point-exceptions
role
clrstoredprocedure
inline-formset
nhprof
vs-android
non-clustered-index
echo3
windows-identity
perl5.12
change-management
wcf-proxy
mathematical-typesetting

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App