Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a Drawable to a Bitmap to change the color of a Marker in Google Maps Android API v2

I'm converting an old application from v1 to v2, and I'm having a problem with the color of my Marker icons. I have a basic, white icon, and It needs to be colorized.

In v1, I did it this way :

Drawable d = DrawableUtils.resizeImageToDrawable(
    MapViewFragment.mapviewActivity,
    Configuration.Display.getDrawableFix(i),
    Configuration.MapView.getWaypointIconWidth(),
    Configuration.MapView.getWaypointIconHeight());
d.setColorFilter(color, Mode.MULTIPLY);

overlay = new MyFplnFixListItimizedOverlay(d);

Since v2 Markers do not accept Drawables for their icons, I thought about converting the Drawable to a Bitmap, like this :

Drawable d = DrawableUtils.resizeImageToDrawable(
    MapViewFragment.mapviewActivity,
    Configuration.Display.getDrawableFix(i),
    Configuration.MapView.getWaypointIconWidth(),
    Configuration.MapView.getWaypointIconHeight());
d.setColorFilter(color, Mode.MULTIPLY);

Bitmap icon = ((BitmapDrawable) d).getBitmap();

Marker marker = MapViewFragment.map.addMarker(new MarkerOptions()
    .position(point)
    .title(Integer.toString(fplnType))
    .visible(true)
    .icon(BitmapDescriptorFactory.fromBitmap(icon)));

But for some reason, it's not working. The icons stay white. Anyone knows why ?

Thanks in advance.

like image 290
Apoz Avatar asked Apr 16 '13 12:04

Apoz


People also ask

How do I change the color of a marker on Google Maps?

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 start a custom image on Google Maps marker for mobile app?

You can start by looking at the Canvas and Drawables from the Android Developer page. Now you also want to download a picture from an URL. URL url = new URL(user_image_url); HttpURLConnection conn = (HttpURLConnection) url. openConnection(); conn.


1 Answers

OK, here's how I did it at the end of the day :

Drawable d = DrawableUtils.resizeImageToDrawable(
                    MapViewFragment.mapViewActivity,
                    Configuration.Display.getDrawableFix(i),
                    Configuration.MapView.getWaypointIconWidth(),
                    Configuration.MapView.getWaypointIconHeight());
d.setColorFilter(color, Mode.MULTIPLY);

Bitmap b = ((BitmapDrawable) d).getBitmap();
Canvas myCanvas = new Canvas(b);

int myColor = b.getPixel(0,0);
ColorFilter filter = new LightingColorFilter(myColor, color);

Paint pnt = new Paint();
pnt.setColorFilter(filter);
myCanvas.drawBitmap(b,0,0,pnt);

Marker marker = MapViewFragment.map.addMarker(new MarkerOptions()
    .position(point)
    .title(Integer.toString(fplnType))
    .visible(true)
    .icon(BitmapDescriptorFactory.fromBitmap(b)));
like image 118
Apoz Avatar answered Sep 28 '22 06:09

Apoz