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

How to optimize bitmap draw in Android?
how I can get the height of a screen in android?
Sharing only to google plus without photo
Chrome remote debugging gives broken page when accessed over network
connect to self hosted webservice running in Android app in emulator
ThreadSample described in android doc. cancelAll() throws exception
Android: How to stop a handler from a different activity ?
Sending Android push notifications one shot instead of one by one
How to refresh a linkedIn OAuth2 token serverside (or in background proces on device)
How to customize the color of the caret of the expanded-search-action-item?
Upload a picture to ubuntu server through an android application
How to use the OnClick attribute in XML for a button inside a ListView item?
Sharing NavigationView with all the Activities?
Android: pass listView item position to next activity
How can i make an application invisible in recent apps?
alertdialog setItems with custom layout

Categories

HOME
wakanda
amp-html
toolbox
soa
big-o
python-multithreading
azure-container-service
retrofit2
modal-dialog
recordrtc
openstreetmap
reactive-programming
mod-security
angular-dart
favicon
frequency-distribution
shared-libraries
enterprise-architect
plink
symfony-2.8
imessage
google-container-registry
drive
poedit
dpdk
php-mysqlidb
exit
mongoid6
game-theory
onload
fragment-backstack
handler
paperjs
subclass
numpy-broadcasting
xajax
docfx
sql-server-ce
jpad
protein-database
projects-and-solutions
visa
escpos
dbnull
tcc
drupal-theming
clang++
memory-mapping
xbmc
textsum
x-ray
arq
oozie-coordinator
offline.js
cmp
objectscript
restore
stax
autosar
xcode-server
mongodb-php
dbamp
qbxml
dynamics-nav-2016
boomerang
java-money
file-move
ons-api
android-launcher
text-search
sony-lifelog-api
rdflib
airbrake
alloy-ui
teamcity-9.1
zend-currency
delayed-execution
nodelist
cosine-similarity
cbind
trdion2011
codeplex
cancellation
live555
mutual-authentication
terminate
spring-3
scriptaculous
time.h
node-blade
composite-component
httponly
koken
matlab-load
fsevents
winmain
google-floodlight
inline-formset
cdonts
web-garden
blender-2.49
scrubyt
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