Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to change icon of selected marker #17

Open
AleksandrTabolin opened this issue Mar 6, 2018 · 4 comments
Open

Add possibility to change icon of selected marker #17

AleksandrTabolin opened this issue Mar 6, 2018 · 4 comments

Comments

@AleksandrTabolin
Copy link

AleksandrTabolin commented Mar 6, 2018

Hi! Thank you for your work!

In app I work on I need to change icon of marker after clicked on it (make it bigger) . Now it's logic sealed in ClusterRenderer class, and there is no any posobility to change that. Easiest way to do it is add Marker from onMarkerClick method to methods of ClusterManager.Callbacks

@AleksandrTabolin AleksandrTabolin changed the title Add possibility to change icon of selected item Add possibility to change icon of selected marker Mar 6, 2018
@AleksandrTabolin
Copy link
Author

AleksandrTabolin commented Mar 14, 2018

public interface RenderPostProcessor<T extends ClusterItem> {
    boolean postProcess(@NonNull Marker marker, @NonNull Cluster<T> cluster);
}
public class MarkerState {

    private final Marker marker;
    private boolean isDirty;

    public MarkerState(@NonNull Marker marker, boolean isDirty) {
        this.marker = marker;
        this.isDirty = isDirty;
    }

    public MarkerState(@NonNull Marker marker) {
        this(marker, false);
    }

    @NonNull
    public Marker getMarker() {
        return marker;
    }

    public boolean isDirty() {
        return isDirty;
    }

    public void setDirty(boolean dirty) {
        isDirty = dirty;
    } 
}

in ClusterRenderer change

    private final Map<Cluster<T>, Marker> mMarkers = new HashMap<>();

to

    private final Map<Cluster<T>, MarkerState> mMarkers = new HashMap<>();

and append this to ClusterRenderer.render

void render(@NonNull List<Cluster<T>> clusters) {
        ...

        for (Map.Entry<Cluster<T>, MarkerState> item : mMarkers.entrySet()) {
            Cluster<T> cluster = item.getKey();
            MarkerState markerState = item.getValue();

            boolean isPostProcessed = mRenderPostProcessor.postProcess(markerState.getMarker(), cluster);
            if (isPostProcessed) {
                markerState.setDirty(true);
            } else if (markerState.isDirty()) {
                // there should be reset to non-dirty state
                markerState.getMarker().setIcon(getMarkerIcon(cluster));
                markerState.setDirty(false);
            }
        }
    }

and then you can do something like this

setRenderPostProcessor { marker, cluster ->
                        selectedPoint?.let { point ->
                            val needProcess = cluster.latitude == point.latitude && cluster.longitude == cluster.longitude
                            if (needProcess) {
                                val icon = if (cluster.items.size == 1) {
                                    context.bitmapDescriptorFromVector(R.drawable.ic_map_pin_active)
                                } else {
                                    BitmapDescriptorFactory
                                            .fromBitmap(selectedClusterIconGenerator.makeIcon(cluster.items.size.toString()))
                                }
                                marker.setIcon(icon)
                            }
                            needProcess
                        } ?: false
                    }

@charlag
Copy link

charlag commented Mar 28, 2018

@AleksandrTabolin hi! Do you have this code published somewhere as a fork?

@AleksandrTabolin
Copy link
Author

@charlag Hi! Here link to fork.

@ak004
Copy link

ak004 commented May 1, 2022

i dont know if you will reply but i just want to change the color of the cluster marker from red to black and keep the number as it is. i did try to usedefaultIconGenerator.setIconStyle(iconStyle); but i dont knw its not working srry but im new to android and cant figure it out by myself and had to ask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants