diff --git a/clustering/src/main/java/com/huawei/clustering/Cluster.java b/clustering/src/main/java/com/huawei/clustering/Cluster.java index 9a98e3b..1195427 100644 --- a/clustering/src/main/java/com/huawei/clustering/Cluster.java +++ b/clustering/src/main/java/com/huawei/clustering/Cluster.java @@ -1,5 +1,7 @@ package com.huawei.clustering; +import com.huawei.hms.maps.model.LatLng; + import androidx.annotation.NonNull; import java.util.List; @@ -17,6 +19,8 @@ public class Cluster { private final double south; private final double east; + private final LatLng latLng; + Cluster(double latitude, double longitude, @NonNull List items, double north, double west, double south, double east) { this.latitude = latitude; @@ -26,6 +30,8 @@ public class Cluster { this.west = west; this.south = south; this.east = east; + + this.latLng = new LatLng(latitude, longitude); } /** @@ -46,6 +52,15 @@ public double getLongitude() { return longitude; } + /** + * The LatLng based on latitude and longitude. + * + * @return the latLng of the cluster + */ + public LatLng getLatLng() { + return latLng; + } + /** * The items contained in the cluster. * diff --git a/clustering/src/main/java/com/huawei/clustering/ClusterManager.java b/clustering/src/main/java/com/huawei/clustering/ClusterManager.java index b2e0b48..d95000a 100644 --- a/clustering/src/main/java/com/huawei/clustering/ClusterManager.java +++ b/clustering/src/main/java/com/huawei/clustering/ClusterManager.java @@ -94,6 +94,16 @@ public void setIconGenerator(@NonNull IconGenerator iconGenerator) { mRenderer.setIconGenerator(iconGenerator); } + /** + * Sets a custom render post processor thus replacing the default one. + * + * @param renderPostProcessor the render post processor that's used for modifying the marker being rendered + */ + public void setRenderPostProcessor(@NonNull RenderPostProcessor renderPostProcessor) { + Preconditions.checkNotNull(renderPostProcessor); + mRenderer.setRenderPostProcessor(renderPostProcessor); + } + /** * Sets a callback that's invoked when a cluster or a cluster item is clicked. * diff --git a/clustering/src/main/java/com/huawei/clustering/ClusterRenderer.java b/clustering/src/main/java/com/huawei/clustering/ClusterRenderer.java index 0830998..b71a723 100644 --- a/clustering/src/main/java/com/huawei/clustering/ClusterRenderer.java +++ b/clustering/src/main/java/com/huawei/clustering/ClusterRenderer.java @@ -22,6 +22,8 @@ import java.util.Map; class ClusterRenderer implements HuaweiMap.OnMarkerClickListener { + private static final LatLngTypeEvaluator latLngTypeEvaluator = new LatLngTypeEvaluator(); + private static final FastOutSlowInInterpolator fastOutSlowInInterpolator = new FastOutSlowInInterpolator(); private static final int BACKGROUND_MARKER_Z_INDEX = 0; @@ -52,7 +54,7 @@ public boolean onMarkerClick(Marker marker) { if (markerTag instanceof Cluster) { //noinspection unchecked Cluster cluster = (Cluster) marker.getTag(); - //noinspection ConstantConditions + List clusterItems = cluster.getItems(); if (mCallbacks != null) { @@ -106,8 +108,7 @@ void render(@NonNull List> clusters) { Cluster parentCluster = findParentCluster(mClusters, clusterToRemove.getLatitude(), clusterToRemove.getLongitude()); if (parentCluster != null) { - animateMarkerToLocation(markerToRemove, new LatLng(parentCluster.getLatitude(), - parentCluster.getLongitude()), true); + animateMarkerToLocation(markerToRemove, parentCluster.getLatLng(), true); } else { markerToRemove.remove(); } @@ -123,20 +124,19 @@ void render(@NonNull List> clusters) { String markerTitle = getMarkerTitle(clusterToAdd); String markerSnippet = getMarkerSnippet(clusterToAdd); - Cluster parentCluster = findParentCluster(clustersToRemove, clusterToAdd.getLatitude(), + Cluster parentCluster = findParentCluster(clustersToRemove, clusterToAdd.getLatitude(), clusterToAdd.getLongitude()); if (parentCluster != null) { markerToAdd = mHuaweiMap.addMarker(new MarkerOptions() - .position(new LatLng(parentCluster.getLatitude(), parentCluster.getLongitude())) + .position(parentCluster.getLatLng()) .icon(markerIcon) .title(markerTitle) .snippet(markerSnippet) .zIndex(FOREGROUND_MARKER_Z_INDEX)); - animateMarkerToLocation(markerToAdd, - new LatLng(clusterToAdd.getLatitude(), clusterToAdd.getLongitude()), false); + animateMarkerToLocation(markerToAdd, clusterToAdd.getLatLng(), false); } else { markerToAdd = mHuaweiMap.addMarker(new MarkerOptions() - .position(new LatLng(clusterToAdd.getLatitude(), clusterToAdd.getLongitude())) + .position(clusterToAdd.getLatLng()) .icon(markerIcon) .title(markerTitle) .snippet(markerSnippet) @@ -147,6 +147,8 @@ void render(@NonNull List> clusters) { markerToAdd.setTag(clusterToAdd); mMarkers.put(clusterToAdd, new MarkerState(markerToAdd)); + + mRenderPostProcessor.postProcess(markerToAdd, clusterToAdd); } } @@ -199,8 +201,8 @@ private Cluster findParentCluster(@NonNull List> clusters, private void animateMarkerToLocation(@NonNull final Marker marker, @NonNull LatLng targetLocation, final boolean removeAfter) { ObjectAnimator objectAnimator = ObjectAnimator.ofObject(marker, "position", - new LatLngTypeEvaluator(), targetLocation); - objectAnimator.setInterpolator(new FastOutSlowInInterpolator()); + latLngTypeEvaluator, targetLocation); + objectAnimator.setInterpolator(fastOutSlowInInterpolator); objectAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { diff --git a/clustering/src/main/java/com/huawei/clustering/DefaultRenderPostProcessor.java b/clustering/src/main/java/com/huawei/clustering/DefaultRenderPostProcessor.java index 6c433c2..7ecb886 100644 --- a/clustering/src/main/java/com/huawei/clustering/DefaultRenderPostProcessor.java +++ b/clustering/src/main/java/com/huawei/clustering/DefaultRenderPostProcessor.java @@ -3,10 +3,16 @@ import com.huawei.hms.maps.model.Marker; -public class DefaultRenderPostProcessor implements RenderPostProcessor { +import androidx.annotation.NonNull; +/** + * A default render post processor that does nothing. + * + * @param type of the the cluster item + */ +public class DefaultRenderPostProcessor implements RenderPostProcessor { @Override - public boolean postProcess(Marker marker, Cluster cluster) { - return false; + public void postProcess(@NonNull Marker marker, @NonNull Cluster cluster) { + // do nothing by default } } \ No newline at end of file diff --git a/clustering/src/main/java/com/huawei/clustering/RenderPostProcessor.java b/clustering/src/main/java/com/huawei/clustering/RenderPostProcessor.java index 85e94a7..65fba83 100644 --- a/clustering/src/main/java/com/huawei/clustering/RenderPostProcessor.java +++ b/clustering/src/main/java/com/huawei/clustering/RenderPostProcessor.java @@ -4,7 +4,17 @@ import com.huawei.hms.maps.model.Marker; +/** + * Allows for modifying the marker that is being rendered. + * + * @param type of the cluster item + */ public interface RenderPostProcessor { - - boolean postProcess(@NonNull Marker marker, @NonNull Cluster cluster); + /** + * Allows for modifying the marker that is being rendered. + * + * @param marker the marker + * @param cluster the cluster + */ + void postProcess(@NonNull Marker marker, @NonNull Cluster cluster); } \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 9ada39c..5d48a81 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,3 @@ rootProject.name='ClusterDemo' -include ':singleCluster', 'multipleClusters', ':clusterings' +include ':singleCluster', 'multipleClusters' include ':clustering'