Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of Custom marker on Map in Android

I'm trying to change the color of markers on Map. I want shape of a common marker but with custom colors. I've tried the following code but it doesn't make any change in color and doesn't give any error as well. Can you please guide any solution or any alternative approach? Any help would be highly appreciated.

Code:

Drawable background = context.getResources().getDrawable(R.drawable.pink_map);
    if (background instanceof ShapeDrawable) {
        ((ShapeDrawable)background).getPaint().setColor(getResources().getColor(R.color.theme_color));
    } else if (background instanceof GradientDrawable) {
        ((GradientDrawable)background).setColor(getResources().getColor(R.color.theme_color));
    }

    // convert drawable to bitmap
    Bitmap bitmap = ((BitmapDrawable) background).getBitmap();

    Marker marker = mMap.addMarker(new MarkerOptions().position(sydney)
                    .title("My Location")
                    .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
like image 787
Usman Ishrat Avatar asked Jan 11 '16 06:01

Usman Ishrat


People also ask

How do you change the color of a marker on a map?

To edit the marker color, click or tap on the marker icon. When you do that, you can change both the color of the marker and its style. Go for the color or style you want to change and then click OK to see the effect. Click on the button labeled Done to save your new marker color settings.

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.


1 Answers

This is working for me:

mMap.addMarker(new MarkerOptions()
                    .position(contactLatLng)
                    .title(contactName)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_blue))
                    );

The important thing is to import the image asset as 'Action Bar and Tab Icons' option. Then you choose the theme CUSTOM and you can select a custom color to apply your image. enter image description here

like image 146
abanet Avatar answered Oct 12 '22 06:10

abanet