Home » Android » java – How to take marker Tag when used cluster Algorithm in Android

java – How to take marker Tag when used cluster Algorithm in Android

Posted by: admin June 15, 2020 Leave a comment

Questions:
        Marker marker = mMap.addMarker(markerOptions);
        marker.setTag(poi.getKey());
        marker.hideInfoWindow();
        builder.include(marker.getPosition()); 

This code works properly because I put as manuel. I did everything marker.tag(). I used hash key for retriving which my pin.

mClusterManager.addItem() I want to use this method. But also I want to take marker tag. So I don’t want to add to map as Marker marker = mMap.addMarker(markerOptions);

How can I solve this problem?

I want to explain More Detail.

public class PoiItem implements ClusterItem{
private final LatLng mPosition;
private String mTitle;
private String mSnippet;
private String tag;
private ResponsePoi pois;

public PoiItem(ResponsePoi poi,double lat, double lng,String tag) {
    mPosition = new LatLng(lat, lng);
    pois = poi;
    mTitle = poi.getName();
    mSnippet = poi.getAddress();
    this.tag = tag;
}


 private void setMyCluster(){
        mClusterManager = new ClusterManager<PoiItem>         (MainActivity.this,mMap);
        mMap.setOnCameraIdleListener(mClusterManager);
        mMap.setOnMarkerClickListener(mClusterManager);
    }

    setMyCluster();
    for(HashMap.Entry<String, ResponsePoi> poi : pois.entrySet()) {
        MarkerOptions markerOptions = new MarkerOptions()
                .snippet(poi.getValue().getAddress())
                .title(poi.getValue().getName())
                .position(new LatLng(poi.getValue().getLocation().getL().get(0),poi.getValue().getLocation().getL().get(1)))
                .icon(BitmapDescriptorFactory.fromBitmap(smallMarker));
        /*
        Marker marker = mMap.addMarker(markerOptions);
        marker.setTag(poi.getKey());
        marker.hideInfoWindow();
        builder.include(marker.getPosition());  */

        mClusterManager.addItem(new   PoiItem(poi.getValue(),poi.getValue().getLocation().getL().get(0),poi.getValue().getLocation().getL().get(1),poi.getKey()));
    }
How to&Answers:

You can just add a Marker tag field to your custom MyItem class.

For example:

public class MyItem implements ClusterItem {
    private final LatLng mPosition;
    private final String mTitle;
    private final String mSnippet;
    private final String mTag; // <- This is the tag

    public MyItem(double lat, double lng, String t, String s, String tg) {
        mPosition = new LatLng(lat, lng);
        mTitle = t;
        mSnippet = s;
        mTag = tg;
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }

    public String getTitle(){
        return mTitle;
    }

    public String getSnippet(){
        return mSnippet;
    }

    public String getTag(){
        return mTag;
    }
}

Then give the tag to the MyItem constructor when using the addItem() method:

MyItem item = new MyItem(lat, lng, "title", "snippet", poi.getKey());
mClusterManager.addItem(item);

Ensure that the cluster manager is handling click events for cluster items:

mClusterManager.setOnClusterItemClickListener(mClusterItemClickListener);
mMap.setOnMarkerClickListener(mClusterManager);

Then define your ClusterManager.OnClusterItemClickListener, where you can access the tag when the cluster item (Marker) is clicked:

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

    @Override
    public boolean onClusterItemClick(MyItem item) {
        if (item.getTag().equals("SomeValue")) {
            //Do something!
        }
        return true;
    }
};