在我们的项目中,我们使用谷歌地图v2,我需要找到一种方法来点击引脚后更改图钉图标.此外,当需要点击另一个引脚时,我需要返回此引脚的初始图标.
所以现在我们有这样的事情:
所以现在我们有这样的事情:
public ClusterManager.OnClusterItemClickListener<OurClusterItem> mClusterItemClickListener = new ClusterManager.OnClusterItemClickListener<OurClusterItem>() { @Override public boolean onClusterItemClick(OurClusterItem item) { // Some actions here return true; } };
然后我们将此侦听器设置为map:
ClusterManager mClusterManager = new ClusterManager<OurClusterItem>(getActivity(),getMap()); mClusterManager.setOnClusterItemClickListener(mClusterItemClickListener); googleMap.setOnMarkerClickListener(mClusterManager);
那么,有人可以为此提供解决方案吗?
谢谢!
解决方法
所以,我找到了它的解决方案 – 也许这对某人也有帮助.
因此,首先,我们需要使用自定义渲染器(继承自DefaultClusterRenderer). DefaultClusterRenderer有自己的缓存,包含ClusterItem和对应的Marker对:
public MarkerCache<T> mMarkerCache = new MarkerCache<T>();
所以我在CustomClusterRenderer中编写了下一个方法
public Marker getMarker(OurClusterItem clusterItem) { return mMarkerCache.get(clusterItem); }
在它之后,我已经添加了我们的片段2变量:
private Marker mCurrentSelectedMarker; private ClusterStore mCurrentSelectedClusterItem;
并更改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; } };
这就是全部,它就像一个魅力.