I have implemented Google Maps Clustering on the iOS version of my app and it works fine. I am now attempting to implement Clustering on the Android version using the android maps utility library and it is not Declustering/Clustering when I zoom in or out.
Here is the cluster item class:
public class MyClusterItem implements ClusterItem {
private final LatLng position;
private final BitmapDescriptor icon;
private final String tag;
public MyClusterItem(double lat, double lng, BitmapDescriptor icon, String tag) {
this.position = new LatLng(lat, lng);
this.icon = icon;
this.tag = tag;
}
@Override
public LatLng getPosition() {
return position;
}
@Override
public String getTitle() {
return null;
}
@Override
public String getSnippet() {
return null;
}
public BitmapDescriptor getIcon() {
return icon;
}
public String getTag() {
return tag;
}
}
Here is how the cluster manager is implemented:
public void onMapReady(GoogleMap googleMap) {
//Log.i ( TAG, "Google Map Ready Callback" );
mMap = googleMap;
//mMap.setOnMarkerClickListener(this);
// Point the map's listeners at the listeners implemented by the cluster
// manager.
mMap.setOnCameraIdleListener(mClusterManager);
mMap.getUiSettings().setMapToolbarEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
mMap.getUiSettings().setRotateGesturesEnabled(false);
if (ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && isLocationEnabled) {
mMap.setMyLocationEnabled(true);
}
float mCurrentZoom = googleMap.getCameraPosition().zoom;
// Initialize the manager with the context and the map.
// (Activity extends context, so we can pass 'this' in the constructor.)
mClusterManager = new ClusterManager<MyClusterItem>(this, mMap);
mClusterManager.setOnClusterItemClickListener(mClusterItemClickListener);
mMap.setOnMarkerClickListener(mClusterManager);
mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyClusterItem>() {
@Override
public boolean onClusterClick(final Cluster<MyClusterItem> cluster) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
cluster.getPosition(), (float) Math.floor(mMap
.getCameraPosition().zoom + 1)), 300,
null);
return true;
}
});
}
Cluster Items are added from a Firebase database as follows:
markerRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
try {
tableCount = tableCount+1;
markerKey = dataSnapshot.getKey();
markerData = dataSnapshot.getValue(Markers.class);
MyClusterItem markerItem = new PicNickClusterItem(markerData.y, markerData.x, BitmapDescriptorFactory.fromResource(R.drawable.verified), markerKey);
mClusterItemMap.put(markerKey, markerItem);
mClusterManager.addItem(markerItem);
if(tableCount >= numTables) {
//cluster markers
mClusterManager.cluster();
}
} catch (Error error) {
noFirebaseAccessAlert();
}
}
}
Screenshot of the initial map:
Screenshot when zoomed out:
Screenshot when zoomed in:
Cluster Items are not being clustered/declustered when zooming out/in and there is no animation. As I zoom out I would expect the cluster items to move into clusters and as I zoom in Cluster Items to separate from clusters as they do in the iOS version of the app. Also when I tap on a cluster marker, the map zooms in, but the cluster does not expand into cluster items. None of the cluster items have the same lat/lng and are separated by significant distances.
In the iOS version I was able to set the algorithm (non hierarchical distance based) & several "buckets" for clustering (and specify custom icons for each). I need to have both versions looks and respond the same so that the user has the same experience on both iOS and Android devices.