@ Override public boolean onOptionsItemSelected ( MenuItem item ) { super . onOptionsItemSelected ( item ) ; switch ( item . getItemId ( ) ) { case 1234 : if ( clusterer != null ) { break ; } // create clusterer instance clusterer = new ClusterManager ( mapView , getMarkerBitmap ( ) , getZoomLevelMax ( ) , false ) ; // Create a Toast, see e.g. http://www.mkyong.com/android/android-toast-example/ Toast toast = Toast . makeText ( this , "" , Toast . LENGTH_LONG ) ; ClusterManager . setToast ( toast ) ; // this uses the framebuffer position, the mapview position can be out of sync with // what the user sees on the screen if an animation is in progress this . mapView . getModel ( ) . frameBufferModel . addObserver ( clusterer ) ; // add geoitems for clustering for ( int i = 0 ; i < geoItems . length ; i ++ ) { clusterer . addItem ( geoItems [ i ] ) ; } // now redraw the cluster. it will create markers. clusterer . redraw ( ) ; displayItems . setEnabled ( false ) ; displayMoreItems . setEnabled ( true ) ; hideItems . setEnabled ( true ) ; // now you can see items clustered on the map. // zoom in/out to see how icons change. break ; case 5678 : setProgressBarIndeterminateVisibility ( true ) ; Handler myHandler = new Handler ( ) { @ Override public void handleMessage ( Message msg ) { switch ( msg . what ) { case 0 : // calling to this function from other places // The notice call method of doing things addMarker ( ) ; break ; default : break ; } } } ; new ManyDummyContent ( myHandler ) ; item . setEnabled ( false ) ; break ; case 9012 : if ( clusterer != null ) { clusterer . destroyGeoClusterer ( ) ; this . mapView . getModel ( ) . frameBufferModel . removeObserver ( clusterer ) ; clusterer = null ; } displayItems . setEnabled ( true ) ; displayMoreItems . setEnabled ( false ) ; hideItems . setEnabled ( false ) ; break ; } return true ; }
onOptionsItemSelected handler since clustering need MapView to be created and visible this sample do clustering here .