Map clustering - max zoom markers still clustered

梦想与她 提交于 2019-12-01 18:57:25

You can extend DefaultClusterRenderer class and set minimum markers to cluster.

public class InfoMarkerRenderer extends DefaultClusterRenderer<MyCustomMarker> {

public InfoMarkerRenderer(Context context, GoogleMap map, ClusterManager<MyCustomMarker> clusterManager) {
    super(context, map, clusterManager);
    //constructor
}

@Override
protected void onBeforeClusterItemRendered(final MyCustomMarker infomarker, MarkerOptions markerOptions) {
      // you can change marker options
}

@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
    return cluster.getSize() > 5; // if markers <=5 then not clustering
}
}

To filter markers that have the same position, you could simply use a hashmasp, whose key is computed from the marker coordinates.

Something like:

Map<String, Marker> uniqueMarkers = new HashMap<String, Marker>();
for (Markers m : allMarkers) {
  // Compute a key to filter duplicates
  // You may need to account for small floating point precision errors by 
  // rounding those coordinates
  String key = m.getLatitude() + "|" + m.getLongitude();

  if (uniqueMarkers.get(key)!=null ) {
    // Skip if we have a marker with the same coordinates
    continue;
  }

  // Add marker and do something with it  
  uniqueMarkers.add(key, m);

  // ...
}
Vijay

Via trail and error I found that if the markers are within ~10 feet (equivalent to 0.0000350º difference in lat or long), the markers don't decluster even at the max zoom level.

One way to solve for this problem is to implement a custom Renderer and let the app decide when to cluster. For example, the below code will cluster only when there are more than 1 marker and not at max Zoom. In other words it will decluster all markers at max zoom.

mClusterManager.setRenderer(new DefaultClusterRenderer<MyItem>(mContext, googleMap, mClusterManager) {
    @Override
    protected boolean shouldRenderAsCluster(Cluster cluster) {
        if(cluster.getSize() > 1 && mCurrentZoom < mMaxZoom) {
            return true;
        } else {
            return false;
        }
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!