带有自定义背景和文字的Google地图标记

时间:2016-05-23 15:24:19

标签: android google-maps android-custom-view marker nine-patch

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

  • 9patch drawable as background
  • 自定义文字
  • 文字左侧可选择绘图(有些标记有文字,有些标记不可用)

我使用了 android google map utils library 中的 IconGenerator ,但不知何故,标记的整个可点击区域都会受到影响。标记周围的一个非常大的区域是可点击的并激活标记。它还存在内容填充和重力属性的问题。

我在地图上添加了自定义黑客程序,以便在不打开信息窗口的情况下将标记放在前面(我设置了一个没有内容可显示的信息窗口,我在Marker中称为showInfoWindow click事件监听器。)

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

我也尝试了here描述的实现,基本上是drawTextToBitmap()方法:

private Bitmap drawTextToBitmap(@DrawableRes int backgroundRes, String text) {
        Resources resources = getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, backgroundRes);
        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();

        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        bitmap = bitmap.copy(bitmapConfig, true);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // SET FONT COLOR (e.g. WHITE -> rgb(255,255,255))
        paint.setColor(Color.rgb(255, 255, 255));
        // SET FONT SIZE (e.g. 15)
        paint.setTextSize((int) (15 * scale));
        // SET SHADOW WIDTH, POSITION AND COLOR (e.g. BLACK)
        paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);

        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        int x = (bitmap.getWidth() - bounds.width()) / 2;
        int y = (bitmap.getHeight() + bounds.height()) / 2;
        canvas.drawText(text, x, y, paint);

        return bitmap;
    }

但它没有按预期工作,因为我的文字比背景更大,而且我也不知道如何在文本的左边添加资源。

这是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,这是标记图标在使用您的解决方案时的样子:

Marker icon using @Harpreet solution

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

1 个答案:

答案 0 :(得分: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;
    }

注意: - 只需填写上下文,然后将完整的自定义创建的视图/布局添加为查看

的对象

它会帮助你。