安卓地图 - 使用布局作为覆盖物项

4
我正在使用由MapQuest提供的Android地图,但实现方式与Google Maps相同,因此同样的问题适用。
对于叠加项,我想使用一个包含ImageView和TextView的布局,因为TextView将显示一些信息。问题在于,我似乎只能显示一个来自Drawable文件夹的可绘制对象作为我的叠加项。
这是添加叠加项的代码:
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();

}

我想要加载并使用的布局:

 <?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>

我希望的结果是这样的:

在此输入图片描述

谢谢大家。
1个回答

4

这并不容易。我知道的唯一方法是创建您的视图(例如,具有背景和文本视图的布局),然后将其呈现为位图。

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;
    }

接着,您可以使用以下类似的代码向地图添加内容。根据您的标记视图大小,您需要调整范围。

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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接