ANDROID: How make custom view(layout) for marker on Google Maps

ihrupin picture ihrupin · Oct 19, 2011 · Viewed 7.1k times · Source

How to make custom view for marker or how to make custom layout. like this... enter image description here and please look at screenshot:

enter image description here

Answer

Rakesh Rangani picture Rakesh Rangani · Jul 10, 2014

custom marker for Google Maps using a layout

View markerView = ((LayoutInflater) getActivity()
                        .getSystemService(
                                getActivity().LAYOUT_INFLATER_SERVICE))
                        .inflate(R.layout.map_marker, null);

set marker

 marker = map.addMarker(new MarkerOptions()
                                        .position(latLng)
                                        .title(strName)
                                        .snippet(strStatus)
                                        .icon(BitmapDescriptorFactory
                                                .fromBitmap(createDrawableFromView(
                                                        getActivity(),
                                                        markerView))));

Create Bitmap from Drawable

public static Bitmap createDrawableFromView(Context context, View view) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay()
            .getMetrics(displayMetrics);
    view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.layout(0, 0, displayMetrics.widthPixels,
            displayMetrics.heightPixels);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
            view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}