4

我在 Android 中使用 MapQuest 提供的地图,但实现方式与 Google Maps 相同,因此同样的问题也适用。

对于覆盖项目,我想使用带有 ImageView 和 TextView 的布局,因为 TextView 将显示一些信息。问题是我似乎只能显示一个drawable作为我的叠加层,它必须来自 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>

这是我想要的结果:

在此处输入图像描述

谢谢大家

4

1 回答 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);
于 2012-06-01T15:00:32.903 回答