KickingLettuce KickingLettuce - 1 year ago 115
Android Question

Using an xml Layout for a Google Map Marker for Android

I want to use a layout and not a drawable for my

I am using with Google Maps Api 2 for an Android app.

Like this:

Marker m = googleMap
.addMarker(new MarkerOptions()

However, it seems this is only designed for use from

I simply what an image with a background where I can change colors based on marker type. This is the layout I want to use:

<LinearLayout xmlns:android=""

android:layout_height="40dp" />


Pretty basic. How can I use this as a marker? (or at least replicate the end result that I am going for?)

The documents say you can use this:

fromResource (int resourceId)

So I assumed a layout might work.


I have taken the concept from answer below and reworked some things; this still is not working; no errors. Just will not show image.

LayoutInflater inflater = (LayoutInflater) getActivity()
View v = inflater.inflate(R.layout.map_marker, null);

m = googleMap
.addMarker(new MarkerOptions()

// method

public static Bitmap loadBitmapFromView(View v) {

if (v.getMeasuredHeight() <= 0) {
v.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
return b;
return null;

Answer Source

You cannot directly use a layout with BitmapDescriptorFactory.fromResource():

Creates a BitmapDescriptor using the resource id of an image.

However, you could draw the layout into a bitmap, and then use that. You'd need to:

  • Inflate the layout from xml (with a LayoutInflater).
  • Change whatever properties you want (e.g. background color).
  • Render this layout into a Bitmap, via Canvas, for example as described here.
  • Pass this bitmap into BitmapDescriptorFactory.fromBitmap().