How to take marker Tag when used cluster Algorithm in Android
Asked Answered
S

1

9
        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()));
    }
Sporogonium answered 16/3, 2017 at 18:34 Comment(0)
P
8

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;
    }
};
Patmore answered 16/3, 2017 at 19:12 Comment(6)
Firstly, thank you so much for your answers. I also think this. But my tag is hash values. So I don't know which one equals to others. What could you suggest me ?Sporogonium
I understand now. Perfect answer! Thank you @DanielNugentSporogonium
ClusterManager.OnClusterItemClickListener doesn't work because of onMarkerClick(Marker marker). Where I call this method ?Sporogonium
@Sporogonium Make sure you're calling this: mClusterManager.setOnClusterItemClickListener(mClusterItemClickListener); before you call mMap.setOnMarkerClickListener(mClusterManager);Patmore
Also, for more info (including InfoWindow stuff), see my other answer here: https://mcmap.net/q/893410/-android-maps-utils-clustering-show-infowindowPatmore
Thank you again for your interested. @DanielSporogonium

© 2022 - 2024 — McMap. All rights reserved.