Clustering on Google Map V2 Part-2

12:03 PM , 5 Comments

We have gone through Clustering on Google map V2 in Clustering on Google Map V2 Part-1 .To overcome drawback of it , I searched for few days and i found one more library which independent from google play service library.

Screenshots :


Features :

  • Clustering based on pixel proximity, not grid membership
  • Animated cluster transitions
  • Supports Android v2.2 (Froyo) and higher

Getting started : 

  • It's easy to add Clusterkraf to your app. Add the Clusterkraf library folder as an Android project to your Eclipse/ADT workspace, and reference it from your app as a library project.
  • Clusterkraf provides an InputPoint class which holds a LatLng position and an Object tag. You just need to construct an ArrayList<InputPoint> object based on your model objects.


    public class YourActivity extends FragmentActivity {
        YourMapPointModel[] yourMapPointModels = new YourMapPointModel[] { new YourMapPointModel(new LatLng(0d, 1d) /* etc */ ) };
        ArrayList inputPoints;
    
        private void buildInputPoints() {
            this.inputPoints = new ArrayList(yourMapPointModels.length);
            for (YourMapPointModel model : this.yourMapPointModels) {
                this.inputPoints.add(new InputPoint(model.latLng, model));
            }
        }
    }
    
  • When your GoogleMap is initialized and your ArrayList<InputPoint> is built, you can then initialize Clusterkraf.
    // YourActivity
    
        Clusterkraf clusterkraf;
    
        private void initClusterkraf() {
            if (this.map != null && this.inputPoints != null && this.inputPoints.size() > 0) {
                com.twotoasters.clusterkraf.Options options = new com.twotoasters.clusterkraf.Options();
                // customize the options before you construct a Clusterkraf instance
                this.clusterkraf = new Clusterkraf(this.map, this.options, this.inputPoints);
            }
        }
    


Advance Options :

  1. To set any type of interpolator in cluster animation :
    options.setTransitionInterpolator(interpolator); 
    /* Types : AccelerateDecelerateInterpolator, AccelerateInterpolator, AnticipateInterpolator,
    AnticipateOvershootInterpolator, BounceInterpolator, DecelerateInterpolator,
    LinearInterpolator, OvershootInterpolator */
    
  2. Clusterkraf calculates whether InputPoint objects should join a cluster based on their pixel proximity. If you want to offer your app on devices with different screen densities, you should identify a Device Independent Pixel measurement and convert it to pixels based on the device's screen density at runtime.
    options.setPixelDistanceToJoinCluster(dipDistanceToJoinCluster);
    
  3. To set zoom to bound animation duration :

    options.setZoomToBoundsAnimationDuration(zoomToBoundsAnimationDuration);
    
  4. To set showInfoWindow animation duration :

    options.setShowInfoWindowAnimationDuration(showInfoWindowAnimationDuration);
    
  5. To set behavior of marker click :

    options.setSinglePointClickBehavior(singlePointClickBehavior);
    /* select from two types SHOW_INFO_WINDOW, NO_OP. */
    
  6. To set behavior of cluster click :

    options.setClusterClickBehavior(this.options.clusterClickBehavior);
    /* select from three types ZOOM_TO_BOUNDS, SHOW_INFO_WINDOW, NO_OP. */
    
  7. To set behavior of cluster info window click :

    options.setClusterInfoWindowClickBehavior(this.options.clusterInfoWindowClickBehavior);
    /* select from three types ZOOM_TO_BOUNDS, HIDE_INFO_WINDOW, NO_OP. */
    
  8. To set padding from window sides after zooming :

    options.setZoomToBoundsPadding(pixels);
    /* When zooming to the bounds of a marker's backing ClusterPoint, zoom until
     * all of the points are at least this far from the edge of the GoogleMap's
     * bounds
     */
    
  9. To set marker click listner :

    options.setOnMarkerClickDownstreamListener(new OnMarkerClickDownstreamListener(new OnMarkerClickDownstreamListener() {
       
       @Override
       public boolean onMarkerClick(Marker marker, ClusterPoint clusterPoint) {
        // TODO Auto-generated method stub
        return false;
       }
      }));
    
  10. To get methods of starting and finishing clustering :

    options.setProcessingListener(this);
    /* generate two methods */
    @Override
     public void onClusteringStarted() {
      
     }
    
     @Override
     public void onClusteringFinished() {
     }

Download Source code : ClusterOnGoogleMapV2


Reference Link: Clusterkraf

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!

5 comments: