Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage click on marker which is not in Cluster in Android Google Map API?

I am having trouble implementing OnClickListener for Marker(s) which are not in Cluster, i.e. not added using:

 mClusterManager.addItem(markerCluster);

I have set OnMarkerClickLister as follows:

 mMap.setOnCameraIdleListener(mClusterManager);
 mMap.setOnMarkerClickListener(mClusterManager);

If I just use:

 mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            return false;
        }
    });

the click on markers is not working at all.

For example: I have this situation:

enter image description here

Two green dots and big blue dot (with number 6) are one Cluster, but the red marker is not in Cluster as I don't want it to be Clustered like other markers. InfoWindow is normally showing when I click on red marker, but onMarkerClick doesn't work - as well as OnClusterItemClickListener. But OnClusterItemClickListener works when I click on markers which are in Cluster.

Hope you understand what I am trying to achieve. If not, please let me know.

like image 621
unipin Avatar asked Sep 06 '18 12:09

unipin


People also ask

How do I change the marker on Google Maps Android?

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 use marker clusters in Google Maps?

To navigate, press the arrow keys. The number on a cluster indicates how many markers it contains. Notice that as you zoom into any of the cluster locations, the number on the cluster decreases, and you begin to see the individual markers on the map. Zooming out of the map consolidates the markers into clusters again.

How do I remove a marker cluster from Google Maps?

Removing all markers You can then clear all the markers using clearMarkers something like this: markerClusterer. clearMarkers(); markers = [];


1 Answers

Updated answer

The image that you added in your edit explains the issue clearly, thanks for that!

In order too solve this issue we need to register the listener differently. Namely: by registering it to the ClusterManager's MarkerManager as that class handles everything of the markers now. We also need to add the NormalMarkers a bit differently, so lets go through it step by step:

1) Update the OnMarkerClickListener of the mMap:

mMap.setOnMarkerClickListener(mClusterManager.getMarkerManager()); // Note the `MarkerManager` here

2) This MarkerManager holds all the collections. We need to create a new collection on this manager to which we will add the NormalMarkers that should be displayed apart from the clusters:

MarkerManager.Collection normalMarkersCollection = mClusterManager.getMarkerManager().newCollection();

3) Adding the markers is done similar to how this was before, but with using the addMarker() method on the collection that we created. We must pass a MarkerOptions() object to this:

// Create a normal marker
val markerOptions = MarkerOptions()
    .position(new LatLng(...))
    .title("My marker")
    .snippet("With description")

// Add it to the collection
normalMarkersCollection.addMarker(markerOptions)

4) Last but not least: we want the OnClickListener on it:

normalMarkersCollection.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener {
    public boolean onMarkerClick(marker: Marker) {
        // NORMAL MARKER CLICKED!
        return false;
    }
})

5) Done! Now the normal markers are added to the map just like before, but with a working OnMarkerClickListener.


Earlier answer

(Setting the click listeners for the clusters and clustered items)

You should add the clicklistener to the mClusterManager. Setting the clicklistener on the mMap does not work when using the ClusterManager.

Thus instead of using mMap.setOnMarkerClickListener, set the ClusterItemClickListener on the cluster manager:

mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
    @Override
    public boolean onClusterItemClick(MyItem item) {
        Log.d("cluster item","clicked");
        return true;
    }
});

Additionally, if you want to capture the onclick of the clustered item item, use the ClusterClickListener:

mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItem>() {
    @Override
    public boolean onClusterClick(Cluster<MyItem> cluster) {
        Log.d("cluster","clicked");
        return true;
    }
});
like image 85
Alex Avatar answered Sep 25 '22 08:09

Alex