我在 Android 中使用 MapQuest 提供的 map ,但其实现方式与 Google map 相同,因此同样的问题也适用。
对于叠加项,我想使用带有 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>
这是我想要的结果:
谢谢大家
最佳答案
这并不容易。我知道的唯一方法是创建您的 View (例如,带有背景和 TextView 的布局),然后将其呈现为位图。
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;
}
然后您可以使用如下所示的内容添加到您的 map 。您必须根据您的标记 View 大小调整边界。
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);
关于Android map - 使用布局作为叠加项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10851593/