Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set my own icon for markers in clusterer in Google Maps

I'm using GoogleMaps clustering. I have 4 different types of markers, each extends from one abstract class, each has its owm icon and it defined in MarkerOptions field. I use ClusterManager for this abstract class. When I just use

//marker is instance of one of extended classes and has abstract class type
clusterManager.addItem(marker);

After clustering it sets icon to default. How I can fix this? I was trying to use something like this:

//marker is abstract marker, getMarker returns the MarkerOptions of this marker
clusterManager.getMarkerCollection().addMarker(marker.getMarker());

but it doesn't work too, marker is printed with needed icon on map, but without clustering.

Should I create create some method in my abstract class or there are some way to do this extending from DefaultClusterRenderer? I haven't found some information about it using Google or learning google maps lib.

Thanks for helping!

like image 762
Vasilov Artur Avatar asked May 14 '14 15:05

Vasilov Artur


People also ask

How do I add custom icons to Google Maps?

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.


1 Answers

So, it was my foolish. Again, it shows, that I should pay more attention, studying library. If somebody is interested in answer, here it is: I was right supposing, that I need to override some method in DefaultClusterRenderer. So, the full way: Create own class and extend it from DefaultClusterRenderer:

public class OwnIconRendered extends DefaultClusterRenderer<AbstractMarker>

Then override method onBeforeClusterItemRendered:

@Override
protected void onBeforeClusterItemRendered(AbstractMarker item,
        MarkerOptions markerOptions) {
    markerOptions.icon(item.getMarker().getIcon());
}

The way is rather simple, but it seems to me, that clustering started to work slower. So, that's enough.

like image 54
Vasilov Artur Avatar answered Sep 18 '22 15:09

Vasilov Artur