Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Map IconGenerator - Making a transparent marker icon

So I'm using a ClusterManagerto cluster my Markers, so that the user can have a better experience.

I have actually implemente Google's code, which I found here. Imagine now that my Marker icon is a ball. I want the background of the icon to be transparent, not white.

On Google's original tutorial they set a ImageView to the IconGenerator, like this:

public class MyClusterManagerRenderer extends DefaultClusterRenderer<ClusteredMarker> {

private final IconGenerator mIconGenerator;
private final ImageView mImageView;

public MyClusterManagerRenderer(Context context, GoogleMap googleMap,
                                ClusterManager<ClusteredMarker> clusterManager){

    super(context, googleMap, clusterManager);

    mIconGenerator = new IconGenerator(context);
    mImageView = new ImageView(context);
    mIconGenerator.setContentView(mImageView);
}

@Override
protected void onBeforeClusterItemRendered(ClusteredMarker item, MarkerOptions markerOptions) {

    mImageView.setImageResource(item.iconPicture);
    Bitmap icon = mIconGenerator.makeIcon();
    markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)).title(item.user);
}

...
}

I have tried several ways to make my icon transparent, like calling:

mImageView.setBackgroundColor(Color.TRANSPARENT);

but without success. The only way I managed to find a solution is to directly attach my transparent image to the IconGenerator, like this:

mIconGenerator.setBackground(aContext.getResources().getDrawable(R.drawable.ball));

The downside of this approach is that a ImageView have some interesting methods that I would like to call, like setPadding, while the IconGenerator doesn't have that.

So, is there a way to make my icon transparent, using the ImageView?

Thank you,

like image 480
Danilo Setton Avatar asked Aug 22 '15 17:08

Danilo Setton


People also ask

How do I start a custom image on Google Maps marker for mobile app?

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.

What are Android markers?

Markers are objects of type Marker , and are added to the map with the GoogleMap. addMarker(markerOptions) method. Markers are designed to be interactive. They receive click events by default, and are often used with event listeners to bring up info windows.


1 Answers

The solution I found is this:

private static final Drawable TRANSPARENT_DRAWABLE = new ColorDrawable(Color.TRANSPARENT);

// Make the background of marker transparent
mIconGenerator.setBackground(TRANSPARENT_DRAWABLE);
like image 163
IgorGanapolsky Avatar answered Nov 15 '22 01:11

IgorGanapolsky