Question

I am using maps in Android provided by MapQuest, but implemented in the same way as Google Maps so the same question applies.

For the overlay item, I would like to use a layout with an ImageView and TextView as the TextView will be displaying some information. The problem is I can only seem to show a drawable as my overlay which must come from the Drawable folder.

Here is the code for adding the overlay item:

private void loadSingleOverlay(TapControlledMap mapViewPassed, String title, String address)
{
    Drawable icon = getResources().getDrawable(R.drawable.marker2);

    OverlayItem newItem = new OverlayItem(point, title, address);

    overlay = new ExtendedItemizedOverlay(icon);
    overlay.addItem(newItem);

    // add a tap listener
    overlay.setTapListener(tapListener);

    List<Overlay> listOfOverlays = mapViewPassed.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(overlay);
    mapViewPassed.invalidate();

}

And the layout I want to inflate and use:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/marker_red" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1" />

</RelativeLayout>

Here is the result I am going for:

enter image description here

Thanks all

Was it helpful?

Solution

It's not easy. The only way I know is to create your view (e.g. layout with background and text view) and then render it as a bitmap.

public static Bitmap loadBitmapFromView(View v) {
    DisplayMetrics dm = v.getContext().getResources().getDisplayMetrics();  
    Bitmap b = Bitmap.createBitmap(Math.round(v.getMeasuredWidth() * dm.density), Math.round(v.getMeasuredHeight() * dm.density), Bitmap.Config.ARGB_8888);                
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }

You can then add to your map with something like below. You'll have to fiddle with the bounds depending on your marker view size.

Drawable d = new BitmapDrawable(Utils.loadBitmapFromView(yourView));
        d.setBounds(-35, -30, Math.round(d.getIntrinsicWidth() * dm.density) - 35, Math.round(d.getIntrinsicHeight() * dm.density) - 30);
        overlayItem.setMarker(d);
        overlayListView.add(overlay);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top