【发布时间】:2014-09-03 05:10:42
【问题描述】:
我想为我的marker 使用布局而不是可绘制对象,我正在将 Google Maps Api 2 用于 Android 应用程序。
像这样:
Marker m = googleMap
.addMarker(new MarkerOptions()
.position(LOC)
.snippet(user)
.title(name)
.icon(BitmapDescriptorFactory.fromResource(R.layout.map_marker)));
但是,这似乎只为在drawable 文件夹中使用而设计。
我只是一个带有背景的图像,我可以根据标记类型更改颜色。这是我要使用的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#222"
android:id="@+id/llMarker">
<ImageView
android:id="@+id/ivMarker"
android:layout_marginTop="5dp"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
非常基本。我怎样才能使用它作为标记? (或者至少复制我想要的最终结果?)
文件说你可以用这个:
fromResource (int resourceId)
所以我认为布局可能有效。
更新:
我从下面的答案中汲取了这个概念并重新设计了一些东西;这仍然不起作用;没有错误。只是不会显示图像。
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.map_marker, null);
m = googleMap
.addMarker(new MarkerOptions()
.position(LOC)
.snippet(user)
.title(name)
.icon(BitmapDescriptorFactory.fromBitmap(loadBitmapFromView(v))));
// 方法
public static Bitmap loadBitmapFromView(View v) {
if (v.getMeasuredHeight() <= 0) {
v.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
return b;
}
return null;
}
【问题讨论】:
-
以防万一,您是否尝试过其他方法从视图中获取位图?除非您明确设置位图的尺寸,否则这可能不起作用。
-
@matiash 这是我现在正在做的事情(见更新的代码);使用该链接中的方法。实际上,我曾经意外地夸大了错误的布局(来自
adaptergetView()方法的行布局)。它试图在地图上显示这一点。所以然后我使用了上面看到的正确布局(有和没有ImageView,它什么都不显示)。所以我认为代码更接近。也许问题是xml? -
如果你调试,它会进入if语句吗?
-
实际上是的,现在它正在工作。不知道第一次发生了什么。我会标记你是正确的。我只需要弄清楚如何动态更改布局的部分(
LinearLayout背景颜色和ImageViewsrc);但我想我可以让那个工作.. -
@KickingLettuce:你是如何解决这个问题的:我有类似的问题:你有机会解决这个问题吗?如果是这样怎么办?我有类似的问题:stackoverflow.com/questions/25965799/xml-layout-to-map-marker
标签: android layout imageview google-maps-android-api-2