Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change marker icon what it was tapped?

In our project we use google maps v2, and I need to find a way to change pin icon after the pin was tapped. Also I need to return initial icon for this pin when another pin will be tapped. So now we have something like this:

public ClusterManager.OnClusterItemClickListener<OurClusterItem> mClusterItemClickListener = new ClusterManager.OnClusterItemClickListener<OurClusterItem>() {

    @Override
    public boolean onClusterItemClick(OurClusterItem item) {
        // Some actions here
        return true;
    }
};

and then we set this listener to map:

ClusterManager mClusterManager = new ClusterManager<OurClusterItem>(getActivity(), getMap());
mClusterManager.setOnClusterItemClickListener(mClusterItemClickListener);
googleMap.setOnMarkerClickListener(mClusterManager);

So, can someone offer solution for this? Thanks!

like image 685
user1774316 Avatar asked Sep 05 '14 10:09

user1774316


People also ask

How do I set a marker icon?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

How do I change the color of a Google Marker?

To edit the marker color, click or tap on the marker icon. When you do that, you can change both the color of the marker and its style. Go for the color or style you want to change and then click OK to see the effect. Click on the button labeled Done to save your new marker color settings.


1 Answers

So, I found solution for it - maybe this also will helps to someone.

So, at first, we need to use our custom renderer (inherited from DefaultClusterRenderer). DefaultClusterRenderer has his own cache that contains pairs of ClusterItem and corresponding Marker:

public MarkerCache<T> mMarkerCache = new MarkerCache<T>();

so I wrote the next method in our CustomClusterRenderer

public Marker getMarker(OurClusterItem clusterItem) {
    return mMarkerCache.get(clusterItem);
}

After it, I've added to our fragment 2 variables:

private Marker mCurrentSelectedMarker;
private ClusterStore mCurrentSelectedClusterItem;

and change implementation for ClusterItemClickListener:

public ClusterManager.OnClusterItemClickListener<OurClusterItem> mClusterItemClickListener = new ClusterManager.OnClusterItemClickListener<OurClusterItem>() {

    @Override
    public boolean onClusterItemClick(ClusterStore item) {
        // return to previous marker non-selected icon
        if (mCurrentSelectedMarker != null) {
            mCurrentSelectedMarker.setIcon(BitmapDescriptorFactory.fromResource(mCurrentSelectedClusterItem.getIconResourceId()));
        }
        Marker marker = mCustomRenderer.getMarker(item);
        if (marker != null) {
            mCurrentSelectedMarker = marker;
            mCurrentSelectedClusterItem = item;
            marker.setIcon(BitmapDescriptorFactory.fromResource(item.getIconSelResourceId()));
        }
        // some other code
        return true;
    }
};

That's all, and it works like a charm.

like image 182
user1774316 Avatar answered Oct 16 '22 18:10

user1774316