谷歌地图api自定义标记

时间:2013-01-15 20:00:14

标签: android google-maps google-api google-maps-markers

我正在为xml文件(RelativeLayout)中的自定义标记寻找好的tourtial或解决方案。

我有:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:layout_width="55dp"
    android:layout_height="65dp"
    android:src="@drawable/custom_marker" />

<TextView
    android:id="@+id/num_txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="5dp"
    android:gravity="center"
    android:text="20"
    android:textColor="#ce8223"
    android:textSize="25dp"
    android:textStyle="bold" />

    </RelativeLayout>

我如何将它用作标记?

1 个答案:

答案 0 :(得分:1)

嗯,有一种解决方法可以将您的布局转换为Drawable,可用作标记图像:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.your_layout, null);
view.setDrawingCacheEnabled(true);
view.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

view.buildDrawingCache();
//EDIT: updated drawable creation
Drawable d = new BitmapDrawable(context.getResources(), view.getDrawingCache().copy(Config.ARGB_8888, true)); //this drawable can be used as a marker

// **** EDIT2: don't forget to set bounds to your drawable
d.setBounds(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(false);

您可能理解,您需要存储Context指针才能获得LayoutInflater

希望有所帮助