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

时间:2014-07-12 20:23:19

标签: android layout imageview google-maps-android-api-2

我想使用布局,而不是我的marker使用Google地图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;
    }

//方法

{{1}}

4 个答案:

答案 0 :(得分:6)

您无法直接使用BitmapDescriptorFactory.fromResource()的布局:

  

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

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

  • 从xml(使用LayoutInflater)充气布局。
  • 更改您想要的任何属性(例如背景颜色)。
  • 通过Bitmap将此布局渲染为Canvas,例如here所述。
  • 将此位图传递到BitmapDescriptorFactory.fromBitmap()

答案 1 :(得分:4)

以下对我有用:

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;
}

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>

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>

答案 2 :(得分:0)

我也有这个问题,这对我有用

    public static Bitmap createDrawableFromView(Context context, View view) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay()
            .getMetrics(displayMetrics);
    view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.layout(0, 0, displayMetrics.widthPixels,
            displayMetrics.heightPixels);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
            view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;

}

答案 3 :(得分:0)

为自定义标记制作布局:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/marker_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        tools:src="@drawable/ic_marker_selected" />

    <TextView
        android:id="@+id/marker_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#fff"
        android:textSize="14sp"
        tools:text="A" />

</merge>

像这样创建自己的MarkerView

private class CustomMarkerView(root: ViewGroup,
                               text: String?,
                               isSelected: Boolean) : FrameLayout(root.context) {
    private var mImage: ImageView
    private var mTitle: TextView

    init {
        View.inflate(context, R.layout.marker_layout, this)
        mImage = findViewById(R.id.marker_image)
        mTitle = findViewById(R.id.marker_title)
        measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
        mTitle.text = text
        if (isSelected) {
            mImage.setImageResource(R.drawable.ic_marker_selected)
        } else {
            mImage.setImageResource(R.drawable.ic_marker_not_selected)
        }
    }
}

现在,您需要从此自定义视图中获取BitmapDescriptor

fun getMarkerIcon(root: ViewGroup, text: String?, isSelected: Boolean): BitmapDescriptor? {
    val markerView = CustomMarkerView(root, text, isSelected)
    markerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
    markerView.layout(0, 0, markerView.measuredWidth, markerView.measuredHeight)
    markerView.isDrawingCacheEnabled = true
    markerView.invalidate()
    markerView.buildDrawingCache(false)
    return BitmapDescriptorFactory.fromBitmap(markerView.drawingCache)
}

最后,你可以像这样在你的标记上应用它:

fun addMarkerOnMaps() {
    val markerIcon = getMarkerIcon(
            root = activity.findViewById(R.id.activity_content_container) as ViewGroup,
            text = markerName,
            isSelected = true)

    googleMap!!.addMarker(MarkerOptions()
            .position(LatLng(coordinates.latitude, coordinates.longitude))
            .icon(markerIcon))
}