Search code examples
androidosmdroid

Too many clusters with osmdroid


I am using osm maps in my app using osmdroid library. I have successfully implemented clustering in osm maps using the below code

private void populateMarkers(final List<Datum> datumList) {
    RadiusMarkerClusterer poiMarkers = new RadiusMarkerClusterer(this);
    map.getOverlays().add(poiMarkers);
    for (int i = 0; i < datumList.size(); i++) {
        Marker marker = new Marker(map);
        marker.setPosition(new GeoPoint(Double.parseDouble(datumList.get(i).getLat()), Double.parseDouble(datumList.get(i).getLng())));
        Drawable currentDraw = ResourcesCompat.getDrawable(getResources(), R.drawable.location_marker, null);

        marker.setIcon(currentDraw);
        final int finalI = i;
        marker.setOnMarkerClickListener(new Marker.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker, MapView mapView) {
                if (marker.isInfoWindowShown()) {
                    InfoWindow.closeAllInfoWindowsOn(mapView);
                } else {
                    getMarkerDetails(marker, datumList.get(finalI).getId());

                }
                return false;
            }
        });

        poiMarkers.add(marker);
        Drawable clusterIconD = getResources().getDrawable(R.drawable.marker_cluster);
        Bitmap clusterIcon = ((BitmapDrawable) clusterIconD).getBitmap();
        poiMarkers.setIcon(clusterIcon);

        map.invalidate();
    }
    progressDialog.dismiss();
}

This works but the problem is that it shows to many small clusters instead of single big cluster for nearby places.See the below image for what I mean

Cluster Image

Also for clustering I am using osmdroid bonus pack dependency


Solution

  • If you look at the source for RadiusMarkerClusterer, you can see there is a setRadius method:

    /** Set the radius of clustering in pixels. Default is 100px. */
    public void setRadius(int radius){
        mRadiusInPixels = radius;
    }
    

    I think all you have to do is set a larger radius to consolidate more of the points into one cluster.