How to change marker icon what it was tapped?

后端 未结 1 1984
清酒与你
清酒与你 2021-02-16 00:09

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

相关标签:
1条回答
  • 2021-02-16 00:43

    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.

    0 讨论(0)
提交回复
热议问题