为Android版Google地图标记使用xml布局

The*_*ter 5 layout android imageview google-maps-android-api-2

我想使用一个布局,而不是一个drawable,因为我marker正在使用谷歌地图Api 2的Android应用程序.

像这样:

  Marker m = googleMap
                        .addMarker(new MarkerOptions()
                                .position(LOC)
                                .snippet(user)
                                .title(name)
                                .icon(BitmapDescriptorFactory.fromResource(R.layout.map_marker)));
Run Code Online (Sandbox Code Playgroud)

但是,它似乎只是为了在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>
Run Code Online (Sandbox Code Playgroud)

很基本的.我如何将其用作标记?(或者至少复制我想要的最终结果?)

文件说你可以用这个:

fromResource (int resourceId)
Run Code Online (Sandbox Code Playgroud)

所以我假设布局可能有效.

更新:

我从下面的答案中采用了这个概念并重新设计了一些东西; 这还不行; 没有错误.只是不会显示图像.

        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))));
Run Code Online (Sandbox Code Playgroud)

// 方法

  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;
    }
Run Code Online (Sandbox Code Playgroud)

mat*_*ash 6

您不能直接使用布局BitmapDescriptorFactory.fromResource():

使用图像的资源ID创建BitmapDescriptor .

但是,您可以将布局绘制到位图中,然后使用它.你需要:


Ada*_*hns 6

以下为我工作:

public static MarkerOptions createMarker(Context context, LatLng point, int bedroomCount) {
    MarkerOptions marker = new MarkerOptions();
    marker.position(point);
    int px = context.getResources().getDimensionPixelSize(R.dimen.map_marker_diameter);
    View markerView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_circle_text, null);
    markerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    markerView.layout(0, 0, px, px);
    markerView.buildDrawingCache();
    TextView bedNumberTextView = (TextView)markerView.findViewById(R.id.bed_num_text_view);
    Bitmap mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mDotMarkerBitmap);
    bedNumberTextView.setText(bedroomCount);
    markerView.draw(canvas);
    marker.icon(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap));
    return marker;
}
Run Code Online (Sandbox Code Playgroud)

map_circle_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bed_num_text_view"
    android:gravity="center"
    android:layout_width="@dimen/map_marker_diameter"
    android:layout_height="@dimen/map_marker_diameter"
    android:background="@drawable/map_circle_pin"
    tools:text="4+"
    android:textColor="@color/white">
</TextView>
Run Code Online (Sandbox Code Playgroud)

map_circle_pin.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="#666666"/>

    <size
        android:width="@dimen/map_marker_diameter"
        android:height="@dimen/map_marker_diameter"/>
</shape>
Run Code Online (Sandbox Code Playgroud)