Android 谷歌地图标记与自定义背景和文本

Android 谷歌地图标记与自定义背景和文本,android,google-maps,android-custom-view,marker,nine-patch,Android,Google Maps,Android Custom View,Marker,Nine Patch,我正在尝试自定义一个地图标记,以便它可以显示3个主要功能: 9可作为背景绘制的面片 自定义文本 可选,可在文本左侧绘制(有些标记有,有些没有) 我使用了android google map utils库中的IconGenerator,但不知怎的,标记的整个可点击区域都受到了影响。标记周围有一个很大的区域可以单击并激活标记。它还存在内容填充和重力属性的问题 我在地图上添加了自定义hack,以便在不打开信息窗口的情况下将标记放在前面(我设置了一个信息窗口,没有要显示的内容,并且在标记单击事件侦听

我正在尝试自定义一个地图标记,以便它可以显示3个主要功能:

  • 9可作为背景绘制的面片
  • 自定义文本
  • 可选,可在文本左侧绘制(有些标记有,有些没有)
我使用了android google map utils库中的IconGenerator,但不知怎的,标记的整个可点击区域都受到了影响。标记周围有一个很大的区域可以单击并激活标记。它还存在内容填充和重力属性的问题

我在地图上添加了自定义hack,以便在不打开信息窗口的情况下将标记放在前面(我设置了一个信息窗口,没有要显示的内容,并且在标记单击事件侦听器中调用了showInfoWindow)

如何手动创建标记的图标并避免其周围有大的可点击区域的问题

我还尝试了上述实现,基本上是drawTextToBitmap()方法:

但它并没有像预期的那样工作,因为我的文本比背景大,而且我也不知道如何在文本的左侧添加资源

这是我正在使用的IconGenerator的实现:

private BitmapDescriptor getBitmapDescriptorForProduct(@NonNull final MyCustomObject product, boolean isActive) {
    if (product.shouldDisplayWithLabel) {
        // We use an custom layout for displaying markers on the map
        @SuppressLint("InflateParams") View view =
                LayoutInflater.from(getActivity()).inflate(R.layout.my_custom_label, null);

        // Get the text view which will display the text 
        TextView label = (TextView) view.findViewById(R.id.text);
        // Set the left drawable if needed
        label.setCompoundDrawablesWithIntrinsicBounds(product.shouldDisplayWithDrawable ? R.drawable.my_custom_drawable : 0, 0,
                                                      0, 0);
        // Set the text for the label
        label.setText(product.label);
        label.setBackgroundResource(R.drawable.my_custom_background);

        // Set the layout for the icon
        mIconGenerator.setContentView(view); // set the custom layout

        // We don't want the default background
        mIconGenerator.setBackground(null);
        // Disable the content padding as we handle it in the view and in the 9patch resources
        mIconGenerator.setContentPadding(0, 0, 0, 0);

        // Make the icon and create the BitmapDescriptor necessary for the marker creation
        Bitmap icon = mIconGenerator.makeIcon();
        return BitmapDescriptorFactory.fromBitmap(icon);
    } else {
        // Lazy initialization for the normal markers
        if (null == simpleMarkerIcon) {
            simpleMarkerIcon = BitmapDescriptorFactory.fromResource(R.drawable.my_simple_marker);
        }

        // Reuse the bitmap for the simple markers that will be displayed on the map.
        return simpleMarkerIcon;
    }
} 
LE: @Harpreet,这是使用您的解决方案时标记图标的外观:

如您所见,位图中未正确显示视图的属性

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;
    }
注意:-只需填写上下文,并将完整的自定义视图/布局添加为视图的对象


这会对你有所帮助。

似乎这种方法非常占用内存,我运行这种方法的时候99%都会出现OOM异常。我还使用你的方法添加了一个带有位图结果的SS。如果你同时在地图上显示多个标记,你也需要使用Google群集。应用程序的设计不依赖群集,因此,这不是我目前的选择。我已经更新了这个问题,并包含了IconGenerator的实现。这是非常接近我需要的,但触摸面积的标记是非常大的。
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;
    }