Search code examples
androidosmdroid

Get all the markers of a cluster in OSMBonusPack


I am creating marker clusters using RadiusMarkerClusterer in android. I want to show the locations of the markers from which a particular marker cluster is made of. I am not getting how to retrieve the marker info after making the clusters. Is it possible to do so? If yes then how?


Solution

  • RadiusMarkerClusterer is quite simple subclass of MarkerClusterer (abstract parent) and it does not provide any information about clustered markers to the caller. But internally the class has this information stored in the instances of StaticCluster

    You can create your own implementation. Look at the source code of RadiusMarkerClusterer and use it as guide or example. Another solution may be to subclass RadiusMarkerClusterer and override method buildClusterMarker. You can add information you need to created marker via setRelatedObjectMethod or you can create an InfoWindow with any information you want.

    For example:

    @Override public Marker buildClusterMarker(StaticCluster cluster, MapView mapView) {
        Marker m = new Marker(mapView);
        m.setPosition(cluster.getPosition());
        m.setInfoWindow(null);
        m.setAnchor(mAnchorU, mAnchorV);
    
        Bitmap finalIcon = Bitmap.createBitmap(mClusterIcon.getWidth(), mClusterIcon.getHeight(), mClusterIcon.getConfig());
        Canvas iconCanvas = new Canvas(finalIcon);
        iconCanvas.drawBitmap(mClusterIcon, 0, 0, null);
        String text = "" + cluster.getSize();
        int textHeight = (int) (mTextPaint.descent() + mTextPaint.ascent());
        iconCanvas.drawText(text,
                mTextAnchorU * finalIcon.getWidth(),
                mTextAnchorV * finalIcon.getHeight() - textHeight / 2,
                mTextPaint);
        m.setIcon(new BitmapDrawable(mapView.getContext().getResources(), finalIcon));
        //beggining of modification
        List<Marker> markersInCluster = new ArrayList<Marker>();
        for (int i = 0; i < cluster.getSize(); i++) {
            markersInCluster.add(cluster.getItem(i))
        }
        m.setRelatedObject(markersInCluster);
        //end of modification
    
        return m;
    
    }