Clustering on Google Map V2 Part-1

9:31 PM , 4 Comments

Introduction: 

I had done with clustering on Google Map V1. After it is deprecated, i was searching for clustering on Google Map V2. And i found one library Android Maps Extensions. It is working great but depends on your implementation how will you use it in your project.

Drawback : This library use google-play-services.jar and has its own implementation of GoogleMap. Hence if there will any change happen in Map v2 API, takes time to implement that changes in this library.

Screenshots:



Getting Started:

  1. Download zip from above link and import library project.
  2. Import this library to project.
    Note:
    No need to import google play service library project as this library contains it.
  3. Get Map key from API Console.

Map Implementation:

  • Activity extends FragmentActivity
  • FragmentManager fm = getSupportFragmentManager();
  • SupportMapFragment f = (SupportMapFragment) fm.findFragmentById(R.id.map); GoogleMap map = f.getExtendedMap();

Cluster Implementatioin:

map.setClustering(new ClusteringSettings().iconDataProvider(new DemoIconProvider(getResources())));


DemoIconProvider.java:


public class DemoIconProvider implements IconDataProvider {

 private static final int[] res = { R.drawable.m1, R.drawable.m2, R.drawable.m3, R.drawable.m4, R.drawable.m5 };

 private static final int[] forCounts = { 10, 100, 1000, 10000, Integer.MAX_VALUE };

 private Bitmap[] baseBitmaps;

 private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
 private Rect bounds = new Rect();

 private MarkerOptions markerOptions = new MarkerOptions().anchor(0.5f, 0.5f);

 public DemoIconProvider(Resources resources) {
  baseBitmaps = new Bitmap[res.length];
  for (int i = 0; i < res.length; i++) {
   baseBitmaps[i] = BitmapFactory.decodeResource(resources, res[i]);
  }
  paint.setColor(Color.WHITE);
  paint.setTextAlign(Align.CENTER);
  paint.setTextSize(resources.getDimension(R.dimen.text_size));
 }

 @Override
 public MarkerOptions getIconData(int markersCount) {
  Bitmap base;
  int i = 0;
  do {
   base = baseBitmaps[i];
  } while (markersCount >= forCounts[i++]);

  Bitmap bitmap = base.copy(Config.ARGB_8888, true);

  String text = String.valueOf(markersCount);
  paint.getTextBounds(text, 0, text.length(), bounds);
  float x = bitmap.getWidth() / 2.0f;
  float y = (bitmap.getHeight() - bounds.height()) / 2.0f - bounds.top;

  Canvas canvas = new Canvas(bitmap);
  canvas.drawText(text, x, y, paint);

  BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap);
  return markerOptions.icon(icon);
 }
}

TO overcome from drawback, I found one library Clustering on Google Map V2 Part-2 .
It is easy to implement and more customizable.

Download Source code : ClusterMap2


Reference Link: Android Maps Extensions

Karn Shah

My name is Karn Shah, I am a software developer living in Ahmedabad,India and my job/hobby is Android. I have been working on the Android platform since 2011 and I hope that sharing what I have learned will help people get proficient in Android faster!

4 comments: