【问题标题】:Using an xml Layout for a Google Map Marker for Android为 Android 的 Google 地图标记使用 xml 布局
【发布时间】: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 背景颜色和ImageView src);但我想我可以让那个工作..
  • @KickingLettuce:你是如何解决这个问题的:我有类似的问题:你有机会解决这个问题吗?如果是这样怎么办?我有类似的问题:stackoverflow.com/questions/25965799/xml-layout-to-map-marker

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


【解决方案1】:

为您的自定义标记制作布局:

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

【讨论】:

    【解决方案2】:

    以下内容对我有用:

    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>
    

    【讨论】:

    • buildDrawingCache() 已过时,如果您将视图直接绘制到 Canvas 中,您甚至不需要它。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

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

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      相关资源
      最近更新 更多